Removed checkstyle warnings.
[bgpcep.git] / bgp / update-mock-config / src / test / java / org / opendaylight / controller / config / yang / bgp / mock / BgpMockModuleTest.java
1 /*
2  * Copyright (c) 2013 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.bgp.mock;
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.threadpool.impl.EventBusModuleFactory;
24 import org.opendaylight.controller.config.yang.threadpool.impl.EventBusModuleMXBean;
25
26 public class BgpMockModuleTest extends AbstractConfigTest {
27
28     private final String instanceName = "bgp-mock";
29
30     private BgpMockModuleFactory factory;
31
32     private EventBusModuleFactory eventBusFactory;
33
34     @Before
35     public void setUp() throws Exception {
36         this.factory = new BgpMockModuleFactory();
37         this.eventBusFactory = new EventBusModuleFactory();
38         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.factory, this.eventBusFactory));
39     }
40
41     @Test
42     public void testValidationExceptionBothAttributesSet() throws InstanceAlreadyExistsException {
43         try {
44             ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
45             createInstance(transaction, this.factory.getImplementationName(), instanceName, new byte[] { 1 }, "hexMsg",
46                     this.eventBusFactory.getImplementationName());
47             transaction.validateConfig();
48             fail();
49         } catch (ValidationException e) {
50             assertTrue(e.getMessage().contains("Both 'HexDump' and 'BinDump' contain value"));
51         }
52     }
53
54     @Test
55     public void testCreateBean() throws Exception {
56         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57         createInstance(transaction, this.factory.getImplementationName(), instanceName, null, "hexString",
58                 this.eventBusFactory.getImplementationName());
59         transaction.validateConfig();
60         CommitStatus status = transaction.commit();
61         assertBeanCount(1, factory.getImplementationName());
62         assertStatus(status, 2, 0, 0);
63     }
64
65     public static ObjectName createInstance(final ConfigTransactionJMXClient transaction, final String moduleName,
66             final String instanceName, final byte[] binDump, final String hexDump, final String eventBusImplName)
67             throws InstanceAlreadyExistsException {
68         ObjectName nameCreated = transaction.createModule(moduleName, instanceName);
69         BgpMockModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated, BgpMockModuleMXBean.class);
70         mxBean.setBinDump(binDump);
71         mxBean.setHexDump(hexDump);
72         mxBean.setEventBus(createEventBus(transaction, eventBusImplName, "event-bus1"));
73         return nameCreated;
74     }
75
76     public static ObjectName createEventBus(final ConfigTransactionJMXClient transaction, final String moduleName, final String instanceName)
77             throws InstanceAlreadyExistsException {
78         ObjectName nameCreated = transaction.createModule(moduleName, instanceName);
79         transaction.newMBeanProxy(nameCreated, EventBusModuleMXBean.class);
80         return nameCreated;
81     }
82
83 }