213ca2709377ca603cbbb649ad3079c7af4a450d
[portal-ng/preferences.git] /
1 /*
2  *
3  * Copyright (c) 2022. Deutsche Telekom AG
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  *
19  *
20  */
21
22 package org.onap.portalng.preferences.preferences;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import org.junit.jupiter.api.Test;
27 import org.onap.portalng.preferences.BaseIntegrationTest;
28 import org.onap.portalng.preferences.openapi.model.Preferences;
29 import org.onap.portalng.preferences.services.PreferencesService;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.MediaType;
33
34 import io.restassured.http.ContentType;
35 import io.restassured.http.Header;
36
37 class PreferencesControllerIntegrationTest extends BaseIntegrationTest {
38
39     protected static final String X_REQUEST_ID = "addf6005-3075-4c80-b7bc-2c70b7d42b57";
40
41     @Autowired
42     PreferencesService preferencesService;
43
44     @Test
45     void thatUserPreferencesCanBeRetrieved() {
46         // First save a user preference before a GET can run
47         Preferences expectedResponse = new Preferences()
48             .properties("{\"properties\": {\"dashboard\": {\"key1:\": \"value2\"}, \"appStarter\": \"value1\"}}");
49         preferencesService
50             .savePreferences(X_REQUEST_ID,"test-user", expectedResponse)
51             .block();
52
53         Preferences actualResponse = requestSpecification("test-user")
54             .given()
55             .accept(MediaType.APPLICATION_JSON_VALUE)
56             .header(new Header("X-Request-Id", X_REQUEST_ID))
57             .when()
58             .get("/v1/preferences")
59             .then()
60             .statusCode(HttpStatus.OK.value())
61             .extract()
62             .body()
63             .as(Preferences.class);
64
65         assertThat(actualResponse).isNotNull();
66         assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
67     }
68
69     @Test
70     void thatUserPreferencesCanNotBeRetrieved() {
71         unauthenticatedRequestSpecification()
72             .given()
73             .accept(MediaType.APPLICATION_JSON_VALUE)
74             .contentType(ContentType.JSON)
75             .header(new Header("X-Request-Id", X_REQUEST_ID))
76             .when()
77             .get("/v1/preferences")
78             .then()
79             .statusCode(HttpStatus.UNAUTHORIZED.value());
80     }
81
82     @Test
83     void thatUserPreferencesCanBeSaved() {
84         Preferences expectedResponse = new Preferences()
85             .properties("""
86                 {
87                     "properties": { "appStarter": "value1",
88                     "dashboard": {"key1:" : "value2"}
89                     }\s
90                 }\
91                 """);
92         Preferences actualResponse = requestSpecification()
93             .given()
94             .accept(MediaType.APPLICATION_JSON_VALUE)
95             .contentType(ContentType.JSON)
96             .header(new Header("X-Request-Id", X_REQUEST_ID))
97             .body(expectedResponse)
98             .when()
99             .post("/v1/preferences")
100             .then()
101             .statusCode(HttpStatus.OK.value())
102             .extract()
103             .body()
104             .as(Preferences.class);
105
106         assertThat(actualResponse).isNotNull();
107         assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
108     }
109
110     @Test
111     void thatUserPreferencesCanBeUpdated() {
112         // First save a user preference before a GET can run
113         Preferences initialPreferences = new Preferences()
114             .properties("""
115                 {
116                     "properties": { "appStarter": "value1",
117                     "dashboard": {"key1:" : "value2"}
118                     }\s
119                 }\
120                 """);
121         preferencesService
122             .savePreferences(X_REQUEST_ID,"test-user", initialPreferences)
123             .block();
124
125         Preferences expectedResponse = new Preferences()
126             .properties("""
127                 {
128                     "properties": { "appStarter": "value3",
129                     "dashboard": {"key2:" : "value4"}
130                     }\s
131                 }\
132                 """);
133         Preferences actualResponse = requestSpecification("test-user")
134             .given()
135             .accept(MediaType.APPLICATION_JSON_VALUE)
136             .contentType(ContentType.JSON)
137             .header(new Header("X-Request-Id", X_REQUEST_ID))
138             .body(expectedResponse)
139             .when()
140             .put("/v1/preferences")
141             .then()
142             .statusCode(HttpStatus.OK.value())
143             .extract()
144             .body()
145             .as(Preferences.class);
146
147         assertThat(actualResponse).isNotNull();
148         assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
149     }
150
151     @Test
152     void thatUserPreferencesCanNotBeFound() {
153
154         Preferences actualResponse = requestSpecification("test-canNotBeFound")
155             .given()
156             .accept(MediaType.APPLICATION_JSON_VALUE)
157             .header(new Header("X-Request-Id", X_REQUEST_ID))
158             .when()
159             .get("/v1/preferences")
160             .then()
161             .statusCode(HttpStatus.OK.value())
162             .extract()
163             .body()
164             .as(Preferences.class);
165
166         assertThat(actualResponse).isNotNull();
167         assertThat(actualResponse.getProperties()).isNull();
168     }
169
170     @Test
171     void thatUserPreferencesHasXRequestIdHeader() {
172
173         String actualResponse = requestSpecification("test-user")
174             .given()
175             .accept(MediaType.APPLICATION_JSON_VALUE)
176             .header(new Header("X-Request-Id", X_REQUEST_ID))
177             .when()
178             .get("/v1/preferences")
179             .then()
180             .statusCode(HttpStatus.OK.value())
181             .extract()
182             .header("X-Request-Id");
183
184         assertThat(actualResponse).isNotNull().isEqualTo(X_REQUEST_ID);
185     }
186
187     @Test
188     void thatUserPreferencesHasNoXRequestIdHeader() {
189
190          requestSpecification("test-user")
191             .given()
192             .accept(MediaType.APPLICATION_JSON_VALUE)
193             .when()
194             .get("/v1/preferences")
195             .then()
196             .statusCode(HttpStatus.BAD_REQUEST.value());
197
198
199     }
200 }