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
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2023 Nordix Foundation.
* Modifications Copyright (C) 2023 Bell Canada.
* ================================================================================
* 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
* ============LICENSE_END=========================================================
*/
package org.onap.policy.pap.main.rest.stub;
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.pap.main.rest.PapRestControllerV1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
@Profile("stub")
class StubUtils {
private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
private static final String APPLICATION_JSON = "application/json";
private static final String SERIALIZE_RESPONSE_FAILURE_MSG =
"Couldn't serialize response for content type application/json";
private final HttpServletRequest request;
private static final String ACCEPT = "Accept";
private static final String PAP_DB =
"/PapDb.json";
private static final Gson JSON_TRANSLATOR = new Gson();
<T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
var accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains(APPLICATION_JSON)) {
final var resource = new ClassPathResource(PAP_DB);
try (var inputStream = resource.getInputStream()) {
final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
return new ResponseEntity<>(targetObject, HttpStatus.OK);
} catch (IOException e) {
log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
<T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
var accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains(APPLICATION_JSON)) {
final var resource = new ClassPathResource(PAP_DB);
try (var inputStream = resource.getInputStream()) {
final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
return new ResponseEntity<>(targetObject, HttpStatus.OK);
} catch (IOException e) {
log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
ResponseEntity<Map<String, Object>> getStubbedResponseMap() {
var accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains(APPLICATION_JSON)) {
final var resource = new ClassPathResource(PAP_DB);
try (var inputStream = resource.getInputStream()) {
Map<String, Object> map = new HashMap<>();
final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
map.put(PapRestControllerV1.API_VERSION_NAME,
JSON_TRANSLATOR.fromJson(string, Object.class));
return new ResponseEntity<>(map, HttpStatus.OK);
} catch (IOException e) {
log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> getStubbedResponseStatistics() {
var accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains(APPLICATION_JSON)) {
Map<String, Map<String, List<PdpStatistics>>> map = new HashMap<>();
return new ResponseEntity<>(map, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
|