97702fa907d4fd3500c9cf999a8c1a8e418ae73f
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / util / EchoResponseTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest.util;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.ArgumentMatchers.anyObject;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33 import java.util.UUID;
34
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.MultivaluedHashMap;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriInfo;
41
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class EchoResponseTest {
49
50     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
51
52     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
53
54     static {
55         VALID_HTTP_STATUS_CODES.add(200);
56         VALID_HTTP_STATUS_CODES.add(201);
57         VALID_HTTP_STATUS_CODES.add(204);
58     }
59
60     private EchoResponse echoResponse;
61
62     private HttpHeaders httpHeaders;
63
64     private UriInfo uriInfo;
65
66     private MultivaluedMap<String, String> headersMultiMap;
67     private MultivaluedMap<String, String> queryParameters;
68
69     private List<String> aaiRequestContextList;
70
71     private List<MediaType> outputMediaTypes;
72
73     private static final Logger logger = LoggerFactory.getLogger(EchoResponseTest.class.getName());
74
75     @Before
76     public void setup() {
77         logger.info("Starting the setup for the integration tests of Rest Endpoints");
78
79         echoResponse = new EchoResponse();
80         httpHeaders = mock(HttpHeaders.class);
81         uriInfo = mock(UriInfo.class);
82
83         headersMultiMap = new MultivaluedHashMap<>();
84         queryParameters = Mockito.spy(new MultivaluedHashMap<>());
85
86         headersMultiMap.add("X-FromAppId", "JUNIT");
87         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
88         headersMultiMap.add("Real-Time", "true");
89         headersMultiMap.add("Accept", "application/json");
90         headersMultiMap.add("aai-request-context", "");
91
92         outputMediaTypes = new ArrayList<>();
93         outputMediaTypes.add(APPLICATION_JSON);
94
95         aaiRequestContextList = new ArrayList<>();
96         aaiRequestContextList.add("");
97
98         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
99         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
100         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
101         when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
102
103         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
104
105         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
106         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
107
108         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very
109         // unreasonable
110         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
111
112         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
113     }
114
115     @Test
116     public void testEchoResultWhenValidHeaders() throws Exception {
117
118         Response response = echoResponse.echoResult(httpHeaders, null, "");
119
120         assertNotNull(response);
121         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
122     }
123
124     @Test
125     public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception {
126
127         httpHeaders = mock(HttpHeaders.class);
128         Response response = echoResponse.echoResult(httpHeaders, null, "");
129
130         assertNotNull(response);
131         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
132     }
133
134     @Test
135     public void testEchoResultWhenValidHeadersButMediaTypeWrong() throws Exception {
136
137         when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException())
138             .thenReturn(outputMediaTypes);
139
140         Response response = echoResponse.echoResult(httpHeaders, null, "");
141
142         assertNotNull(response);
143         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
144     }
145 }