dc8d4c798deadec590624a256d8ce083460da37d
[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.modules.junit4.PowerMockRunner;
38
39 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42
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;
47
48 @RunWith(PowerMockRunner.class)
49 public class DRRouteCLITest {
50
51     @Mock
52     private AbstractHttpClient httpClient;
53
54     @Mock
55     private CloseableHttpResponse httpResponse;
56
57     @Mock
58     private HttpEntity httpEntity;
59
60     @Mock
61     private StatusLine statusLine;
62
63     private DRRouteCLI drRouteCLI;
64
65     @Before
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);
70     }
71
72     @Test
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"}));
76     }
77
78     @Test
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"}));
82     }
83
84     @Test
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"}));
88     }
89
90     @Test
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"}));
94     }
95
96     @Test
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"}));
100     }
101
102     @Test
103     public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{
104         mockHttpClientForRestCall();
105         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"}));
106     }
107
108     @Test
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"}));
112     }
113
114     @Test
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"}));
118     }
119
120     @Test
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"}));
124     }
125
126     @Test
127     public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{
128         mockHttpClientForGetRequest();
129         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
130     }
131
132     @Test
133     public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{
134         mockErrorResponseFromRestCall();
135         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
136     }
137
138     @Test
139     public void Given_Width_Arg_Then_RunCommand_Returns_True() {
140         Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"}));
141     }
142
143     @Test
144     public void Given_Usage_Arg_Then_RunCommand_Returns_False() {
145         Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"}));
146     }
147
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);
154     }
155
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);
163     }
164
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);
172     }
173
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");
180         routing.put(route);
181         return routing;
182     }
183
184     private JSONObject addEgressObject() {
185         JSONObject egress = new JSONObject();
186         egress.put("1", "node.datarouternew.com");
187         egress.put("2", "172.0.0.1");
188         return egress;
189     }
190
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);
203         return ingresses;
204     }
205
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);
214     }
215 }