2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.dal.proxy.processor;
27 import static org.junit.Assert.assertEquals;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.ws.rs.core.MediaType;
32 import org.apache.camel.Exchange;
33 import org.apache.camel.Message;
34 import org.json.JSONObject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.AdditionalMatchers;
38 import org.mockito.Matchers;
39 import org.mockito.Mockito;
40 import org.onap.aai.restclient.client.OperationResult;
41 import org.onap.aai.restclient.client.RestClient;
42 import org.onap.aai.restclient.enums.RestAuthenticationMode;
43 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
44 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
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";
68 public void init()throws RestClientConstructionException {
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);
76 RestEndpointConfig config = new RestEndpointConfig();
77 config.setRestAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
78 aaiUiProxyProcessor = new AaiUiProxyProcessor(config,"ui-request");
80 initializeMocks(getProxyRequestJson("someHashValue"));
81 aaiUiProxyProcessor.setClient(client);
85 public void testProxyMessage_successPath() {
86 OperationResult successResultSpy = Mockito.spy(successResult);
87 Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
88 Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
89 .thenReturn(successResultSpy);
91 Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(goodBeTargetUrl);
92 Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
93 aaiUiProxyProcessor.proxyMessage(mockExchange);
95 //Mockito.verify(successResultSpy).getResult();
96 //assertEquals(Status.SUCCESS_OK.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
100 public void testProxyMessage_failurePath() {
101 OperationResult failureResultSpy = Mockito.spy(failureResult);
102 Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
103 Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
104 Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResultSpy);
106 Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(badBeTargetUrl);
107 Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
108 aaiUiProxyProcessor.proxyMessage(mockExchange);
110 Mockito.verify(failureResultSpy).getFailureCause();
111 assertEquals(Status.SERVER_ERROR_INTERNAL.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
114 private String getProxyRequestJson(String hashId) {
115 JSONObject root = new JSONObject();
116 root.put("hashId", hashId);
117 return root.toString();
121 @SuppressWarnings("unchecked")
122 private void initializeMocks(String requestPayload) {
124 client = Mockito.mock(RestClient.class);
125 successResult = new OperationResult(200, successResponsePayload);
126 failureResult = new OperationResult(500, failureResponsePayload);
127 failureResult.setFailureCause(failureResponsePayload);
129 Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
130 Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
131 .thenReturn(successResult);
133 Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
134 Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
135 Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResult);
137 Mockito.when(mockHttpServletRequest.getRequestURI()).thenReturn("fakeUri");
138 Mockito.when(mockHttpServletRequest.getLocalPort()).thenReturn(8001);
140 Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage);
141 Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage);