diff options
Diffstat (limited to 'cps-nf-proxy-rest/src')
8 files changed, 354 insertions, 0 deletions
diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java new file mode 100644 index 0000000000..abad806e0b --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java new file mode 100755 index 0000000000..3bdedc3631 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class NfProxyConfig { + + /** + * Swagger-ui configuration. + */ + @Bean + public Docket api() { + return new Docket(DocumentationType.OAS_30).select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + +}
\ No newline at end of file diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java new file mode 100644 index 0000000000..8125c5aed7 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy.rest.controller; + +import org.onap.cps.nfproxy.rest.api.NfProxyApi; +import org.onap.cps.spi.exceptions.CpsException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("${rest.api.base-path}") +public class NfProxyController implements NfProxyApi { + + @Override + public ResponseEntity<Object> helloWorld() { + return new ResponseEntity<>("Hello World!", HttpStatus.OK); + } + + @Override + public ResponseEntity<Object> helloError() { + throw new CpsException("Example error Message", "Example error description"); + } +} diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java new file mode 100644 index 0000000000..96a1cb267d --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy.rest.exceptions; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.cps.nfproxy.rest.controller.NfProxyController; +import org.onap.cps.nfproxy.rest.model.ErrorMessage; +import org.onap.cps.spi.exceptions.CpsException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * Exception handler with error message return. + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@RestControllerAdvice(assignableTypes = {NfProxyController.class}) +public class NfProxyRestExceptionHandler { + + /** + * Default exception handler. + * + * @param exception the exception to handle + * @return response with response code 500. + */ + @ExceptionHandler + public static ResponseEntity<Object> handleInternalServerErrorExceptions( + final Exception exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); + } + + @ExceptionHandler({CpsException.class}) + public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + } + + private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) { + return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); + } + + private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message, + final String details) { + log.error("An error has occurred : {} Status: {} Details: {}", message, status, details); + final ErrorMessage errorMessage = new ErrorMessage(); + errorMessage.setStatus(status.toString()); + errorMessage.setMessage(message); + errorMessage.setDetails(details); + return new ResponseEntity<>(errorMessage, status); + } + + private static String extractDetails(final CpsException exception) { + return exception.getCause() == null + ? exception.getDetails() + : ExceptionUtils.getStackTrace(exception.getCause()); + } +} diff --git a/cps-nf-proxy-rest/src/main/resources/application.yml b/cps-nf-proxy-rest/src/main/resources/application.yml new file mode 100644 index 0000000000..06c0fd1ea3 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/resources/application.yml @@ -0,0 +1,46 @@ +server: + port: 8080 + +rest: + api: + base-path: /cps-nf-proxy/api + +spring: + main: + banner-mode: "off" +# for POC only, later this should move to cpi-ri module + jpa: + ddl-auto: create + open-in-view: false + properties: + hibernate: + enable_lazy_load_no_trans: true + dialect: org.hibernate.dialect.PostgreSQLDialect + + datasource: + url: jdbc:postgresql://${DB_HOST}:5432/cpsdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driverClassName: org.postgresql.Driver + initialization-mode: always + +# Actuator +management: + endpoints: + web: + base-path: /manage + exposure: + include: info,health,loggers + endpoint: + health: + show-details: always + # kubernetes probes: liveness and readiness + probes: + enabled: true + loggers: + enabled: true + +logging: + level: + org: + springframework: INFO diff --git a/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json b/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json new file mode 100644 index 0000000000..efc2f9778a --- /dev/null +++ b/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json @@ -0,0 +1,28 @@ +{ + "resourcePackages": [ + "org.onap.cps.nfproxy.rest.controller" + ], + "prettyPrint": true, + "cacheTTL": 0, + "openAPI": { + "info": { + "title": "ONAP Open API v3 CPS xNF Proxy Spec", + "description": "The API Description may be multiline, and GitHub Flavored Markdown, GFM syntax, can be used for rich text representation.", + "x-logo": { + "url": "logo.png" + }, + "contact": { + "name": "ONAP", + "url": "https://onap.readthedocs.io", + "email": "onap-discuss@lists.onap.org" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + }, + "version": "1.2.34", + "x-planned-retirement-date": "202207", + "x-component": "Modeling" + } + } +} diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy new file mode 100644 index 0000000000..3eb42d7779 --- /dev/null +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy.config + + +import spock.lang.Specification +import springfox.documentation.spring.web.plugins.Docket + +class NfProxyConfigSpec extends Specification { + def objectUnderTest = new NfProxyConfig() + + def 'xNF Proxy configuration has a Docket API'() { + expect: 'the CPS configuration has a Docket API' + objectUnderTest.api() instanceof Docket + } +} diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy new file mode 100644 index 0000000000..d1c3b1648f --- /dev/null +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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.cps.nfproxy.rest.controller + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.HttpStatus +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import spock.lang.Specification + +@WebMvcTest +class NfProxyControllerSpec extends Specification { + + @Autowired + MockMvc mvc + + @Value('${rest.api.base-path}') + def basePath + + def 'Hello world method invocation.'(){ + when: 'hello-world request performed' + def response = mvc.perform(MockMvcRequestBuilders.get("$basePath/v1/hello-world")).andReturn().response + then: 'success response returned' + response.status == HttpStatus.OK.value() + response.getContentAsString().contains("Hello World!") + } + + def 'Example error handling.'(){ + when: 'hello-error request performed' + def response = mvc.perform(MockMvcRequestBuilders.get("$basePath/v1/hello-error")).andReturn().response + then: 'error response returned' + response.status == HttpStatus.INTERNAL_SERVER_ERROR.value() + response.getContentAsString().contains("Example error") + } +} |