Adding Server stubs for policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / stub / StubUtils.java
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/stub/StubUtils.java b/main/src/main/java/org/onap/policy/api/main/rest/stub/StubUtils.java
new file mode 100644 (file)
index 0000000..562ce56
--- /dev/null
@@ -0,0 +1,81 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.api.main.rest.stub;
+
+import com.google.gson.Gson;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import lombok.RequiredArgsConstructor;
+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 final HttpServletRequest request;
+    private static final String ACCEPT = "Accept";
+    private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
+            "nodetemplates/nodetemplates.metadatasets.input.tosca.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(TOSCA_NODE_TEMPLATE_RESOURCE);
+            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("Couldn't serialize response for content type application/json", 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(TOSCA_NODE_TEMPLATE_RESOURCE);
+            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("Couldn't serialize response for content type application/json", e);
+                return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+            }
+        }
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+}