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