2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
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;
30 public class NetconfOperationServiceImplTest {
32 private static final Date date1970_01_01;
36 date1970_01_01 = new SimpleDateFormat("yyyy-MM-dd").parse("1970-01-01");
37 } catch (ParseException e) {
38 throw new IllegalStateException(e);
43 public void testCheckConsistencyBetweenYangStoreAndConfig_ok() throws Exception {
44 NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
45 mockJmxClient("qname1", "qname2"),
46 mockYangStoreSnapshot("qname2", "qname1"));
50 public void testCheckConsistencyBetweenYangStoreAndConfig_ok2() throws Exception {
51 NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
52 mockJmxClient("qname1", "qname2", "qname4", "qname5"),
53 mockYangStoreSnapshot("qname2", "qname1"));
57 public void testCheckConsistencyBetweenYangStoreAndConfig_ok3() throws Exception {
58 NetconfOperationServiceImpl.checkConsistencyBetweenYangStoreAndConfig(
60 mockYangStoreSnapshot());
64 public void testCheckConsistencyBetweenYangStoreAndConfig_yangStoreMore() throws Exception {
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();
74 .containsString("missing from config subsystem but present in yangstore: [(namespace?revision=1970-01-01)qname2]"));
78 .containsString("All modules present in config: [(namespace?revision=1970-01-01)qname1]"));
82 private YangStoreSnapshot mockYangStoreSnapshot(final String... qnames) {
83 YangStoreSnapshot mock = mock(YangStoreSnapshot.class);
85 Map<String, Map<String, ModuleMXBeanEntry>> map = Maps.newHashMap();
87 Map<String, ModuleMXBeanEntry> innerMap = Maps.newHashMap();
90 for (String qname : qnames) {
91 innerMap.put(Integer.toString(i++), mockMBeanEntry(qname));
94 map.put("1", innerMap);
96 doReturn(map).when(mock).getModuleMXBeanEntryMap();
101 private ModuleMXBeanEntry mockMBeanEntry(final String qname) {
102 ModuleMXBeanEntry mock = mock(ModuleMXBeanEntry.class);
103 QName q = getQName(qname);
104 doReturn(q).when(mock).getYangModuleQName();
108 private QName getQName(final String qname) {
109 return QName.create(URI.create("namespace"), date1970_01_01, qname);
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());
119 doReturn(qnames).when(mock).getAvailableModuleFactoryQNames();