Fix license header violations in sal-netconf-connector
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / listener / NetconfSessionPreferencesTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.sal.connect.netconf.listener;
10
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14
15 import com.google.common.collect.Lists;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
19 import org.opendaylight.yangtools.yang.common.QName;
20
21 public class NetconfSessionPreferencesTest {
22
23     @Test
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&amp;revision=2012-12-12",
28                 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&amp;revision=2010-10-04",
29                 "urn:ietf:params:netconf:base:1.0",
30                 "urn:ietf:params:netconf:capability:rollback-on-error:1.0"
31         );
32         final NetconfSessionPreferences sessionCaps1 = NetconfSessionPreferences.fromStrings(caps1);
33         assertCaps(sessionCaps1, 2, 3);
34
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",
38                 "randomNonModuleCap"
39         );
40         final NetconfSessionPreferences sessionCaps2 = NetconfSessionPreferences.fromStrings(caps2);
41         assertCaps(sessionCaps2, 1, 2);
42
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));
47         }
48         assertThat(merged.getModuleBasedCaps(), hasItem(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING));
49
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"));
52     }
53
54     @Test
55     public void testCapabilityNoRevision() throws Exception {
56         final List<String> caps1 = Lists.newArrayList(
57                 "namespace:2?module=module2",
58                 "namespace:2?module=module2&amp;revision=2012-12-12",
59                 "namespace:2?module=module1&amp;RANDOMSTRING;revision=2013-12-12",
60                 "namespace:2?module=module2&amp;RANDOMSTRING;revision=2013-12-12" // This one should be ignored(same as first), since revision is in wrong format
61         );
62
63         final NetconfSessionPreferences sessionCaps1 = NetconfSessionPreferences.fromStrings(caps1);
64         assertCaps(sessionCaps1, 0, 3);
65     }
66
67     private void assertCaps(final NetconfSessionPreferences sessionCaps1, final int nonModuleCaps, final int moduleCaps) {
68         assertEquals(nonModuleCaps, sessionCaps1.getNonModuleCaps().size());
69         assertEquals(moduleCaps, sessionCaps1.getModuleBasedCaps().size());
70     }
71 }