1 package org.onap.aai.sparky.dal.proxy.processor;
3 * ============LICENSE_START=======================================================
4 * SPARKY (AAI UI service)
5 * ================================================================================
6 * Copyright © 2017 AT&T Intellectual Property.
7 * Copyright © 2017 Amdocs
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
23 * ECOMP and OpenECOMP are trademarks
24 * and service marks of AT&T Intellectual Property.
27 /*package org.openecomp.sparky.dal.proxy.processor;
29 import static org.junit.Assert.assertEquals;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.MediaType;
34 import org.apache.camel.Exchange;
35 import org.apache.camel.Message;
36 import org.codehaus.groovy.grails.web.json.JSONObject;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.AdditionalMatchers;
40 import org.mockito.Matchers;
41 import org.mockito.Mockito;
42 import org.onap.aai.restclient.client.OperationResult;
43 import org.onap.aai.restclient.client.RestClient;
44 import org.openecomp.sparky.dal.proxy.config.DataRouterConfig;
45 import org.restlet.data.Status;
47 public class AaiUiProxyProcessorTest {
49 private RestClient client = null;
50 private OperationResult successResult = null;
51 OperationResult failureResult = null;
52 private Exchange mockExchange;
53 private Message mockRequestMessage;
54 private Message mockResponseMessage;
56 private HttpServletRequest mockHttpServletRequest;
58 private AaiUiProxyProcessor aaiUiProxyProcessor;
60 private String goodBeTargetUrl = "https://0.0.0.0:8000/services/routerService/servicegraph";
61 private String badBeTargetUrl = "https://0.0.0.0:8000/aservicegraph";
62 private String goodDrTargetUrl = "https://0.0.0.0:9502/ui-request/servicegraph";
64 String successResponsePayload = "good-payload";
65 String failureResponsePayload = "Server Error";
69 client = Mockito.mock(RestClient.class);
70 mockExchange = Mockito.mock(Exchange.class);
71 mockRequestMessage = Mockito.mock(Message.class);
72 mockResponseMessage = Mockito.mock(Message.class);
73 mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
75 DataRouterConfig config = new DataRouterConfig(DataRouterConfigUtil.getTestProperties());
76 aaiUiProxyProcessor = new AaiUiProxyProcessor(config);
78 initializeMocks(getProxyRequestJson("someHashValue"));
79 aaiUiProxyProcessor.setClient(client);
83 public void testProxyMessage_successPath() {
84 OperationResult successResultSpy = Mockito.spy(successResult);
85 Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
86 Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
87 .thenReturn(successResultSpy);
89 Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(goodBeTargetUrl);
90 Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
91 aaiUiProxyProcessor.proxyMessage(mockExchange);
93 Mockito.verify(successResultSpy).getResult();
94 assertEquals(Status.SUCCESS_OK.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
98 public void testProxyMessage_failurePath() {
99 OperationResult failureResultSpy = Mockito.spy(failureResult);
100 Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
101 Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
102 Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResultSpy);
104 Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(badBeTargetUrl);
105 Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
106 aaiUiProxyProcessor.proxyMessage(mockExchange);
108 Mockito.verify(failureResultSpy).getFailureCause();
109 assertEquals(Status.SERVER_ERROR_INTERNAL.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
112 private String getProxyRequestJson(String hashId) {
113 JSONObject root = new JSONObject();
114 root.put("hashId", hashId);
115 return root.toString();
119 @SuppressWarnings("unchecked")
120 private void initializeMocks(String requestPayload) {
122 client = Mockito.mock(RestClient.class);
123 successResult = new OperationResult(200, successResponsePayload);
124 failureResult = new OperationResult(500, failureResponsePayload);
125 failureResult.setFailureCause(failureResponsePayload);
127 Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
128 Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
129 .thenReturn(successResult);
131 Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
132 Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
133 Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResult);
135 Mockito.when(mockHttpServletRequest.getRequestURI()).thenReturn("fakeUri");
136 Mockito.when(mockHttpServletRequest.getLocalPort()).thenReturn(8001);
138 Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage);
139 Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage);