2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest.util;
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;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashSet;
31 import java.util.List;
33 import java.util.UUID;
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;
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;
48 public class EchoResponseTest {
50 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
52 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
55 VALID_HTTP_STATUS_CODES.add(200);
56 VALID_HTTP_STATUS_CODES.add(201);
57 VALID_HTTP_STATUS_CODES.add(204);
60 private EchoResponse echoResponse;
62 private HttpHeaders httpHeaders;
64 private UriInfo uriInfo;
66 private MultivaluedMap<String, String> headersMultiMap;
67 private MultivaluedMap<String, String> queryParameters;
69 private List<String> aaiRequestContextList;
71 private List<MediaType> outputMediaTypes;
73 private static final Logger logger = LoggerFactory.getLogger(EchoResponseTest.class.getName());
77 logger.info("Starting the setup for the integration tests of Rest Endpoints");
79 echoResponse = new EchoResponse();
80 httpHeaders = mock(HttpHeaders.class);
81 uriInfo = mock(UriInfo.class);
83 headersMultiMap = new MultivaluedHashMap<>();
84 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
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", "");
92 outputMediaTypes = new ArrayList<>();
93 outputMediaTypes.add(APPLICATION_JSON);
95 aaiRequestContextList = new ArrayList<>();
96 aaiRequestContextList.add("");
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"));
103 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
105 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
106 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
108 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very
110 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
112 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
116 public void testEchoResultWhenValidHeaders() throws Exception {
118 Response response = echoResponse.echoResult(httpHeaders, null, "");
120 assertNotNull(response);
121 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
125 public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception {
127 httpHeaders = mock(HttpHeaders.class);
128 Response response = echoResponse.echoResult(httpHeaders, null, "");
130 assertNotNull(response);
131 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
135 public void testEchoResultWhenValidHeadersButMediaTypeWrong() throws Exception {
137 when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException())
138 .thenReturn(outputMediaTypes);
140 Response response = echoResponse.echoResult(httpHeaders, null, "");
142 assertNotNull(response);
143 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());