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