160981dee842210ea37fa42cec8dc8551f0b9e9e
[aai/data-router.git] / src / test / java / org / openecomp / datarouter / util / client / NoAuthRestClientTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
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
26 package org.openecomp.datarouter.util.client;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.fail;
30
31 import javax.ws.rs.core.MediaType;
32
33 import org.apache.camel.CamelContext;
34 import org.apache.camel.Exchange;
35 import org.apache.camel.impl.DefaultCamelContext;
36 import org.apache.camel.impl.DefaultExchange;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.aai.restclient.client.OperationResult;
41 import org.onap.aai.restclient.client.RestClient;
42
43 public class NoAuthRestClientTest {
44
45   RestClient client = null;
46   OperationResult successResult = null;
47   OperationResult failureResult = null;
48   Exchange exchange = null;
49   NoAuthRestClient narc = new NoAuthRestClient(60,60);
50   String goodDomain = "AGoodUrlThatNeverFails.com";
51   String badDomain = "ABadUrlThatAlwaysFails.com";
52   String goodTargetUrl = "http://" + goodDomain + ":1234/servicegraph";
53   String badTargetUrl = "http://" + badDomain + ":1234/servicegraph";
54   String payload = "{\"origin-uri\":\"/routerService/servicegraph\","
55       + "\"origin-payload\":{\"hashId\":\"claymore-sdwan-service.full.(View and Inspect)\"}}";
56   
57   String successResponsePayload = "very-good-result";
58   String failureResponsePayload = "Server Error";
59   
60   @SuppressWarnings("unchecked")
61   @Before
62   public void init(){
63     client = Mockito.mock(RestClient.class);
64     successResult = new OperationResult(200, successResponsePayload);
65     failureResult = new OperationResult(500, failureResponsePayload);
66     failureResult.setFailureCause(failureResponsePayload);
67     Mockito.when(client.post(Mockito.eq(goodTargetUrl), Mockito.anyString(), Mockito.anyMap(), 
68         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
69     .thenReturn(successResult);
70     Mockito.when(client.post(Mockito.eq(badTargetUrl), Mockito.anyString(), Mockito.anyMap(), 
71         Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(MediaType.APPLICATION_JSON_TYPE)))
72     .thenReturn(failureResult);
73     narc.setRestClient(client);
74
75   }
76   
77   public Exchange getExchange(){
78     CamelContext ctx = new DefaultCamelContext(); 
79     Exchange ex = new DefaultExchange(ctx);
80     ex.getIn().setHeader(Exchange.HTTP_URL, "http://ARandomOrigin.com");
81     ex.getIn().setBody(payload);
82     return ex;
83   }
84   
85   @Test
86   public void testHandleRequest_successScenario() {
87     Exchange ex = getExchange();
88     try {
89       narc.handleRequest(goodDomain, "1234", ex);
90       String outBody = ex.getOut().getBody(String.class);
91       assertEquals("Routing success scenario: Failure to get correct http status.", 
92           ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE), 200 );
93       assertEquals("Routing success scenario: Failure to get response body.", 
94           outBody, successResponsePayload);
95     } catch (Exception e) {
96       fail("Routing success scenario: Failure to process.");
97     }
98   }
99   
100   @Test
101   public void testHandleRequest_failureScenario() {
102     Exchange ex = getExchange();
103     try {
104       narc.handleRequest(badDomain, "1234", ex);
105       String outBody = ex.getOut().getBody(String.class);
106       assertEquals("Routing failure scenario: Failure to get correct http status.", 
107           ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE), 500 );
108       assertEquals("Routing failure scenario: Failure to get response body.", 
109           outBody, failureResult.getFailureCause());
110     } catch (Exception e) {
111       fail("Routing failure scenario: Failure to process.");
112     }
113   }
114
115 }