2 * Copyright (c) 2014, 2015 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.netconf.sal.connect.netconf.listener;
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
15 import com.google.common.collect.Lists;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
19 import org.opendaylight.yangtools.yang.common.QName;
21 public class NetconfSessionPreferencesTest {
24 public void testMerge() throws Exception {
25 final List<String> caps1 = Lists.newArrayList(
26 "namespace:1?module=module1&revision=2012-12-12",
27 "namespace:2?module=module2&revision=2012-12-12",
28 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04",
29 "urn:ietf:params:netconf:base:1.0",
30 "urn:ietf:params:netconf:capability:rollback-on-error:1.0"
32 final NetconfSessionPreferences sessionCaps1 = NetconfSessionPreferences.fromStrings(caps1);
33 assertCaps(sessionCaps1, 2, 3);
35 final List<String> caps2 = Lists.newArrayList(
36 "namespace:3?module=module3&revision=2012-12-12",
37 "namespace:4?module=module4&revision=2012-12-12",
40 final NetconfSessionPreferences sessionCaps2 = NetconfSessionPreferences.fromStrings(caps2);
41 assertCaps(sessionCaps2, 1, 2);
43 final NetconfSessionPreferences merged = sessionCaps1.addModuleCaps(sessionCaps2);
44 assertCaps(merged, 2, 2 + 1 /*Preserved monitoring*/ + 2 /*already present*/);
45 for (final QName qName : sessionCaps2.getModuleBasedCaps()) {
46 assertThat(merged.getModuleBasedCaps(), hasItem(qName));
48 assertThat(merged.getModuleBasedCaps(), hasItem(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING));
50 assertThat(merged.getNonModuleCaps(), hasItem("urn:ietf:params:netconf:base:1.0"));
51 assertThat(merged.getNonModuleCaps(), hasItem("urn:ietf:params:netconf:capability:rollback-on-error:1.0"));
55 public void testReplace() throws Exception {
56 final List<String> caps1 = Lists.newArrayList(
57 "namespace:1?module=module1&revision=2012-12-12",
58 "namespace:2?module=module2&revision=2012-12-12",
59 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04",
60 "urn:ietf:params:netconf:base:1.0",
61 "urn:ietf:params:netconf:capability:rollback-on-error:1.0"
63 final NetconfSessionPreferences sessionCaps1 = NetconfSessionPreferences.fromStrings(caps1);
64 assertCaps(sessionCaps1, 2, 3);
66 final List<String> caps2 = Lists.newArrayList(
67 "namespace:3?module=module3&revision=2012-12-12",
68 "namespace:4?module=module4&revision=2012-12-12",
71 final NetconfSessionPreferences sessionCaps2 = NetconfSessionPreferences.fromStrings(caps2);
72 assertCaps(sessionCaps2, 1, 2);
74 final NetconfSessionPreferences replaced = sessionCaps1.replaceModuleCaps(sessionCaps2);
75 assertCaps(replaced, 2, 2);
79 public void testCapabilityNoRevision() throws Exception {
80 final List<String> caps1 = Lists.newArrayList(
81 "namespace:2?module=module2",
82 "namespace:2?module=module2&revision=2012-12-12",
83 "namespace:2?module=module1&RANDOMSTRING;revision=2013-12-12",
84 // Revision parameter present, but no revision defined
85 "namespace:2?module=module4&RANDOMSTRING;revision=",
86 // This one should be ignored(same as first), since revision is in wrong format
87 "namespace:2?module=module2&RANDOMSTRING;revision=2013-12-12"
90 final NetconfSessionPreferences sessionCaps1 = NetconfSessionPreferences.fromStrings(caps1);
91 assertCaps(sessionCaps1, 0, 4);
94 private void assertCaps(final NetconfSessionPreferences sessionCaps1, final int nonModuleCaps, final int moduleCaps) {
95 assertEquals(nonModuleCaps, sessionCaps1.getNonModuleCaps().size());
96 assertEquals(moduleCaps, sessionCaps1.getModuleBasedCaps().size());