xueliang
Committed by Gerrit Code Review

[ONOS-5422] JUNIT Test cases for volt-all as FUJITSU NETCONF

Change-Id: I802f1f10f4e1ee408aa13652c0ba039425fa8e4d
...@@ -53,20 +53,22 @@ public class FujitsuVoltNeConfig extends AbstractHandlerBehaviour ...@@ -53,20 +53,22 @@ public class FujitsuVoltNeConfig extends AbstractHandlerBehaviour
53 log.warn("Not master for {} Use {} to execute command", 53 log.warn("Not master for {} Use {} to execute command",
54 ncDeviceId, 54 ncDeviceId,
55 mastershipService.getMasterFor(ncDeviceId)); 55 mastershipService.getMasterFor(ncDeviceId));
56 - return reply; 56 + return null;
57 } 57 }
58 58
59 try { 59 try {
60 StringBuilder request = new StringBuilder(); 60 StringBuilder request = new StringBuilder();
61 - request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE); 61 + request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
62 - request.append(ANGLE_RIGHT).append(NEW_LINE); 62 + request.append(ANGLE_RIGHT + NEW_LINE);
63 request.append(VOLT_NE_CLOSE); 63 request.append(VOLT_NE_CLOSE);
64 64
65 - reply = controller. 65 + reply = controller
66 - getDevicesMap().get(ncDeviceId).getSession(). 66 + .getDevicesMap()
67 - get(request.toString(), REPORT_ALL); 67 + .get(ncDeviceId)
68 + .getSession()
69 + .get(request.toString(), REPORT_ALL);
68 } catch (IOException e) { 70 } catch (IOException e) {
69 - log.error("Cannot communicate to device {} exception ", ncDeviceId, e); 71 + log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
70 } 72 }
71 return reply; 73 return reply;
72 } 74 }
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.drivers.fujitsu;
18 +
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.junit.Assert.assertTrue;
23 +import static org.junit.Assert.assertNotNull;
24 +import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
25 +
26 +/**
27 + * Unit tests for methods of FujitsuVoltNeConfig.
28 + */
29 +public class FujitsuVoltNeConfigTest {
30 +
31 + private FujitsuNetconfControllerMock controller;
32 + private FujitsuDriverHandlerAdapter driverHandler;
33 + private FujitsuVoltNeConfig voltConfig;
34 +
35 + private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener();
36 +
37 + @Before
38 + public void setUp() throws Exception {
39 + controller = new FujitsuNetconfControllerMock();
40 + driverHandler = controller.setUp(listener);
41 + voltConfig = new FujitsuVoltNeConfig();
42 + voltConfig.setHandler(driverHandler);
43 + }
44 +
45 + /**
46 + * Run to verify handling of valid input for get operation.
47 + */
48 + @Test
49 + public void testGetAll() throws Exception {
50 + String reply = voltConfig.getAll();
51 + assertNotNull("Incorrect response", reply);
52 + }
53 +
54 + /**
55 + * Verifies XML request string by comparing with generated string.
56 + *
57 + * @param request XML string for set operation
58 + * @return true if XML string matches with generated
59 + */
60 + private boolean verifyGetRequest(String request) {
61 + StringBuilder rpc = new StringBuilder();
62 +
63 + rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
64 + rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
65 + rpc.append(TEST_VOLT_NE_CLOSE);
66 +
67 + String testRequest = rpc.toString();
68 + String regex = TEST_WHITESPACES_REGEX;
69 + int index = rpc.indexOf(regex);
70 + while (index >= ZERO) {
71 + testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
72 + request = request.replaceAll(regex, TEST_EMPTY_STRING);
73 + }
74 + boolean result = request.equals(testRequest);
75 + assertTrue("Does not match with generated string", result);
76 + return result;
77 + }
78 +
79 + /**
80 + * Internal listener for device service events.
81 + */
82 + private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
83 + @Override
84 + public boolean verifyEditConfig(String request) {
85 + return false;
86 + }
87 +
88 + @Override
89 + public boolean verifyEditConfig(String target, String mode, String request) {
90 + return false;
91 + }
92 +
93 + @Override
94 + public boolean verifyGet(String filterSchema, String withDefaultsMode) {
95 + assertTrue("Incorrect withDefaultsMode",
96 + withDefaultsMode.equals(TEST_REPORT_ALL));
97 +
98 + filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
99 + assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
100 + filterSchema.contains(TEST_VOLT_NAMESPACE));
101 +
102 + boolean result = verifyGetRequest(filterSchema);
103 + assertTrue("XML verification failure", result);
104 + return true;
105 + }
106 +
107 + @Override
108 + public String buildGetReply() {
109 + return null;
110 + }
111 +
112 + @Override
113 + public boolean verifyWrappedRpc(String request) {
114 + return false;
115 + }
116 +
117 + @Override
118 + public void verifyStartSubscription(String filterSchema) {
119 + }
120 + }
121 +
122 +}