f2bae1ce933460da3863bb482c3b92c250867a97
[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.PreferencesApiDto;
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
36 class PreferencesControllerIntegrationTest extends BaseIntegrationTest {
37
38   @Autowired
39   PreferencesService preferencesService;
40
41   @Test
42   void thatUserPreferencesCanBeRetrieved() {
43     // First save a user preference before a GET can run
44     PreferencesApiDto expectedResponse = new PreferencesApiDto()
45         .properties("{\"properties\": {\"dashboard\": {\"key1:\": \"value2\"}, \"appStarter\": \"value1\"}}");
46     preferencesService
47         .savePreferences("test-user", expectedResponse)
48         .block();
49
50     PreferencesApiDto actualResponse = requestSpecification("test-user")
51         .given()
52         .accept(MediaType.APPLICATION_JSON_VALUE)
53         .when()
54         .get("/v1/preferences")
55         .then()
56         .statusCode(HttpStatus.OK.value())
57         .extract()
58         .body()
59         .as(PreferencesApiDto.class);
60
61     assertThat(actualResponse).isNotNull();
62     assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
63   }
64
65   @Test
66   void thatUserPreferencesCanNotBeRetrieved() {
67     unauthenticatedRequestSpecification()
68         .given()
69         .accept(MediaType.APPLICATION_JSON_VALUE)
70         .contentType(ContentType.JSON)
71         .when()
72         .get("/v1/preferences")
73         .then()
74         .statusCode(HttpStatus.UNAUTHORIZED.value());
75   }
76
77   @Test
78   void thatUserPreferencesCanBeSaved() {
79     PreferencesApiDto expectedResponse = new PreferencesApiDto()
80         .properties("""
81             {
82                 "properties": { "appStarter": "value1",
83                 "dashboard": {"key1:" : "value2"}
84                 }\s
85             }\
86             """);
87     PreferencesApiDto actualResponse = requestSpecification()
88         .given()
89         .accept(MediaType.APPLICATION_JSON_VALUE)
90         .contentType(ContentType.JSON)
91         .body(expectedResponse)
92         .when()
93         .post("/v1/preferences")
94         .then()
95         .statusCode(HttpStatus.OK.value())
96         .extract()
97         .body()
98         .as(PreferencesApiDto.class);
99
100     assertThat(actualResponse).isNotNull();
101     assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
102   }
103
104   @Test
105   void thatUserPreferencesCanBeUpdated() {
106     // First save a user preference before a GET can run
107     PreferencesApiDto initialPreferences = new PreferencesApiDto()
108         .properties("""
109             {
110                 "properties": { "appStarter": "value1",
111                 "dashboard": {"key1:" : "value2"}
112                 }\s
113             }\
114             """);
115     preferencesService
116         .savePreferences("test-user", initialPreferences)
117         .block();
118
119     PreferencesApiDto expectedResponse = new PreferencesApiDto()
120         .properties("""
121             {
122                 "properties": { "appStarter": "value3",
123                 "dashboard": {"key2:" : "value4"}
124                 }\s
125             }\
126             """);
127     PreferencesApiDto actualResponse = requestSpecification("test-user")
128         .given()
129         .accept(MediaType.APPLICATION_JSON_VALUE)
130         .contentType(ContentType.JSON)
131         .body(expectedResponse)
132         .when()
133         .put("/v1/preferences")
134         .then()
135         .statusCode(HttpStatus.OK.value())
136         .extract()
137         .body()
138         .as(PreferencesApiDto.class);
139
140     assertThat(actualResponse).isNotNull();
141     assertThat(actualResponse.getProperties()).isEqualTo(expectedResponse.getProperties());
142   }
143
144   @Test
145   void thatUserPreferencesCanNotBeFound() {
146
147     PreferencesApiDto actualResponse = requestSpecification("test-canNotBeFound")
148         .given()
149         .accept(MediaType.APPLICATION_JSON_VALUE)
150         .when()
151         .get("/v1/preferences")
152         .then()
153         .statusCode(HttpStatus.OK.value())
154         .extract()
155         .body()
156         .as(PreferencesApiDto.class);
157
158     assertThat(actualResponse).isNotNull();
159     assertThat(actualResponse.getProperties()).isNull();
160   }
161 }