[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / DRRouteCLITest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning.utils;
24
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;
39
40 import java.io.ByteArrayInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43
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;
48
49 @RunWith(PowerMockRunner.class)
50 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
51 public class DRRouteCLITest {
52
53     @Mock
54     private AbstractHttpClient httpClient;
55
56     @Mock
57     private CloseableHttpResponse httpResponse;
58
59     @Mock
60     private HttpEntity httpEntity;
61
62     @Mock
63     private StatusLine statusLine;
64
65     private DRRouteCLI drRouteCLI;
66
67     @Before
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);
72     }
73
74     @Test
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"}));
78     }
79
80     @Test
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"}));
84     }
85
86     @Test
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"}));
90     }
91
92     @Test
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"}));
96     }
97
98     @Test
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"}));
102     }
103
104     @Test
105     public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{
106         mockHttpClientForRestCall();
107         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"}));
108     }
109
110     @Test
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"}));
114     }
115
116     @Test
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"}));
120     }
121
122     @Test
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"}));
126     }
127
128     @Test
129     public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{
130         mockHttpClientForGetRequest();
131         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
132     }
133
134     @Test
135     public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{
136         mockErrorResponseFromRestCall();
137         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
138     }
139
140     @Test
141     public void Given_Width_Arg_Then_RunCommand_Returns_True() {
142         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"}));
143     }
144
145     @Test
146     public void Given_Usage_Arg_Then_RunCommand_Returns_False() {
147         Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"}));
148     }
149
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);
156     }
157
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);
165     }
166
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);
174     }
175
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");
182         routing.put(route);
183         return routing;
184     }
185
186     private JSONObject addEgressObject() {
187         JSONObject egress = new JSONObject();
188         egress.put("1", "node.datarouternew.com");
189         egress.put("2", "172.0.0.1");
190         return egress;
191     }
192
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);
205         return ingresses;
206     }
207
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);
216     }
217 }