2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.rest.stub;
23 import com.google.gson.Gson;
24 import jakarta.servlet.http.HttpServletRequest;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.context.annotation.Profile;
32 import org.springframework.core.io.ClassPathResource;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
37 @RequiredArgsConstructor
41 private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
42 private final HttpServletRequest request;
43 private static final String ACCEPT = "Accept";
44 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
45 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
46 private static final Gson JSON_TRANSLATOR = new Gson();
48 <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
49 var accept = request.getHeader(ACCEPT);
50 if (accept != null && accept.contains("application/json")) {
51 final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
52 try (var inputStream = resource.getInputStream()) {
53 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
54 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
55 return new ResponseEntity<>(targetObject, HttpStatus.OK);
56 } catch (IOException e) {
57 log.error("Couldn't serialize response for content type application/json", e);
58 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
61 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
64 <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
65 var accept = request.getHeader(ACCEPT);
66 if (accept != null && accept.contains("application/json")) {
67 final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
68 try (var inputStream = resource.getInputStream()) {
69 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
70 var targetObject = List.of(JSON_TRANSLATOR.fromJson(string, clazz));
71 return new ResponseEntity<>(targetObject, HttpStatus.OK);
72 } catch (IOException e) {
73 log.error("Couldn't serialize response for content type application/json", e);
74 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
77 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);