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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.