Add test for generated code checking list of dependencies.
[controller.git] / opendaylight / config / yang-test / src / test / java / org / opendaylight / controller / config / yang / test / impl / NetconfTestImplModuleTest.java
1 package org.opendaylight.controller.config.yang.test.impl;
2
3 import com.google.common.collect.Lists;
4 import junit.framework.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.opendaylight.controller.config.api.jmx.CommitStatus;
8 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
9 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
10 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
11 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 public class NetconfTestImplModuleTest  extends AbstractConfigTest {
25
26     public static final String TESTING_DEP_PREFIX = "testing-dep";
27     private NetconfTestImplModuleFactory factory;
28     private final String instanceName = "n1";
29
30     @Before
31     public void setUp() {
32
33         factory = new NetconfTestImplModuleFactory();
34         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(factory,
35                 new DepTestImplModuleFactory()));
36     }
37
38     @Test
39     public void testDependencyList() throws Exception {
40         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
41
42         ObjectName on = createInstance(transaction, instanceName, 4);
43         transaction.validateConfig();
44         CommitStatus status1 = transaction.commit();
45
46         assertBeanCount(1, factory.getImplementationName());
47         assertBeanCount(4 + 1, DepTestImplModuleFactory.NAME);
48         assertStatus(status1, 1 + 4 + 1, 0, 0);
49
50         transaction = configRegistryClient.createTransaction();
51
52         NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(ObjectNameUtil.withoutTransactionName(on),
53                 NetconfTestImplModuleMXBean.class);
54         proxy.getComplexList();
55         List<ObjectName> testingDeps = proxy.getTestingDeps();
56         ObjectName testingDep = proxy.getTestingDep();
57
58         Assert.assertEquals(TESTING_DEP_PREFIX, ObjectNameUtil.getInstanceName(testingDep));
59         assertTestingDeps(testingDeps, 4);
60
61         transaction.abortConfig();
62
63         // check that reuse logic works - equals on list of dependencies.
64         transaction = configRegistryClient.createTransaction();
65         CommitStatus status2 = transaction.commit();
66         assertStatus(status2, 0, 0, 6);
67
68         // replace single dependency
69         transaction = configRegistryClient.createTransaction();
70         String instanceName1 = TESTING_DEP_PREFIX + 1;
71         transaction.destroyModule(DepTestImplModuleFactory.NAME, instanceName1);
72         transaction.createModule(DepTestImplModuleFactory.NAME, instanceName1);
73         CommitStatus status3 = transaction.commit();
74         assertStatus(status3, 1, 1, 4);
75
76     }
77
78     @Test
79     public void testNullCheckInListOfDependencies() throws Exception {
80         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
81
82         ObjectName on = createInstance(transaction, instanceName, 4);
83         NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
84         try{
85             proxy.setTestingDeps(null);
86             fail();
87         }catch(RuntimeException e) {
88             Throwable cause = e.getCause();
89             assertNotNull(cause);
90             assertTrue("Invalid type " + cause, cause instanceof IllegalArgumentException);
91             assertEquals("Null not supported", cause.getMessage());
92         }
93         proxy.setTestingDeps(Collections.<ObjectName>emptyList());
94     }
95
96     private void assertTestingDeps(List<ObjectName> testingDeps, int i) {
97         Assert.assertEquals(i, testingDeps.size());
98
99         int c = 1;
100         for (ObjectName testingDep : testingDeps) {
101             Assert.assertEquals(TESTING_DEP_PREFIX + Integer.toString(c++), ObjectNameUtil.getInstanceName(testingDep));
102         }
103     }
104
105
106     private ObjectName createInstance(ConfigTransactionJMXClient transaction, String instanceName, int depsCount)
107             throws InstanceAlreadyExistsException {
108         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
109         NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NetconfTestImplModuleMXBean.class);
110
111         ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX);
112         mxBean.setTestingDep(dep);
113
114         ArrayList<ObjectName> testingDeps = Lists.newArrayList();
115         for (int i = 0; i < depsCount; i++) {
116             dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX + Integer.toString(i + 1));
117             testingDeps.add(dep);
118         }
119         mxBean.setTestingDeps(testingDeps);
120
121         return nameCreated;
122     }
123
124 }

©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.