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