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()
86 " \"properties\": { \"appStarter\": \"value1\",\n" +
87 " \"dashboard\": {\"key1:\" : \"value2\"}\n" +
90 Preferences actualResponse = requestSpecification()
92 .accept(MediaType.APPLICATION_JSON_VALUE)
93 .contentType(ContentType.JSON)
94 .header(new Header("X-Request-Id", X_REQUEST_ID))
95 .body(expectedResponse)
97 .post("/v1/preferences")
99 .statusCode(HttpStatus.OK.value())
102 .as(Preferences.class);
104 assertThat(actualResponse).isNotNull();
105 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
109 void thatUserPreferencesCanBeUpdated() {
110 // First save a user preference before a GET can run
111 Preferences initialPreferences = new Preferences()
113 " \"properties\": { \"appStarter\": \"value1\",\n" +
114 " \"dashboard\": {\"key1:\" : \"value2\"}\n" +
118 .savePreferences(X_REQUEST_ID,"test-user", initialPreferences)
121 Preferences expectedResponse = new Preferences()
123 " \"properties\": { \"appStarter\": \"value3\",\n" +
124 " \"dashboard\": {\"key2:\" : \"value4\"}\n" +
127 Preferences actualResponse = requestSpecification("test-user")
129 .accept(MediaType.APPLICATION_JSON_VALUE)
130 .contentType(ContentType.JSON)
131 .header(new Header("X-Request-Id", X_REQUEST_ID))
132 .body(expectedResponse)
134 .put("/v1/preferences")
136 .statusCode(HttpStatus.OK.value())
139 .as(Preferences.class);
141 assertThat(actualResponse).isNotNull();
142 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
146 void thatUserPreferencesCanNotBeFound() {
148 Preferences actualResponse = requestSpecification("test-canNotBeFound")
150 .accept(MediaType.APPLICATION_JSON_VALUE)
151 .header(new Header("X-Request-Id", X_REQUEST_ID))
153 .get("/v1/preferences")
155 .statusCode(HttpStatus.OK.value())
158 .as(Preferences.class);
160 assertThat(actualResponse).isNotNull();
161 assertThat(actualResponse.getProperties()).isNull();
165 void thatUserPreferencesHasXRequestIdHeader() {
167 String actualResponse = requestSpecification("test-user")
169 .accept(MediaType.APPLICATION_JSON_VALUE)
170 .header(new Header("X-Request-Id", X_REQUEST_ID))
172 .get("/v1/preferences")
174 .statusCode(HttpStatus.OK.value())
176 .header("X-Request-Id");
178 assertThat(actualResponse).isNotNull().isEqualTo(X_REQUEST_ID);
182 void thatUserPreferencesHasNoXRequestIdHeader() {
184 requestSpecification("test-user")
186 .accept(MediaType.APPLICATION_JSON_VALUE)
188 .get("/v1/preferences")
190 .statusCode(HttpStatus.BAD_REQUEST.value());