8eb4aba3d128712b9d6f3cae5d4db633ddf911ea
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / stub / StubUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2023 Bell Canada.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest.stub;
23
24 import com.google.gson.Gson;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import javax.servlet.http.HttpServletRequest;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.policy.pap.main.rest.PapRestControllerV1;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.annotation.Profile;
37 import org.springframework.core.io.ClassPathResource;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Service;
41
42 @RequiredArgsConstructor
43 @Service
44 @Profile("stub")
45 class StubUtils {
46     private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
47     private static final String APPLICATION_JSON = "application/json";
48     private static final String SERIALIZE_RESPONSE_FAILURE_MSG =
49             "Couldn't serialize response for content type application/json";
50     private final HttpServletRequest request;
51     private static final String ACCEPT = "Accept";
52     private static final String PAP_DB =
53             "/PapDb.json";
54     private static final Gson JSON_TRANSLATOR = new Gson();
55
56     <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
57         var accept = request.getHeader(ACCEPT);
58         if (accept != null && accept.contains(APPLICATION_JSON)) {
59             final var resource = new ClassPathResource(PAP_DB);
60             try (var inputStream = resource.getInputStream()) {
61                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
62                 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
63                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
64             } catch (IOException e) {
65                 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
66                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
67             }
68         }
69         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
70     }
71
72     <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
73         var accept = request.getHeader(ACCEPT);
74         if (accept != null && accept.contains(APPLICATION_JSON)) {
75             final var resource = new ClassPathResource(PAP_DB);
76             try (var inputStream = resource.getInputStream()) {
77                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
78                 var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
79                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
80             } catch (IOException e) {
81                 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
82                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
83             }
84         }
85         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
86     }
87
88     ResponseEntity<Map<String, Object>> getStubbedResponseMap() {
89         var accept = request.getHeader(ACCEPT);
90         if (accept != null && accept.contains(APPLICATION_JSON)) {
91             final var resource = new ClassPathResource(PAP_DB);
92             try (var inputStream = resource.getInputStream()) {
93                 Map<String, Object> map = new HashMap<>();
94                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
95                 map.put(PapRestControllerV1.API_VERSION_NAME,
96                         JSON_TRANSLATOR.fromJson(string, Object.class));
97                 return new ResponseEntity<>(map, HttpStatus.OK);
98             } catch (IOException e) {
99                 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
100                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
101             }
102         }
103         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
104     }
105
106 }