Cleanup root pom "name".
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / NetconfOperationServiceImplTest.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
17 import java.net.URI;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.Map;
22 import java.util.Set;
23 import org.hamcrest.CoreMatchers;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.opendaylight.controller.config.api.LookupRegistry;
27 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
28 import org.opendaylight.yangtools.yang.common.QName;
29
30 public class NetconfOperationServiceImplTest {
31
32     private static final Date date1970_01_01;
33
34     static {
35         try {
36             date1970_01_01 = new SimpleDateFormat("yyyy-MM-dd").parse("1970-01-01");
37         } catch (ParseException e) {
38             throw new IllegalStateException(e);
39         }
40     }
41
42     @Test
43     public void testCheckConsistencyBetweenYangStoreAndConfig_ok() throws Exception {
44         NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
45                 mockJmxClient("qname1", "qname2"),
46                 mockYangStoreSnapshot("qname2", "qname1"));
47     }
48
49     @Test
50     public void testCheckConsistencyBetweenYangStoreAndConfig_ok2() throws Exception {
51         NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
52                 mockJmxClient("qname1", "qname2", "qname4", "qname5"),
53                 mockYangStoreSnapshot("qname2", "qname1"));
54     }
55
56     @Test
57     public void testCheckConsistencyBetweenYangStoreAndConfig_ok3() throws Exception {
58         NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
59                 mockJmxClient(),
60                 mockYangStoreSnapshot());
61     }
62
63     @Test
64     public void testCheckConsistencyBetweenYangStoreAndConfig_yangStoreMore() throws Exception {
65         try {
66             NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(mockJmxClient("qname1"),
67                     mockYangStoreSnapshot("qname2", "qname1"));
68             fail("An exception of type " + IllegalStateException.class + " was expected");
69         } catch (IllegalStateException e) {
70             String message = e.getMessage();
71             Assert.assertThat(
72                     message,
73                     CoreMatchers
74                     .containsString("missing from config subsystem but present in yangstore: [(namespace?revision=1970-01-01)qname2]"));
75             Assert.assertThat(
76                     message,
77                     CoreMatchers
78                     .containsString("All modules present in config: [(namespace?revision=1970-01-01)qname1]"));
79         }
80     }
81
82     private YangStoreSnapshot mockYangStoreSnapshot(final String... qnames) {
83         YangStoreSnapshot mock = mock(YangStoreSnapshot.class);
84
85         Map<String, Map<String, ModuleMXBeanEntry>> map = Maps.newHashMap();
86
87         Map<String, ModuleMXBeanEntry> innerMap = Maps.newHashMap();
88
89         int i = 1;
90         for (String qname : qnames) {
91             innerMap.put(Integer.toString(i++), mockMBeanEntry(qname));
92         }
93
94         map.put("1", innerMap);
95
96         doReturn(map).when(mock).getModuleMXBeanEntryMap();
97
98         return mock;
99     }
100
101     private ModuleMXBeanEntry mockMBeanEntry(final String qname) {
102         ModuleMXBeanEntry mock = mock(ModuleMXBeanEntry.class);
103         QName q = getQName(qname);
104         doReturn(q).when(mock).getYangModuleQName();
105         return mock;
106     }
107
108     private QName getQName(final String qname) {
109         return QName.create(URI.create("namespace"), date1970_01_01, qname);
110     }
111
112     private LookupRegistry mockJmxClient(final String... visibleQNames) {
113         LookupRegistry mock = mock(LookupRegistry.class);
114         Set<String> qnames = Sets.newHashSet();
115         for (String visibleQName : visibleQNames) {
116             QName q = getQName(visibleQName);
117             qnames.add(q.toString());
118         }
119         doReturn(qnames).when(mock).getAvailableModuleFactoryQNames();
120         return mock;
121     }
122 }