Update the license for 2017-2018 license
[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 com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27
28 import javax.ws.rs.core.*;
29 import java.util.*;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33 import static org.mockito.Matchers.anyObject;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36
37 public class EchoResponseTest {
38
39     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
40
41     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
42
43     static {
44         VALID_HTTP_STATUS_CODES.add(200);
45         VALID_HTTP_STATUS_CODES.add(201);
46         VALID_HTTP_STATUS_CODES.add(204);
47     }
48
49     private EchoResponse echoResponse;
50
51     private HttpHeaders httpHeaders;
52
53     private UriInfo uriInfo;
54
55     private MultivaluedMap<String, String> headersMultiMap;
56     private MultivaluedMap<String, String> queryParameters;
57
58     private List<String> aaiRequestContextList;
59
60     private List<MediaType> outputMediaTypes;
61
62     private static final EELFLogger logger = EELFManager.getInstance().getLogger(EchoResponseTest.class.getName());
63
64     @Before
65     public void setup(){
66         logger.info("Starting the setup for the integration tests of Rest Endpoints");
67
68         echoResponse  = new EchoResponse();
69         httpHeaders         = mock(HttpHeaders.class);
70         uriInfo             = mock(UriInfo.class);
71
72         headersMultiMap     = new MultivaluedHashMap<>();
73         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
74
75         headersMultiMap.add("X-FromAppId", "JUNIT");
76         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
77         headersMultiMap.add("Real-Time", "true");
78         headersMultiMap.add("Accept", "application/json");
79         headersMultiMap.add("aai-request-context", "");
80
81         outputMediaTypes = new ArrayList<>();
82         outputMediaTypes.add(APPLICATION_JSON);
83
84         aaiRequestContextList = new ArrayList<>();
85         aaiRequestContextList.add("");
86
87         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
88         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
89         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
90         when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
91
92         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
93
94
95         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
96         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
97
98         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
99         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
100
101         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
102     }
103
104     @Test
105     public void testEchoResultWhenValidHeaders() throws Exception {
106
107         Response response = echoResponse.echoResult(httpHeaders, null, "");
108
109         assertNotNull(response);
110         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
111     }
112
113     @Test
114     public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception {
115
116         httpHeaders = mock(HttpHeaders.class);
117         Response response = echoResponse.echoResult(httpHeaders, null, "");
118
119         assertNotNull(response);
120         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
121     }
122
123     @Test
124     public void testEchoResultWhenValidHeadersButMediaTypeWrong() throws Exception {
125
126         when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException())
127         .thenReturn(outputMediaTypes);
128
129         Response response = echoResponse.echoResult(httpHeaders, null, "");
130
131         assertNotNull(response);
132         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
133     }
134 }