Bug 6859 - Binding generator v1 refactoring
[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 import com.google.common.collect.Lists;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import javax.management.InstanceAlreadyExistsException;
18 import javax.management.ObjectName;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.IdentityAttributeRef;
22 import org.opendaylight.controller.config.api.jmx.CommitStatus;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
25 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
26 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
27 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
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
31 public class NetconfTestImplModuleTest  extends AbstractConfigTest {
32
33     public static final String TESTING_DEP_PREFIX = "testing-dep";
34     private NetconfTestImplModuleFactory factory;
35     private final String instanceName = "n1";
36
37     @Before
38     public void setUp() {
39
40         this.factory = new NetconfTestImplModuleFactory();
41         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext,this.factory,
42                 new DepTestImplModuleFactory(), new IdentityTestModuleFactory()));
43     }
44
45     @Override
46     protected BindingRuntimeContext getBindingRuntimeContext() {
47         final BindingRuntimeContext ret = super.getBindingRuntimeContext();
48         doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
49         doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
50         return ret;
51     }
52
53     @Test
54     public void testIdentities() throws Exception {
55         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
56
57         final ObjectName nameCreated = transaction.createModule(IdentityTestModuleFactory.NAME, this.instanceName);
58         final IdentityTestModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, IdentityTestModuleMXBean.class);
59
60         final IdentitiesContainer c = new IdentitiesContainer();
61         c.setAfi(new IdentityAttributeRef(TestIdentity2.QNAME.toString()));
62         mxBean.setIdentitiesContainer(c);
63         transaction.commit();
64     }
65
66     @Test
67     public void testDependencyList() throws Exception {
68         ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
69
70         final ObjectName on = createInstance(transaction, this.instanceName, 4);
71         transaction.validateConfig();
72         final CommitStatus status1 = transaction.commit();
73
74         assertBeanCount(1, this.factory.getImplementationName());
75         assertBeanCount(4 + 1, DepTestImplModuleFactory.NAME);
76         assertStatus(status1, 1 + 4 + 1, 0, 0);
77
78         transaction = this.configRegistryClient.createTransaction();
79
80         final NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(ObjectNameUtil.withoutTransactionName(on),
81                 NetconfTestImplModuleMXBean.class);
82         proxy.getComplexList();
83         final List<ObjectName> testingDeps = proxy.getTestingDeps();
84         final ObjectName testingDep = proxy.getTestingDep();
85
86         assertEquals(TESTING_DEP_PREFIX, ObjectNameUtil.getInstanceName(testingDep));
87         assertTestingDeps(testingDeps, 4);
88
89         transaction.abortConfig();
90
91         // check that reuse logic works - equals on list of dependencies.
92         transaction = this.configRegistryClient.createTransaction();
93         final CommitStatus status2 = transaction.commit();
94         assertStatus(status2, 0, 0, 6);
95
96         // replace single dependency
97         transaction = this.configRegistryClient.createTransaction();
98         final String instanceName1 = TESTING_DEP_PREFIX + 1;
99         transaction.destroyModule(DepTestImplModuleFactory.NAME, instanceName1);
100         transaction.createModule(DepTestImplModuleFactory.NAME, instanceName1);
101         final CommitStatus status3 = transaction.commit();
102         assertStatus(status3, 1, 1, 4);
103
104     }
105
106     @Test
107     public void testNullCheckInListOfDependencies() throws Exception {
108         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
109
110         final ObjectName on = createInstance(transaction, this.instanceName, 4);
111         final NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
112         proxy.setTestingDeps(null);
113         assertTrue(proxy.getTestingDeps().isEmpty());
114         proxy.setTestingDeps(Collections.<ObjectName>emptyList());
115     }
116
117     private void assertTestingDeps(final List<ObjectName> testingDeps, final int i) {
118         assertEquals(i, testingDeps.size());
119
120         int c = 1;
121         for (final ObjectName testingDep : testingDeps) {
122             assertEquals(TESTING_DEP_PREFIX + Integer.toString(c++), ObjectNameUtil.getInstanceName(testingDep));
123         }
124     }
125
126
127     private ObjectName createInstance(final ConfigTransactionJMXClient transaction, final String instanceName, final int depsCount)
128             throws InstanceAlreadyExistsException {
129         final ObjectName nameCreated = transaction.createModule(this.factory.getImplementationName(), instanceName);
130         final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NetconfTestImplModuleMXBean.class);
131
132         ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX);
133         mxBean.setTestingDep(dep);
134
135         final ArrayList<ObjectName> testingDeps = Lists.newArrayList();
136         for (int i = 0; i < depsCount; i++) {
137             dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX + Integer.toString(i + 1));
138             testingDeps.add(dep);
139         }
140         mxBean.setTestingDeps(testingDeps);
141
142         return nameCreated;
143     }
144
145 }

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