1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning.utils;
25 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.apache.http.HttpEntity;
27 import org.apache.http.StatusLine;
28 import org.apache.http.client.methods.CloseableHttpResponse;
29 import org.apache.http.impl.client.AbstractHttpClient;
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.powermock.modules.junit4.PowerMockRunner;
39 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
43 import static org.mockito.Matchers.anyObject;
44 import static org.mockito.Mockito.doCallRealMethod;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
48 @RunWith(PowerMockRunner.class)
49 public class DRRouteCLITest {
52 private AbstractHttpClient httpClient;
55 private CloseableHttpResponse httpResponse;
58 private HttpEntity httpEntity;
61 private StatusLine statusLine;
63 private DRRouteCLI drRouteCLI;
66 public void setUp() throws IllegalAccessException{
67 drRouteCLI = mock(DRRouteCLI.class);
68 doCallRealMethod().when(drRouteCLI).runCommand(anyObject());
69 FieldUtils.writeField(drRouteCLI, "server", "prov.datarouternew.com", true);
73 public void Given_Add_Egress_Then_RunCommand_Returns_True() throws Exception{
74 mockHttpClientForRestCall();
75 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "node.datarouternew.com"}));
79 public void Given_Add_Network_Then_RunCommand_Returns_True() throws Exception{
80 mockHttpClientForRestCall();
81 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com", "172.100.0.1"}));
85 public void Given_Add_Egress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
86 mockHttpClientForRestCall();
87 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "user1", "172.100.0.0", "node.datarouternew.com"}));
91 public void Given_Error_On_Post_Rest_Call_RunCommand_Returns_False() throws Exception{
92 mockErrorResponseFromRestCall();
93 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
97 public void Given_Delete_Ingress_Then_RunCommand_Returns_True() throws Exception{
98 mockHttpClientForRestCall();
99 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "ingress", "1", "user1", "172.100.0.0"}));
103 public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{
104 mockHttpClientForRestCall();
105 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"}));
109 public void Given_Delete_Network_Then_RunCommand_Returns_True() throws Exception{
110 mockHttpClientForRestCall();
111 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
115 public void Given_Delete_Ingress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
116 mockHttpClientForRestCall();
117 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "ingress", "prov.datarouternew.com", "node.datarouternew.com"}));
121 public void Given_Error_On_Delete_Rest_Call_RunCommand_Returns_False() throws Exception{
122 mockErrorResponseFromRestCall();
123 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
127 public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{
128 mockHttpClientForGetRequest();
129 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
133 public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{
134 mockErrorResponseFromRestCall();
135 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
139 public void Given_Width_Arg_Then_RunCommand_Returns_True() {
140 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"}));
144 public void Given_Usage_Arg_Then_RunCommand_Returns_False() {
145 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"}));
148 private void mockHttpClientForRestCall() throws Exception{
149 when(httpResponse.getEntity()).thenReturn(httpEntity);
150 when(statusLine.getStatusCode()).thenReturn(200);
151 when(httpResponse.getStatusLine()).thenReturn(statusLine);
152 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
153 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
156 private void mockHttpClientForGetRequest() throws Exception{
157 mockResponseFromGet();
158 when(httpResponse.getEntity()).thenReturn(httpEntity);
159 when(statusLine.getStatusCode()).thenReturn(200);
160 when(httpResponse.getStatusLine()).thenReturn(statusLine);
161 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
162 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
165 private void mockResponseFromGet() throws IOException {
166 JSONObject response = new JSONObject();
167 response.put("ingress", addIngressObject());
168 response.put("egress", addEgressObject());
169 response.put("routing", addRoutingObject());
170 InputStream in = new ByteArrayInputStream(response.toString().getBytes());
171 when(httpEntity.getContent()).thenReturn(in);
174 private JSONArray addRoutingObject() {
175 JSONArray routing = new JSONArray();
176 JSONObject route = new JSONObject();
177 route.put("from", "prov.datarouternew.com");
178 route.put("to", "node.datarouternew.com");
179 route.put("via", "172.100.0.1");
184 private JSONObject addEgressObject() {
185 JSONObject egress = new JSONObject();
186 egress.put("1", "node.datarouternew.com");
187 egress.put("2", "172.0.0.1");
191 private JSONArray addIngressObject() {
192 JSONArray ingresses = new JSONArray();
193 JSONObject ingress = new JSONObject();
194 ingress.put("seq", 21);
195 ingress.put("feedid", 1);
196 ingress.put("user", "user1");
197 ingress.put("subnet", "172.0.0.0");
198 JSONArray nodes = new JSONArray();
199 nodes.put("node.datarouternew.com");
200 nodes.put("172.0.0.1");
201 ingress.put("node", nodes);
202 ingresses.put(ingress);
206 private void mockErrorResponseFromRestCall() throws Exception{
207 InputStream in = new ByteArrayInputStream("<pre> Server Not Found </pre>".getBytes());
208 when(httpEntity.getContent()).thenReturn(in);
209 when(httpResponse.getEntity()).thenReturn(httpEntity);
210 when(statusLine.getStatusCode()).thenReturn(400);
211 when(httpResponse.getStatusLine()).thenReturn(statusLine);
212 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
213 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);