BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / ietf-stateful07 / src / test / java / org / opendaylight / protocol / pcep / ietf / Stateful07SessionProposalFactoryModuleTest.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.protocol.pcep.ietf;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.api.ValidationException;
19 import org.opendaylight.controller.config.api.jmx.CommitStatus;
20 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
21 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
22 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import org.opendaylight.controller.config.yang.pcep.stateful07.cfg.Stateful07PCEPSessionProposalFactoryModuleFactory;
24 import org.opendaylight.controller.config.yang.pcep.stateful07.cfg.Stateful07PCEPSessionProposalFactoryModuleMXBean;
25
26 public class Stateful07SessionProposalFactoryModuleTest extends AbstractConfigTest {
27
28     private static final String INSTANCE_NAME = "pcep-stateful07-proposal";
29     private static final String FACTORY_NAME = Stateful07PCEPSessionProposalFactoryModuleFactory.NAME;
30
31     @Before
32     public void setUp() throws Exception {
33         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, new Stateful07PCEPSessionProposalFactoryModuleFactory()));
34     }
35
36     @Test
37     public void testValidationExceptionDeadTimerValueNotSet() throws Exception {
38         try {
39             createInstance(null, (short) 100, true, true, true);
40             fail();
41         } catch (final ValidationException e) {
42             assertTrue(e.getMessage().contains("DeadTimerValue value is not set"));
43         }
44     }
45
46     @Test
47     public void testValidationExceptionKeepAliveTimerNotSet() throws Exception {
48         try {
49             createInstance((short) 200, null, true, true, true);
50             fail();
51         } catch (final ValidationException e) {
52             assertTrue(e.getMessage().contains("KeepAliveTimerValue value is not set"));
53         }
54     }
55
56     @Test
57     public void testValidationExceptionStatefulNotSet() throws Exception {
58         try {
59             createInstance((short) 200, (short) 100, null, false, false);
60             fail();
61         } catch (final ValidationException e) {
62             assertTrue(e.getMessage().contains("Stateful value is not set"));
63         }
64     }
65
66     @Test
67     public void testValidationExceptionActiveNotSet() throws Exception {
68         try {
69             createInstance((short) 200, (short) 100, true, null, true);
70             fail();
71         } catch (final ValidationException e) {
72             assertTrue(e.getMessage().contains("Active value is not set"));
73         }
74     }
75
76     @Test
77     public void testValidationExceptionInstantiatedNotSet() throws Exception {
78         try {
79             createInstance((short) 200, (short) 100, true, true, null);
80             fail();
81         } catch (final ValidationException e) {
82             assertTrue(e.getMessage().contains("Initiated value is not set"));
83         }
84     }
85
86     @Test
87     public void testValidationExceptionKeepAliveTimerMinValue() throws Exception {
88         try {
89             createInstance((short) 200, (short) -10, true, true, true);
90             fail();
91         } catch (final ValidationException e) {
92             assertTrue(e.getMessage().contains("minimum value is 1."));
93         }
94     }
95
96     @Test
97     public void testStatefulAfterCommitted() throws Exception {
98         createInstance((short) 200, (short) 100, false, true, true);
99         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
100         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(
101                 FACTORY_NAME, INSTANCE_NAME), Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
102         assertTrue(mxBean.getStateful());
103     }
104
105     @Test
106     public void testCreateBean() throws Exception {
107         final CommitStatus status = createInstance();
108         assertBeanCount(1, FACTORY_NAME);
109         assertStatus(status, 1, 0, 0);
110     }
111
112     @Test
113     public void testReusingOldInstance() throws Exception {
114         createInstance();
115         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
116         assertBeanCount(1, FACTORY_NAME);
117         final CommitStatus status = transaction.commit();
118         assertBeanCount(1, FACTORY_NAME);
119         assertStatus(status, 0, 0, 1);
120     }
121
122     @Test
123     public void testReconfigure() throws Exception {
124         createInstance();
125         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
126         assertBeanCount(1, FACTORY_NAME);
127         transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
128                 Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
129         final CommitStatus status = transaction.commit();
130         assertBeanCount(1, FACTORY_NAME);
131         assertStatus(status, 0, 0, 1);
132     }
133
134     private CommitStatus createInstance(final Short deadTimer, final Short keepAlive, final Boolean stateful, final Boolean active,
135             final Boolean instant) throws Exception {
136         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
137         createInstance(transaction, deadTimer, keepAlive, stateful, active, instant);
138         return transaction.commit();
139     }
140
141     private CommitStatus createInstance() throws Exception {
142         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
143         createStateful07SessionProposalInstance(transaction);
144         return transaction.commit();
145     }
146
147     public static ObjectName createStateful07SessionProposalInstance(final ConfigTransactionJMXClient transaction) throws Exception {
148         return createInstance(transaction, (short) 200, (short) 100, true, true, true);
149     }
150
151     private static ObjectName createInstance(final ConfigTransactionJMXClient transaction, final Short deadTimer, final Short keepAlive,
152             final Boolean stateful, final Boolean active, final Boolean instant) throws InstanceAlreadyExistsException {
153         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
154         final Stateful07PCEPSessionProposalFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated,
155                 Stateful07PCEPSessionProposalFactoryModuleMXBean.class);
156         mxBean.setActive(active);
157         mxBean.setDeadTimerValue(deadTimer);
158         mxBean.setInitiated(instant);
159         mxBean.setKeepAliveTimerValue(keepAlive);
160         mxBean.setStateful(stateful);
161         return nameCreated;
162     }
163
164 }