aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLITest.java
blob: a21bd9fb9d3874ec412d7a1c24e771c9e95102b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.dmaap.datarouter.provisioning.utils;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.AbstractHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
public class DRRouteCLITest {

    @Mock
    private AbstractHttpClient httpClient;

    @Mock
    private CloseableHttpResponse httpResponse;

    @Mock
    private HttpEntity httpEntity;

    @Mock
    private StatusLine statusLine;

    private DRRouteCLI drRouteCLI;

    @Before
    public void setUp() throws IllegalAccessException{
        drRouteCLI = mock(DRRouteCLI.class);
        doCallRealMethod().when(drRouteCLI).runCommand(anyObject());
        FieldUtils.writeField(drRouteCLI, "server", "prov.datarouternew.com", true);
    }

    @Test
    public void Given_Add_Egress_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "node.datarouternew.com"}));
    }

    @Test
    public void Given_Add_Network_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com", "172.100.0.1"}));
    }

    @Test
    public void Given_Add_Egress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "user1", "172.100.0.0", "node.datarouternew.com"}));
    }

    @Test
    public void Given_Error_On_Post_Rest_Call_RunCommand_Returns_False() throws Exception{
        mockErrorResponseFromRestCall();
        Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
    }

    @Test
    public void Given_Delete_Ingress_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "ingress", "1", "user1", "172.100.0.0"}));
    }

    @Test
    public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"}));
    }

    @Test
    public void Given_Delete_Network_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
    }

    @Test
    public void Given_Delete_Ingress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{
        mockHttpClientForRestCall();
        Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "ingress", "prov.datarouternew.com", "node.datarouternew.com"}));
    }

    @Test
    public void Given_Error_On_Delete_Rest_Call_RunCommand_Returns_False() throws Exception{
        mockErrorResponseFromRestCall();
        Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"}));
    }

    @Test
    public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{
        mockHttpClientForGetRequest();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
    }

    @Test
    public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{
        mockErrorResponseFromRestCall();
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"}));
    }

    @Test
    public void Given_Width_Arg_Then_RunCommand_Returns_True() {
        Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"}));
    }

    @Test
    public void Given_Usage_Arg_Then_RunCommand_Returns_False() {
        Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"}));
    }

    private void mockHttpClientForRestCall() throws Exception{
        when(httpResponse.getEntity()).thenReturn(httpEntity);
        when(statusLine.getStatusCode()).thenReturn(200);
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(httpClient.execute(anyObject())).thenReturn(httpResponse);
        FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
    }

    private void mockHttpClientForGetRequest() throws Exception{
        mockResponseFromGet();
        when(httpResponse.getEntity()).thenReturn(httpEntity);
        when(statusLine.getStatusCode()).thenReturn(200);
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(httpClient.execute(anyObject())).thenReturn(httpResponse);
        FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
    }

    private void mockResponseFromGet() throws IOException {
        JSONObject response = new JSONObject();
        response.put("ingress", addIngressObject());
        response.put("egress", addEgressObject());
        response.put("routing", addRoutingObject());
        InputStream in = new ByteArrayInputStream(response.toString().getBytes());
        when(httpEntity.getContent()).thenReturn(in);
    }

    private JSONArray addRoutingObject() {
        JSONArray routing = new JSONArray();
        JSONObject route = new JSONObject();
        route.put("from", "prov.datarouternew.com");
        route.put("to", "node.datarouternew.com");
        route.put("via", "172.100.0.1");
        routing.put(route);
        return routing;
    }

    private JSONObject addEgressObject() {
        JSONObject egress = new JSONObject();
        egress.put("1", "node.datarouternew.com");
        egress.put("2", "172.0.0.1");
        return egress;
    }

    private JSONArray addIngressObject() {
        JSONArray ingresses = new JSONArray();
        JSONObject ingress = new JSONObject();
        ingress.put("seq", 21);
        ingress.put("feedid", 1);
        ingress.put("user", "user1");
        ingress.put("subnet", "172.0.0.0");
        JSONArray nodes = new JSONArray();
        nodes.put("node.datarouternew.com");
        nodes.put("172.0.0.1");
        ingress.put("node", nodes);
        ingresses.put(ingress);
        return ingresses;
    }

    private void mockErrorResponseFromRestCall() throws Exception{
        InputStream in = new ByteArrayInputStream("<pre> Server Not Found </pre>".getBytes());
        when(httpEntity.getContent()).thenReturn(in);
        when(httpResponse.getEntity()).thenReturn(httpEntity);
        when(statusLine.getStatusCode()).thenReturn(400);
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(httpClient.execute(anyObject())).thenReturn(httpResponse);
        FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true);
    }
}