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