Java 11 update
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / util / client / NoAuthRestClientTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.util.client;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import javax.ws.rs.core.MediaType;
27
28 import org.apache.camel.CamelContext;
29 import org.apache.camel.Exchange;
30 import org.apache.camel.impl.DefaultCamelContext;
31 import org.apache.camel.support.DefaultExchange;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.datarouter.util.client.NoAuthRestClient;
36 import org.onap.aai.restclient.client.OperationResult;
37 import org.onap.aai.restclient.client.RestClient;
38
39 public class NoAuthRestClientTest {
40
41   RestClient client = null;
42   OperationResult successResult = null;
43   OperationResult failureResult = null;
44   Exchange exchange = null;
45   NoAuthRestClient narc = new NoAuthRestClient(60,60);
46   String goodDomain = "AGoodUrlThatNeverFails.com";
47   String badDomain = "ABadUrlThatAlwaysFails.com";
48   String goodTargetUrl = "http://" + goodDomain + ":1234/servicegraph";
49   String badTargetUrl = "http://" + badDomain + ":1234/servicegraph";
50   String payload = "{\"origin-uri\":\"/routerService/servicegraph\","
51       + "\"origin-payload\":{\"hashId\":\"claymore-sdwan-service.full.(View and Inspect)\"}}";
52   
53   String successResponsePayload = "very-good-result";
54   String failureResponsePayload = "Server Error";
55   
56   @SuppressWarnings("unchecked")
57   @Before
58   public void init(){
59     client = Mockito.mock(RestClient.class);
60     successResult = new OperationResult(200, successResponsePayload);
61     failureResult = new OperationResult(500, failureResponsePayload);
62     failureResult.setFailureCause(failureResponsePayload);
63     Mockito.when(client.post(Mockito.eq(goodTargetUrl), Mockito.anyString(), Mockito.anyMap(), 
64         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
65     .thenReturn(successResult);
66     Mockito.when(client.post(Mockito.eq(badTargetUrl), Mockito.anyString(), Mockito.anyMap(), 
67         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
68     .thenReturn(failureResult);
69     narc.setRestClient(client);
70
71   }
72   
73   public Exchange getExchange(){
74     CamelContext ctx = new DefaultCamelContext(); 
75     Exchange ex = new DefaultExchange(ctx);
76     ex.getIn().setHeader(Exchange.HTTP_URL, "http://ARandomOrigin.com");
77     ex.getIn().setBody(payload);
78     return ex;
79   }
80   
81   @Test
82   public void testHandleRequest_successScenario() {
83     Exchange ex = getExchange();
84     try {
85       narc.handleRequest(goodDomain, "1234", ex);
86       String outBody = ex.getOut().getBody(String.class);
87       assertEquals("Routing success scenario: Failure to get correct http status.", 
88           ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE), 200 );
89       assertEquals("Routing success scenario: Failure to get response body.", 
90           outBody, successResponsePayload);
91     } catch (Exception e) {
92       fail("Routing success scenario: Failure to process.");
93     }
94   }
95   
96   @Test
97   public void testHandleRequest_failureScenario() {
98     Exchange ex = getExchange();
99     try {
100       narc.handleRequest(badDomain, "1234", ex);
101       String outBody = ex.getOut().getBody(String.class);
102       assertEquals("Routing failure scenario: Failure to get correct http status.", 
103           ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE), 500 );
104       assertEquals("Routing failure scenario: Failure to get response body.", 
105           outBody, failureResult.getFailureCause());
106     } catch (Exception e) {
107       fail("Routing failure scenario: Failure to process.");
108     }
109   }
110
111 }