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.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 import java.io.ByteArrayInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
44 import static org.mockito.Matchers.anyObject;
45 import static org.mockito.Mockito.doCallRealMethod;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
49 @RunWith(PowerMockRunner.class)
50 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
51 public class DRRouteCLITest {
54 private AbstractHttpClient httpClient;
57 private CloseableHttpResponse httpResponse;
60 private HttpEntity httpEntity;
63 private StatusLine statusLine;
65 private DRRouteCLI drRouteCLI;
68 public void setUp() throws IllegalAccessException{
69 drRouteCLI = mock(DRRouteCLI.class);
70 doCallRealMethod().when(drRouteCLI).runCommand(anyObject());
71 FieldUtils.writeField(drRouteCLI, "server", "prov.datarouternew.com", true);
75 public void Given_Add_Egress_Then_RunCommand_Returns_True() throws Exception{
76 mockHttpClientForRestCall();
77 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "node.datarouternew.com"}));
81 public void Given_Add_Network_Then_RunCommand_Returns_True() throws Exception{
82 mockHttpClientForRestCall();
83 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com", "172.100.0.1"}));
87 public void Given_Add_Egress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
88 mockHttpClientForRestCall();
89 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "user1", "172.100.0.0", "node.datarouternew.com"}));
93 public void Given_Error_On_Post_Rest_Call_RunCommand_Returns_False() throws Exception{
94 mockErrorResponseFromRestCall();
95 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
99 public void Given_Delete_Ingress_Then_RunCommand_Returns_True() throws Exception{
100 mockHttpClientForRestCall();
101 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "ingress", "1", "user1", "172.100.0.0"}));
105 public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{
106 mockHttpClientForRestCall();
107 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"}));
111 public void Given_Delete_Network_Then_RunCommand_Returns_True() throws Exception{
112 mockHttpClientForRestCall();
113 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
117 public void Given_Delete_Ingress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
118 mockHttpClientForRestCall();
119 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "ingress", "prov.datarouternew.com", "node.datarouternew.com"}));
123 public void Given_Error_On_Delete_Rest_Call_RunCommand_Returns_False() throws Exception{
124 mockErrorResponseFromRestCall();
125 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
129 public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{
130 mockHttpClientForGetRequest();
131 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
135 public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{
136 mockErrorResponseFromRestCall();
137 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
141 public void Given_Width_Arg_Then_RunCommand_Returns_True() {
142 Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"}));
146 public void Given_Usage_Arg_Then_RunCommand_Returns_False() {
147 Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"}));
150 private void mockHttpClientForRestCall() throws Exception{
151 when(httpResponse.getEntity()).thenReturn(httpEntity);
152 when(statusLine.getStatusCode()).thenReturn(200);
153 when(httpResponse.getStatusLine()).thenReturn(statusLine);
154 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
155 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
158 private void mockHttpClientForGetRequest() throws Exception{
159 mockResponseFromGet();
160 when(httpResponse.getEntity()).thenReturn(httpEntity);
161 when(statusLine.getStatusCode()).thenReturn(200);
162 when(httpResponse.getStatusLine()).thenReturn(statusLine);
163 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
164 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
167 private void mockResponseFromGet() throws IOException {
168 JSONObject response = new JSONObject();
169 response.put("ingress", addIngressObject());
170 response.put("egress", addEgressObject());
171 response.put("routing", addRoutingObject());
172 InputStream in = new ByteArrayInputStream(response.toString().getBytes());
173 when(httpEntity.getContent()).thenReturn(in);
176 private JSONArray addRoutingObject() {
177 JSONArray routing = new JSONArray();
178 JSONObject route = new JSONObject();
179 route.put("from", "prov.datarouternew.com");
180 route.put("to", "node.datarouternew.com");
181 route.put("via", "172.100.0.1");
186 private JSONObject addEgressObject() {
187 JSONObject egress = new JSONObject();
188 egress.put("1", "node.datarouternew.com");
189 egress.put("2", "172.0.0.1");
193 private JSONArray addIngressObject() {
194 JSONArray ingresses = new JSONArray();
195 JSONObject ingress = new JSONObject();
196 ingress.put("seq", 21);
197 ingress.put("feedid", 1);
198 ingress.put("user", "user1");
199 ingress.put("subnet", "172.0.0.0");
200 JSONArray nodes = new JSONArray();
201 nodes.put("node.datarouternew.com");
202 nodes.put("172.0.0.1");
203 ingress.put("node", nodes);
204 ingresses.put(ingress);
208 private void mockErrorResponseFromRestCall() throws Exception{
209 InputStream in = new ByteArrayInputStream("<pre> Server Not Found </pre>".getBytes());
210 when(httpEntity.getContent()).thenReturn(in);
211 when(httpResponse.getEntity()).thenReturn(httpEntity);
212 when(statusLine.getStatusCode()).thenReturn(400);
213 when(httpResponse.getStatusLine()).thenReturn(statusLine);
214 when(httpClient.execute(anyObject())).thenReturn(httpResponse);
215 FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);