Increasing test coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / dal / proxy / processor / AaiUiProxyProcessorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * SPARKY (AAI UI service)
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.dal.proxy.processor;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.MediaType;
32
33 import org.apache.camel.Exchange;
34 import org.apache.camel.Message;
35 import org.codehaus.groovy.grails.web.json.JSONObject;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.AdditionalMatchers;
39 import org.mockito.Matchers;
40 import org.mockito.Mockito;
41 import org.onap.aai.restclient.client.OperationResult;
42 import org.onap.aai.restclient.client.RestClient;
43 import org.onap.aai.restclient.enums.RestAuthenticationMode;
44 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
45 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
46 import org.restlet.data.Status;
47
48 public class AaiUiProxyProcessorTest {
49
50   private RestClient client = null;
51   private OperationResult successResult = null;
52   OperationResult failureResult = null;
53   private Exchange mockExchange;
54   private Message mockRequestMessage;
55   private Message mockResponseMessage;
56
57   private HttpServletRequest mockHttpServletRequest;
58   
59   private AaiUiProxyProcessor aaiUiProxyProcessor;
60
61   private String goodBeTargetUrl = "https://0.0.0.0:8000/services/routerService/servicegraph";
62   private String badBeTargetUrl = "https://0.0.0.0:8000/aservicegraph";
63   private String goodDrTargetUrl = "https://0.0.0.0:9502/ui-request/servicegraph";
64
65   String successResponsePayload = "good-payload";
66   String failureResponsePayload = "Server Error";
67
68   @Before
69   public void init()throws RestClientConstructionException {
70     client = Mockito.mock(RestClient.class);
71     mockExchange = Mockito.mock(Exchange.class);
72     mockRequestMessage = Mockito.mock(Message.class);
73     mockResponseMessage = Mockito.mock(Message.class);
74     mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
75
76
77     RestEndpointConfig config = new RestEndpointConfig(); 
78     config.setRestAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
79     aaiUiProxyProcessor = new AaiUiProxyProcessor(config,"ui-request");
80
81     initializeMocks(getProxyRequestJson("someHashValue"));
82     aaiUiProxyProcessor.setClient(client);
83   }
84
85   @Test
86   public void testProxyMessage_successPath() {
87     OperationResult successResultSpy = Mockito.spy(successResult);
88     Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
89         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
90         .thenReturn(successResultSpy);
91
92     Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(goodBeTargetUrl);
93     Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
94     aaiUiProxyProcessor.proxyMessage(mockExchange);
95
96     //Mockito.verify(successResultSpy).getResult();
97     //assertEquals(Status.SUCCESS_OK.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
98   }
99
100   @Test
101   public void testProxyMessage_failurePath() {
102     OperationResult failureResultSpy = Mockito.spy(failureResult);
103     Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
104         Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
105         Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResultSpy);
106
107     Mockito.when(mockExchange.getIn().getHeader(Exchange.HTTP_URI)).thenReturn(badBeTargetUrl);
108     Mockito.when(mockExchange.getIn().getBody(HttpServletRequest.class)).thenReturn(mockHttpServletRequest);
109     aaiUiProxyProcessor.proxyMessage(mockExchange);
110
111     Mockito.verify(failureResultSpy).getFailureCause();
112     assertEquals(Status.SERVER_ERROR_INTERNAL.getCode(), aaiUiProxyProcessor.getOperationResult().getResultCode());
113   }
114
115   private String getProxyRequestJson(String hashId) {
116     JSONObject root = new JSONObject();
117     root.put("hashId", hashId);
118     return root.toString();
119
120   }
121
122   @SuppressWarnings("unchecked")
123   private void initializeMocks(String requestPayload) {
124
125     client = Mockito.mock(RestClient.class);
126     successResult = new OperationResult(200, successResponsePayload);
127     failureResult = new OperationResult(500, failureResponsePayload);
128     failureResult.setFailureCause(failureResponsePayload);
129
130     Mockito.when(client.post(Mockito.eq(goodDrTargetUrl), Mockito.anyString(), Mockito.anyMap(),
131         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
132         .thenReturn(successResult);
133
134     Mockito.when(client.post(AdditionalMatchers.not(Matchers.eq(goodDrTargetUrl)),
135         Mockito.anyString(), Mockito.anyMap(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE),
136         Mockito.eq(MediaType.APPLICATION_JSON_TYPE))).thenReturn(failureResult);
137     
138     Mockito.when(mockHttpServletRequest.getRequestURI()).thenReturn("fakeUri");
139     Mockito.when(mockHttpServletRequest.getLocalPort()).thenReturn(8001);
140
141     Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage);
142     Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage);
143   }
144
145 }
146