3 * Copyright (c) 2022. Deutsche Telekom AG
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
22 package org.onap.portalng.preferences.preferences;
24 import static org.assertj.core.api.Assertions.assertThat;
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;
34 import io.restassured.http.ContentType;
35 import io.restassured.http.Header;
37 class PreferencesControllerIntegrationTest extends BaseIntegrationTest {
39 protected static final String X_REQUEST_ID = "addf6005-3075-4c80-b7bc-2c70b7d42b57";
42 PreferencesService preferencesService;
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\"}}");
50 .savePreferences(X_REQUEST_ID,"test-user", expectedResponse)
53 Preferences actualResponse = requestSpecification("test-user")
55 .accept(MediaType.APPLICATION_JSON_VALUE)
56 .header(new Header("X-Request-Id", X_REQUEST_ID))
58 .get("/v1/preferences")
60 .statusCode(HttpStatus.OK.value())
63 .as(Preferences.class);
65 assertThat(actualResponse).isNotNull();
66 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
70 void thatUserPreferencesCanNotBeRetrieved() {
71 unauthenticatedRequestSpecification()
73 .accept(MediaType.APPLICATION_JSON_VALUE)
74 .contentType(ContentType.JSON)
75 .header(new Header("X-Request-Id", X_REQUEST_ID))
77 .get("/v1/preferences")
79 .statusCode(HttpStatus.UNAUTHORIZED.value());
83 void thatUserPreferencesCanBeSaved() {
84 Preferences expectedResponse = new Preferences()
87 "properties": { "appStarter": "value1",
88 "dashboard": {"key1:" : "value2"}
92 Preferences actualResponse = requestSpecification()
94 .accept(MediaType.APPLICATION_JSON_VALUE)
95 .contentType(ContentType.JSON)
96 .header(new Header("X-Request-Id", X_REQUEST_ID))
97 .body(expectedResponse)
99 .post("/v1/preferences")
101 .statusCode(HttpStatus.OK.value())
104 .as(Preferences.class);
106 assertThat(actualResponse).isNotNull();
107 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
111 void thatUserPreferencesCanBeUpdated() {
112 // First save a user preference before a GET can run
113 Preferences initialPreferences = new Preferences()
116 "properties": { "appStarter": "value1",
117 "dashboard": {"key1:" : "value2"}
122 .savePreferences(X_REQUEST_ID,"test-user", initialPreferences)
125 Preferences expectedResponse = new Preferences()
128 "properties": { "appStarter": "value3",
129 "dashboard": {"key2:" : "value4"}
133 Preferences actualResponse = requestSpecification("test-user")
135 .accept(MediaType.APPLICATION_JSON_VALUE)
136 .contentType(ContentType.JSON)
137 .header(new Header("X-Request-Id", X_REQUEST_ID))
138 .body(expectedResponse)
140 .put("/v1/preferences")
142 .statusCode(HttpStatus.OK.value())
145 .as(Preferences.class);
147 assertThat(actualResponse).isNotNull();
148 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
152 void thatUserPreferencesCanNotBeFound() {
154 Preferences actualResponse = requestSpecification("test-canNotBeFound")
156 .accept(MediaType.APPLICATION_JSON_VALUE)
157 .header(new Header("X-Request-Id", X_REQUEST_ID))
159 .get("/v1/preferences")
161 .statusCode(HttpStatus.OK.value())
164 .as(Preferences.class);
166 assertThat(actualResponse).isNotNull();
167 assertThat(actualResponse.getProperties()).isNull();
171 void thatUserPreferencesHasXRequestIdHeader() {
173 String actualResponse = requestSpecification("test-user")
175 .accept(MediaType.APPLICATION_JSON_VALUE)
176 .header(new Header("X-Request-Id", X_REQUEST_ID))
178 .get("/v1/preferences")
180 .statusCode(HttpStatus.OK.value())
182 .header("X-Request-Id");
184 assertThat(actualResponse).isNotNull().isEqualTo(X_REQUEST_ID);
188 void thatUserPreferencesHasNoXRequestIdHeader() {
190 requestSpecification("test-user")
192 .accept(MediaType.APPLICATION_JSON_VALUE)
194 .get("/v1/preferences")
196 .statusCode(HttpStatus.BAD_REQUEST.value());