From 50816e1fdd0da6b932c3595f41425b595c3823f0 Mon Sep 17 00:00:00 2001 From: lapentafd Date: Wed, 18 Jan 2023 13:34:27 +0000 Subject: [PATCH] Adding Server stubs for policy-api Added server stubs for policy-api with profile "stub", with very simple implementation, and unit tests Added context-path in the application.yaml Issue-ID: POLICY-4143 Change-Id: Ibcf4cf2f6d3e6a87ac7eea365b387d75e19e96c0 Signed-off-by: lapentafd --- .../policy/api/main/rest/ApiRestController.java | 2 + .../api/main/rest/NodeTemplateController.java | 2 + .../api/main/rest/stub/ApiRestControllerStub.java | 183 +++++++++++++++++++++ .../main/rest/stub/NodeTemplateControllerStub.java | 72 ++++++++ .../onap/policy/api/main/rest/stub/StubUtils.java | 81 +++++++++ main/src/main/resources/application-stub.yaml | 3 + main/src/main/resources/application.yaml | 10 +- main/src/main/resources/openapi/openapi.yaml | 28 ++-- .../policy/api/main/rest/TestApiRestServer.java | 2 +- .../api/main/rest/TestApiStatisticsManager.java | 2 +- .../api/main/rest/TestNodeTemplateController.java | 2 +- .../main/rest/stub/ApisRestControllerStubTest.java | 92 +++++++++++ .../main/rest/utils/CommonTestRestController.java | 2 +- 13 files changed, 460 insertions(+), 21 deletions(-) create mode 100644 main/src/main/java/org/onap/policy/api/main/rest/stub/ApiRestControllerStub.java create mode 100644 main/src/main/java/org/onap/policy/api/main/rest/stub/NodeTemplateControllerStub.java create mode 100644 main/src/main/java/org/onap/policy/api/main/rest/stub/StubUtils.java create mode 100644 main/src/main/resources/application-stub.yaml create mode 100644 main/src/test/java/org/onap/policy/api/main/rest/stub/ApisRestControllerStubTest.java diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java index d440e1ce..47d82927 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -43,6 +43,7 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.context.annotation.Profile; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -55,6 +56,7 @@ import org.springframework.web.bind.annotation.RestController; */ @RestController @RequiredArgsConstructor +@Profile("default") public class ApiRestController extends CommonRestController implements PolicyDesignApi { private enum Target { diff --git a/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java b/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java index ecad0e59..44962663 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java @@ -34,6 +34,7 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.context.annotation.Profile; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -42,6 +43,7 @@ import org.springframework.web.bind.annotation.RestController; */ @RestController @RequiredArgsConstructor +@Profile("default") public class NodeTemplateController extends CommonRestController implements ToscaNodeTemplateDesignApi { private final ToscaServiceTemplateService toscaServiceTemplateService; diff --git a/main/src/main/java/org/onap/policy/api/main/rest/stub/ApiRestControllerStub.java b/main/src/main/java/org/onap/policy/api/main/rest/stub/ApiRestControllerStub.java new file mode 100644 index 00000000..4b848904 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/stub/ApiRestControllerStub.java @@ -0,0 +1,183 @@ +/*- + * ============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 java.util.UUID; +import javax.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.onap.policy.api.main.rest.CommonRestController; +import org.onap.policy.api.main.rest.PolicyFetchMode; +import org.onap.policy.api.main.rest.genapi.PolicyDesignApi; +import org.onap.policy.api.main.rest.provider.statistics.StatisticsReport; +import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.context.annotation.Profile; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Profile("stub") +public class ApiRestControllerStub extends CommonRestController implements PolicyDesignApi { + + private final StubUtils stubUtils; + + @Override + public ResponseEntity createPolicies( + @Valid ToscaServiceTemplate body, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity createPolicy( + String policyTypeId, + String policyTypeVersion, + @Valid ToscaServiceTemplate body, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity createPolicyType( + @Valid ToscaServiceTemplate body, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity deleteSpecificPolicy( + String policyId, + String policyVersion, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity deleteSpecificVersionOfPolicy( + String policyTypeId, + String policyTypeVersion, + String policyId, + String policyVersion, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity deleteSpecificVersionOfPolicyType( + String policyTypeId, + String versionId, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getAllPolicies( + String policyTypeId, + String policyTypeVersion, + @Valid PolicyFetchMode mode, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getAllPolicyTypes( + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getAllVersionsOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + @Valid PolicyFetchMode mode, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getAllVersionsOfPolicyType( + String policyTypeId, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getHealthCheck( + UUID requestID) { + return stubUtils.getStubbedResponse(HealthCheckReport.class); + } + + @Override + public ResponseEntity getLatestVersionOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + @Valid PolicyFetchMode mode, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getLatestVersionOfPolicyType( + String policyTypeId, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getPolicies( + @Valid PolicyFetchMode mode, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getSpecificPolicy( + String policyId, + String policyVersion, + @Valid PolicyFetchMode mode, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getSpecificVersionOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + String policyVersion, + @Valid PolicyFetchMode mode, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getSpecificVersionOfPolicyType( + String policyTypeId, + String versionId, + UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity getStatistics( + UUID requestID) { + return stubUtils.getStubbedResponse(StatisticsReport.class); + } + +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/stub/NodeTemplateControllerStub.java b/main/src/main/java/org/onap/policy/api/main/rest/stub/NodeTemplateControllerStub.java new file mode 100644 index 00000000..2e9f4a21 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/stub/NodeTemplateControllerStub.java @@ -0,0 +1,72 @@ +/*- + * ============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 java.util.List; +import java.util.UUID; +import javax.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.onap.policy.api.main.rest.CommonRestController; +import org.onap.policy.api.main.rest.genapi.ToscaNodeTemplateDesignApi; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.context.annotation.Profile; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Profile("stub") +public class NodeTemplateControllerStub extends CommonRestController + implements ToscaNodeTemplateDesignApi { + + private final StubUtils stubUtils; + + @Override + public ResponseEntity createToscaNodeTemplates( + @Valid ToscaServiceTemplate body, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity deleteToscaNodeTemplates( + String name, String version, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } + + @Override + public ResponseEntity> getAllNodeTemplates( + UUID requestID) { + return stubUtils.getStubbedResponseList(ToscaNodeTemplate.class); + } + + @Override + public ResponseEntity> getSpecificVersionOfNodeTemplate( + String name, String version, UUID requestID) { + return stubUtils.getStubbedResponseList(ToscaNodeTemplate.class); + } + + @Override + public ResponseEntity updateToscaNodeTemplates( + @Valid ToscaServiceTemplate body, UUID requestID) { + return stubUtils.getStubbedResponse(ToscaServiceTemplate.class); + } +} 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 index 00000000..562ce565 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/stub/StubUtils.java @@ -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(); + + ResponseEntity getStubbedResponse(Class 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); + } + + ResponseEntity> getStubbedResponseList(Class 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); + } + +} diff --git a/main/src/main/resources/application-stub.yaml b/main/src/main/resources/application-stub.yaml new file mode 100644 index 00000000..b509fd69 --- /dev/null +++ b/main/src/main/resources/application-stub.yaml @@ -0,0 +1,3 @@ +spring: + application: + name: "policy-api-stub" \ No newline at end of file diff --git a/main/src/main/resources/application.yaml b/main/src/main/resources/application.yaml index 41a3afd4..305af94b 100644 --- a/main/src/main/resources/application.yaml +++ b/main/src/main/resources/application.yaml @@ -1,7 +1,6 @@ -server: - port: 6969 - spring: + profiles: + active: default security.user: name: policyadmin password: zb!XztG34 @@ -21,6 +20,11 @@ spring: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy +server: + port: 6969 + servlet: + context-path: /policy/api/v1 + policy-api: name: ApiGroup aaf: false diff --git a/main/src/main/resources/openapi/openapi.yaml b/main/src/main/resources/openapi/openapi.yaml index 4b71aebd..5ab7e796 100644 --- a/main/src/main/resources/openapi/openapi.yaml +++ b/main/src/main/resources/openapi/openapi.yaml @@ -32,7 +32,7 @@ tags: that, API is also exposed for clients to retrieve healthcheck status of this API rest service and the statistics report including the counters of API invocation. paths: - /policy/api/v1/healthcheck: + /healthcheck: get: tags: - "Policy Design" @@ -144,7 +144,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/nodetemplates: + /nodetemplates: get: tags: - "Tosca Node Template Design" @@ -623,7 +623,7 @@ paths: api-version: 1.0.0 last-mod-release: Jakarta x-codegen-request-body-name: body - /policy/api/v1/nodetemplates/{name}/versions/{version}: + /nodetemplates/{name}/versions/{version}: get: tags: - "Tosca Node Template Design" @@ -925,7 +925,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Jakarta - /policy/api/v1/policies: + /policies: get: tags: - "Policy Design" @@ -1224,7 +1224,7 @@ paths: api-version: 1.0.0 last-mod-release: El Alto x-codegen-request-body-name: body - /policy/api/v1/policies/{policyId}/versions/{policyVersion}: + /policies/{policyId}/versions/{policyVersion}: get: tags: - "Policy Design" @@ -1512,7 +1512,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Guilin - /policy/api/v1/policytypes: + /policytypes: get: tags: - "Policy Design" @@ -1805,7 +1805,7 @@ paths: api-version: 1.0.0 last-mod-release: Dublin x-codegen-request-body-name: body - /policy/api/v1/policytypes/{policyTypeId}: + /policytypes/{policyTypeId}: get: tags: - "Policy Design" @@ -1924,7 +1924,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/policytypes/{policyTypeId}/versions/latest: + /policytypes/{policyTypeId}/versions/latest: get: tags: - "Policy Design" @@ -2042,7 +2042,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies: + /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies: get: tags: - "Policy Design" @@ -2367,7 +2367,7 @@ paths: api-version: 1.0.0 last-mod-release: Dublin x-codegen-request-body-name: body - /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}: + /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}: get: tags: - "Policy Design" @@ -2504,7 +2504,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest: + /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest: get: tags: - "Policy Design" @@ -2640,7 +2640,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}: + /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}: get: tags: - "Policy Design" @@ -2954,7 +2954,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/policytypes/{policyTypeId}/versions/{versionId}: + /policytypes/{policyTypeId}/versions/{versionId}: get: tags: - "Policy Design" @@ -3237,7 +3237,7 @@ paths: x-interface info: api-version: 1.0.0 last-mod-release: Dublin - /policy/api/v1/statistics: + /statistics: get: tags: - "Policy Design" diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index eab9fe72..412c28d1 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -68,7 +68,7 @@ import org.springframework.test.context.junit4.SpringRunner; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ActiveProfiles("test") +@ActiveProfiles({ "test", "default" }) @DirtiesContext(classMode = ClassMode.AFTER_CLASS) public class TestApiRestServer extends CommonTestRestController { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java index f343e135..0e01ee40 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java @@ -37,7 +37,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = PolicyApiApplication.class) -@ActiveProfiles("test") +@ActiveProfiles({ "test", "default" }) public class TestApiStatisticsManager { @Autowired diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java b/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java index 993fbee1..fb7a152a 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java @@ -57,7 +57,7 @@ import org.springframework.test.context.junit4.SpringRunner; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ActiveProfiles("test") +@ActiveProfiles({ "test", "default" }) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class TestNodeTemplateController extends CommonTestRestController { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/stub/ApisRestControllerStubTest.java b/main/src/test/java/org/onap/policy/api/main/rest/stub/ApisRestControllerStubTest.java new file mode 100644 index 00000000..6608f9b7 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/stub/ApisRestControllerStubTest.java @@ -0,0 +1,92 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import javax.ws.rs.core.Response; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.policy.api.main.PolicyApiApplication; +import org.onap.policy.api.main.rest.utils.CommonTestRestController; +import org.onap.policy.common.utils.security.SelfSignedKeyStore; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles({ "test", "stub" }) +@DirtiesContext(classMode = ClassMode.AFTER_CLASS) +public class ApisRestControllerStubTest extends CommonTestRestController { + protected static final String APP_JSON = "application/json"; + protected static final String APP_YAML = "application/yaml"; + + @LocalServerPort + private int apiPort; + + private static SelfSignedKeyStore keystore; + + + @BeforeClass + public static void setupParameters() throws IOException, InterruptedException { + keystore = new SelfSignedKeyStore(); + } + + @DynamicPropertySource + static void registerPgProperties(DynamicPropertyRegistry registry) { + registry.add("server.ssl.enabled", () -> "true"); + registry.add("server.ssl.key-store", () -> keystore.getKeystoreName()); + registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD); + registry.add("server.ssl.key-store-type", () -> "PKCS12"); + registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org"); + registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD); + } + + @Test + public void testStubbedGet() throws Exception { + checkStubJson("policies"); + checkStubJson("policies/policyname/versions/1.0.2"); + checkStubJson("nodetemplates"); + checkStubJson("nodetemplates/k8stemplate/versions/1.0.0"); + checkStubJson("policytypes"); + checkStubJson("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest"); + checkStubJson("statistics"); + checkStubJson("healthcheck"); + } + + private void checkStubJson(String url) throws Exception { + var response = super.readResource(url, APP_JSON, apiPort); + assertNotNull(response); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + var responseYaml = super.readResource(url, APP_YAML, apiPort); + assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus()); + } + +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/utils/CommonTestRestController.java b/main/src/test/java/org/onap/policy/api/main/rest/utils/CommonTestRestController.java index 4f2e8b22..947ed6a5 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/utils/CommonTestRestController.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/utils/CommonTestRestController.java @@ -60,7 +60,7 @@ public class CommonTestRestController { protected static final String CONTEXT_PATH = "/policy/api/v1/"; protected void testSwagger(final int apiPort) throws Exception { - final Invocation.Builder invocationBuilder = sendHttpsRequest("/", "v3/api-docs", APP_JSON, apiPort); + final Invocation.Builder invocationBuilder = sendHttpsRequest(CONTEXT_PATH, "v3/api-docs", APP_JSON, apiPort); final String resp = invocationBuilder.get(String.class); assertTrue((resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\"")); } -- 2.16.6