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()
49 " \"properties\": { \"appStarter\": \"value1\",\n" +
50 " \"dashboard\": {\"key1:\" : \"value2\"}\n" +
54 .savePreferences(X_REQUEST_ID,"test-user", expectedResponse)
57 Preferences actualResponse = requestSpecification("test-user")
59 .accept(MediaType.APPLICATION_JSON_VALUE)
60 .header(new Header("X-Request-Id", X_REQUEST_ID))
62 .get("/v1/preferences")
64 .statusCode(HttpStatus.OK.value())
67 .as(Preferences.class);
69 assertThat(actualResponse).isNotNull();
70 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
74 void thatUserPreferencesCanNotBeRetrieved() {
75 unauthenticatedRequestSpecification()
77 .accept(MediaType.APPLICATION_JSON_VALUE)
78 .contentType(ContentType.JSON)
79 .header(new Header("X-Request-Id", X_REQUEST_ID))
81 .get("/v1/preferences")
83 .statusCode(HttpStatus.UNAUTHORIZED.value());
87 void thatUserPreferencesCanBeSaved() {
88 Preferences expectedResponse = new Preferences()
90 " \"properties\": { \"appStarter\": \"value1\",\n" +
91 " \"dashboard\": {\"key1:\" : \"value2\"}\n" +
94 Preferences actualResponse = requestSpecification()
96 .accept(MediaType.APPLICATION_JSON_VALUE)
97 .contentType(ContentType.JSON)
98 .header(new Header("X-Request-Id", X_REQUEST_ID))
99 .body(expectedResponse)
101 .post("/v1/preferences")
103 .statusCode(HttpStatus.OK.value())
106 .as(Preferences.class);
108 assertThat(actualResponse).isNotNull();
109 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
113 void thatUserPreferencesCanBeUpdated() {
114 // First save a user preference before a GET can run
115 Preferences initialPreferences = new Preferences()
117 " \"properties\": { \"appStarter\": \"value1\",\n" +
118 " \"dashboard\": {\"key1:\" : \"value2\"}\n" +
122 .savePreferences(X_REQUEST_ID,"test-user", initialPreferences)
125 Preferences expectedResponse = new Preferences()
127 " \"properties\": { \"appStarter\": \"value3\",\n" +
128 " \"dashboard\": {\"key2:\" : \"value4\"}\n" +
131 Preferences actualResponse = requestSpecification("test-user")
133 .accept(MediaType.APPLICATION_JSON_VALUE)
134 .contentType(ContentType.JSON)
135 .header(new Header("X-Request-Id", X_REQUEST_ID))
136 .body(expectedResponse)
138 .put("/v1/preferences")
140 .statusCode(HttpStatus.OK.value())
143 .as(Preferences.class);
145 assertThat(actualResponse).isNotNull();
146 assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
150 void thatUserPreferencesCanNotBeFound() {
152 Preferences actualResponse = requestSpecification("test-canNotBeFound")
154 .accept(MediaType.APPLICATION_JSON_VALUE)
155 .header(new Header("X-Request-Id", X_REQUEST_ID))
157 .get("/v1/preferences")
159 .statusCode(HttpStatus.OK.value())
162 .as(Preferences.class);
164 assertThat(actualResponse).isNotNull();
165 assertThat(actualResponse.getProperties()).isEqualTo("");
169 void thatUserPreferencesHasXRequestIdHeader() {
171 String actualResponse = requestSpecification("test-user")
173 .accept(MediaType.APPLICATION_JSON_VALUE)
174 .header(new Header("X-Request-Id", X_REQUEST_ID))
176 .get("/v1/preferences")
178 .statusCode(HttpStatus.OK.value())
180 .header("X-Request-Id");
182 assertThat(actualResponse).isNotNull().isEqualTo(X_REQUEST_ID);
186 void thatUserPreferencesHasNoXRequestIdHeader() {
188 requestSpecification("test-user")
190 .accept(MediaType.APPLICATION_JSON_VALUE)
192 .get("/v1/preferences")
194 .statusCode(HttpStatus.BAD_REQUEST.value());