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