3e0a741076f2e3c80e3b8b5e335484a815fb94a5
[controller.git] / opendaylight / config / yang-test / src / test / java / org / opendaylight / controller / config / yang / test / impl / NetconfTestImplModuleTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.test.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13
14 import com.google.common.collect.Lists;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import javax.management.InstanceAlreadyExistsException;
19 import javax.management.ObjectName;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.api.IdentityAttributeRef;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
26 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
27 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
30 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
31
32 public class NetconfTestImplModuleTest  extends AbstractConfigTest {
33
34     public static final String TESTING_DEP_PREFIX = "testing-dep";
35     private NetconfTestImplModuleFactory factory;
36     private final String instanceName = "n1";
37
38     @Before
39     public void setUp() {
40
41         factory = new NetconfTestImplModuleFactory();
42         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory,
43                 new DepTestImplModuleFactory(), new IdentityTestModuleFactory()));
44     }
45
46     @Override
47     protected BindingRuntimeContext getBindingRuntimeContext() {
48         final BindingRuntimeContext ret = super.getBindingRuntimeContext();
49         doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
50         doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
51         return ret;
52     }
53
54     @Test
55     public void testIdentities() throws Exception {
56         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57
58         ObjectName nameCreated = transaction.createModule(IdentityTestModuleFactory.NAME, instanceName);
59         IdentityTestModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, IdentityTestModuleMXBean.class);
60
61         final IdentitiesContainer c = new IdentitiesContainer();
62         c.setAfi(new IdentityAttributeRef(TestIdentity2.QNAME.toString()));
63         mxBean.setIdentitiesContainer(c);
64         transaction.commit();
65     }
66
67     @Test
68     public void testDependencyList() throws Exception {
69         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
70
71         ObjectName on = createInstance(transaction, instanceName, 4);
72         transaction.validateConfig();
73         CommitStatus status1 = transaction.commit();
74
75         assertBeanCount(1, factory.getImplementationName());
76         assertBeanCount(4 + 1, DepTestImplModuleFactory.NAME);
77         assertStatus(status1, 1 + 4 + 1, 0, 0);
78
79         transaction = configRegistryClient.createTransaction();
80
81         NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(ObjectNameUtil.withoutTransactionName(on),
82                 NetconfTestImplModuleMXBean.class);
83         proxy.getComplexList();
84         List<ObjectName> testingDeps = proxy.getTestingDeps();
85         ObjectName testingDep = proxy.getTestingDep();
86
87         assertEquals(TESTING_DEP_PREFIX, ObjectNameUtil.getInstanceName(testingDep));
88         assertTestingDeps(testingDeps, 4);
89
90         transaction.abortConfig();
91
92         // check that reuse logic works - equals on list of dependencies.
93         transaction = configRegistryClient.createTransaction();
94         CommitStatus status2 = transaction.commit();
95         assertStatus(status2, 0, 0, 6);
96
97         // replace single dependency
98         transaction = configRegistryClient.createTransaction();
99         String instanceName1 = TESTING_DEP_PREFIX + 1;
100         transaction.destroyModule(DepTestImplModuleFactory.NAME, instanceName1);
101         transaction.createModule(DepTestImplModuleFactory.NAME, instanceName1);
102         CommitStatus status3 = transaction.commit();
103         assertStatus(status3, 1, 1, 4);
104
105     }
106
107     @Test
108     public void testNullCheckInListOfDependencies() throws Exception {
109         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
110
111         ObjectName on = createInstance(transaction, instanceName, 4);
112         NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
113         proxy.setTestingDeps(null);
114         assertTrue(proxy.getTestingDeps().isEmpty());
115         proxy.setTestingDeps(Collections.<ObjectName>emptyList());
116     }
117
118     private void assertTestingDeps(List<ObjectName> testingDeps, int i) {
119         assertEquals(i, testingDeps.size());
120
121         int c = 1;
122         for (ObjectName testingDep : testingDeps) {
123             assertEquals(TESTING_DEP_PREFIX + Integer.toString(c++), ObjectNameUtil.getInstanceName(testingDep));
124         }
125     }
126
127
128     private ObjectName createInstance(ConfigTransactionJMXClient transaction, String instanceName, int depsCount)
129             throws InstanceAlreadyExistsException {
130         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
131         NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NetconfTestImplModuleMXBean.class);
132
133         ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX);
134         mxBean.setTestingDep(dep);
135
136         ArrayList<ObjectName> testingDeps = Lists.newArrayList();
137         for (int i = 0; i < depsCount; i++) {
138             dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX + Integer.toString(i + 1));
139             testingDeps.add(dep);
140         }
141         mxBean.setTestingDeps(testingDeps);
142
143         return nameCreated;
144     }
145
146 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.