aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/test/java/org/onap/portalng/bff/actions/ActionsMocks.java
blob: aa9e2f3e9069fcbb529d3079b4df364a4fee4e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 *
 */

package org.onap.portalng.bff.actions;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import io.restassured.http.Header;
import org.apache.http.HttpHeaders;
import org.onap.portalng.bff.BaseIntegrationTest;
import org.onap.portalng.bff.openapi.client_portal_history.model.ActionResponsePortalHistoryDto;
import org.onap.portalng.bff.openapi.client_portal_history.model.ActionsListResponsePortalHistoryDto;
import org.onap.portalng.bff.openapi.client_portal_history.model.ProblemPortalHistoryDto;
import org.onap.portalng.bff.openapi.server.model.ActionsListResponseApiDto;
import org.onap.portalng.bff.openapi.server.model.ActionsResponseApiDto;
import org.onap.portalng.bff.openapi.server.model.CreateActionRequestApiDto;
import org.onap.portalng.bff.openapi.server.model.ProblemApiDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

public class ActionsMocks extends BaseIntegrationTest {
  protected static final String X_REQUEST_ID = "addf6005-3075-4c80-b7bc-2c70b7d42b00";

  // used for test thatActionsListCanBeRetrieved
  protected ActionsListResponseApiDto listActions() {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .when()
        .get("/actions")
        .then()
        .statusCode(HttpStatus.OK.value())
        .extract()
        .body()
        .as(ActionsListResponseApiDto.class);
  }

  // used for test thatActionsListCanBeRetrieved
  protected void mockListActions(
      ActionsListResponsePortalHistoryDto actionsListResponsePortalHistoryDto) throws Exception {
    WireMock.stubFor(
        WireMock.get(WireMock.urlEqualTo("/v1/actions?page=1&pageSize=10"))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .withBody(
                        objectMapper.writeValueAsString(actionsListResponsePortalHistoryDto))));
  }

  // used for test thatActionsListCanNotBeRetrieved
  protected ProblemApiDto listActionsProblem() {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .when()
        .get("/actions")
        .then()
        .statusCode(HttpStatus.BAD_GATEWAY.value())
        .extract()
        .body()
        .as(ProblemApiDto.class);
  }

  // used for test thatActionsListCanNotBeRetrieved
  protected void mockListActionsProblem(ProblemPortalHistoryDto problemPortalHistoryDto)
      throws Exception {
    WireMock.stubFor(
        WireMock.get(WireMock.urlEqualTo("/v1/actions?page=1&pageSize=10"))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE)
                    .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
                    .withBody(objectMapper.writeValueAsString(problemPortalHistoryDto))));
  }

  // used for test thatActionCanBeRetrieved
  protected void mockGetActions(
      ActionsListResponsePortalHistoryDto actionsListResponsePortalHistoryDto,
      String userId,
      Integer showLastHours)
      throws Exception {
    WireMock.stubFor(
        WireMock.get(
                WireMock.urlEqualTo(
                    "/v1/actions/"
                        + userId
                        + "?page=1&pageSize=10"
                        + "&showLastHours="
                        + showLastHours))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .withBody(
                        objectMapper.writeValueAsString(actionsListResponsePortalHistoryDto))));
  }
  // used for test thatActionCanBeRetrieved
  protected ActionsListResponseApiDto getActions(String userId) {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .when()
        .get("/actions/" + userId + "?page=1&pageSize=10&showLastHours=2")
        .then()
        .statusCode(HttpStatus.OK.value())
        .extract()
        .body()
        .as(ActionsListResponseApiDto.class);
  }

  // used for test thatActionCanBeRetrievedWithoutParameterShowLastHours
  protected void mockGetActionsWithoutParameterShowLastHours(
      ActionsListResponsePortalHistoryDto actionsListResponsePortalHistoryDto, String userId)
      throws Exception {
    WireMock.stubFor(
        WireMock.get(WireMock.urlEqualTo("/v1/actions/" + userId + "?page=1&pageSize=10"))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .withBody(
                        objectMapper.writeValueAsString(actionsListResponsePortalHistoryDto))));
  }
  // used for test thatActionCanBeRetrievedWithoutParameterShowLastHours
  protected ActionsListResponseApiDto getActionsWithoutParameterShowLastHours(String userId) {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .when()
        .get("/actions/" + userId + "?page=1&pageSize=10")
        .then()
        .statusCode(HttpStatus.OK.value())
        .extract()
        .body()
        .as(ActionsListResponseApiDto.class);
  }

  // Used for thatActionCanBeCreated
  protected void mockCreateActions(
      String userId, ActionResponsePortalHistoryDto actionResponsePortalHistoryDto)
      throws Exception {
    WireMock.stubFor(
        WireMock.post(WireMock.urlEqualTo("/v1/actions/" + userId))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .withRequestBody(WireMock.matchingJsonPath("$.action"))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .withStatus(200)
                    .withBody(objectMapper.writeValueAsString(actionResponsePortalHistoryDto))));
  }

  // Used for thatActionCanBeCreated
  protected ActionsResponseApiDto createAction(
      CreateActionRequestApiDto createActionRequestApiDto, String userId) throws Exception {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .body(objectMapper.writeValueAsString(createActionRequestApiDto))
        .when()
        .post("/actions/" + userId)
        .then()
        .statusCode(HttpStatus.OK.value())
        .extract()
        .body()
        .as(ActionsResponseApiDto.class);
  }

  // Used for thatActionCanNotBeCreated
  protected void mockCreateActionsProblem(
      String userId, ProblemPortalHistoryDto problemPortalHistoryDto)
      throws JsonProcessingException {
    WireMock.stubFor(
        WireMock.post(WireMock.urlEqualTo("/v1/actions/" + userId))
            .withHeader("X-Request-Id", new EqualToPattern(X_REQUEST_ID))
            .withRequestBody(WireMock.matchingJsonPath("$.action"))
            .willReturn(
                WireMock.aResponse()
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE)
                    .withStatus(500)
                    .withBody(objectMapper.writeValueAsString(problemPortalHistoryDto))));
  }
  // Used for thatActionCanNotBeCreated
  protected ProblemApiDto createActionProblem(
      CreateActionRequestApiDto createActionRequestApiDto, String userId)
      throws JsonProcessingException {
    return requestSpecification()
        .given()
        .accept(MediaType.APPLICATION_JSON_VALUE)
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .header(new Header("X-Request-Id", X_REQUEST_ID))
        .body(objectMapper.writeValueAsString(createActionRequestApiDto))
        .when()
        .post("/actions/" + userId)
        .then()
        .statusCode(HttpStatus.BAD_GATEWAY.value())
        .extract()
        .body()
        .as(ProblemApiDto.class);
  }
}