f446a9a91cf4589ce6c3aa7a0a5391fc1027330c
[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("{\n" +
86                 "    \"properties\": { \"appStarter\": \"value1\",\n" +
87                 "    \"dashboard\": {\"key1:\" : \"value2\"}\n" +
88                 "    } \n" +
89                 "}");
90         Preferences actualResponse = requestSpecification()
91             .given()
92             .accept(MediaType.APPLICATION_JSON_VALUE)
93             .contentType(ContentType.JSON)
94             .header(new Header("X-Request-Id", X_REQUEST_ID))
95             .body(expectedResponse)
96             .when()
97             .post("/v1/preferences")
98             .then()
99             .statusCode(HttpStatus.OK.value())
100             .extract()
101             .body()
102             .as(Preferences.class);
103
104         assertThat(actualResponse).isNotNull();
105         assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
106     }
107
108     @Test
109     void thatUserPreferencesCanBeUpdated() {
110         // First save a user preference before a GET can run
111         Preferences initialPreferences = new Preferences()
112             .properties("{\n" +
113                 "    \"properties\": { \"appStarter\": \"value1\",\n" +
114                 "    \"dashboard\": {\"key1:\" : \"value2\"}\n" +
115                 "    } \n" +
116                 "}");
117         preferencesService
118             .savePreferences(X_REQUEST_ID,"test-user", initialPreferences)
119             .block();
120
121         Preferences expectedResponse = new Preferences()
122             .properties("{\n" +
123                 "    \"properties\": { \"appStarter\": \"value3\",\n" +
124                 "    \"dashboard\": {\"key2:\" : \"value4\"}\n" +
125                 "    } \n" +
126                 "}");
127         Preferences actualResponse = requestSpecification("test-user")
128             .given()
129             .accept(MediaType.APPLICATION_JSON_VALUE)
130             .contentType(ContentType.JSON)
131             .header(new Header("X-Request-Id", X_REQUEST_ID))
132             .body(expectedResponse)
133             .when()
134             .put("/v1/preferences")
135             .then()
136             .statusCode(HttpStatus.OK.value())
137             .extract()
138             .body()
139             .as(Preferences.class);
140
141         assertThat(actualResponse).isNotNull();
142         assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
143     }
144
145     @Test
146     void thatUserPreferencesCanNotBeFound() {
147
148         Preferences actualResponse = requestSpecification("test-canNotBeFound")
149             .given()
150             .accept(MediaType.APPLICATION_JSON_VALUE)
151             .header(new Header("X-Request-Id", X_REQUEST_ID))
152             .when()
153             .get("/v1/preferences")
154             .then()
155             .statusCode(HttpStatus.OK.value())
156             .extract()
157             .body()
158             .as(Preferences.class);
159
160         assertThat(actualResponse).isNotNull();
161         assertThat(actualResponse.getProperties()).isNull();
162     }
163
164     @Test
165     void thatUserPreferencesHasXRequestIdHeader() {
166
167         String actualResponse = requestSpecification("test-user")
168             .given()
169             .accept(MediaType.APPLICATION_JSON_VALUE)
170             .header(new Header("X-Request-Id", X_REQUEST_ID))
171             .when()
172             .get("/v1/preferences")
173             .then()
174             .statusCode(HttpStatus.OK.value())
175             .extract()
176             .header("X-Request-Id");
177
178         assertThat(actualResponse).isNotNull().isEqualTo(X_REQUEST_ID);
179     }
180
181     @Test
182     void thatUserPreferencesHasNoXRequestIdHeader() {
183
184          requestSpecification("test-user")
185             .given()
186             .accept(MediaType.APPLICATION_JSON_VALUE)
187             .when()
188             .get("/v1/preferences")
189             .then()
190             .statusCode(HttpStatus.BAD_REQUEST.value());
191
192
193     }
194 }