Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / MsoUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.mso;
23
24 import static org.apache.http.HttpStatus.SC_OK;
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import io.joshworks.restclient.http.HttpResponse;
28 import org.onap.vid.testUtils.TestUtils;
29 import org.testng.annotations.DataProvider;
30 import org.testng.annotations.Test;
31
32 public class MsoUtilTest {
33
34     @Test
35     public void shouldWrapRestObject() {
36         // given
37         String entity = "entity";
38         RestObject<String> restObject = new RestObject<>();
39         restObject.set(entity);
40         restObject.setStatusCode(SC_OK);
41         // when
42         MsoResponseWrapper result = MsoUtil.wrapResponse(restObject);
43         // then
44         assertThat(result.getEntity()).isEqualTo(entity);
45         assertThat(result.getStatus()).isEqualTo(SC_OK);
46     }
47
48     @Test
49     public void shouldWrapHttpResponse() throws Exception {
50         // given
51         HttpResponse<String> httpResponse = TestUtils.createTestHttpResponse(SC_OK, null);
52         // when
53         MsoResponseWrapper result = MsoUtil.wrapResponse(httpResponse);
54         // then
55         assertThat(result.getEntity()).isEqualTo(null);
56         assertThat(result.getStatus()).isEqualTo(SC_OK);
57     }
58
59     @Test
60     public void shouldWrapHttpResponseWithEntity() throws Exception {
61         // given
62         String entity = "entity";
63         HttpResponse<String> httpResponse = TestUtils.createTestHttpResponse(SC_OK, entity);
64         // when
65         MsoResponseWrapper result = MsoUtil.wrapResponse(httpResponse);
66         // then
67         assertThat(result.getEntity()).isEqualTo(entity);
68         assertThat(result.getStatus()).isEqualTo(SC_OK);
69     }
70
71     @DataProvider
72     public static Object[][] formatExceptionAdditionalInfo() {
73         return new Object[][]{
74             {"message", "Http Code:400, message"},
75
76             {null, "Http Code:400"},
77
78             {"{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC0002\",\"text\":\"message\"}}}",
79                 "Http Code:400, \"messageId\":\"SVC0002\",\"text\":\"message\""},
80
81             {"{\"validJson\": \"Error: message\"}", "Http Code:400, {\"validJson\": \"Error: message\"}"},
82
83             {"{\"serviceException\":{\"messageId\":\"SVC0002\",\"text\":\"Error: message\"}}",
84                 "Http Code:400, \"messageId\":\"SVC0002\",\"text\":\"Error: message\""},
85         };
86     }
87
88     @Test(dataProvider = "formatExceptionAdditionalInfo")
89     public void formatExceptionAdditionalInfo_payloadWithError400_doNotReturnNull(String payload, String expected) {
90         assertThat(MsoUtil.formatExceptionAdditionalInfo(400, payload))
91             .isEqualTo(expected);
92     }
93 }