Implement Plugin Registration in DMI-Plugin 55/122455/14
authorniamhcore <niamh.core@est.tech>
Tue, 6 Jul 2021 09:32:17 +0000 (10:32 +0100)
committerniamhcore <niamh.core@est.tech>
Thu, 15 Jul 2021 13:45:34 +0000 (14:45 +0100)
- Refactor ncmp/dmi package

Issue-ID: CPS-405
Issue-ID: CPS-499
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I0a7ab9bb0ca3861b171388613c44f7bc6e7f9aa9

16 files changed:
lombok.config [new file with mode: 0644]
pom.xml
src/main/java/org/onap/cps/ncmp/dmi/Application.java [moved from src/main/java/org/onap/cps/ncmp/Application.java with 97% similarity]
src/main/java/org/onap/cps/ncmp/dmi/config/CpsConfiguration.java [new file with mode: 0644]
src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java [moved from src/main/java/org/onap/cps/ncmp/config/DmiPluginConfig.java with 97% similarity]
src/main/java/org/onap/cps/ncmp/dmi/config/WebSecurityConfig.java [moved from src/main/java/org/onap/cps/ncmp/config/WebSecurityConfig.java with 98% similarity]
src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java [moved from src/main/java/org/onap/cps/ncmp/rest/controller/DmiRestController.java with 94% similarity]
src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java [moved from src/main/java/org/onap/cps/ncmp/service/DmiService.java with 96% similarity]
src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java [moved from src/main/java/org/onap/cps/ncmp/service/DmiServiceImpl.java with 96% similarity]
src/main/java/org/onap/cps/ncmp/dmi/service/client/NcmpRestClient.java [new file with mode: 0644]
src/main/resources/application.yml
src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy [moved from src/test/groovy/org/onap/cps/ncmp/rest/controller/ControllerSecuritySpec.groovy with 98% similarity]
src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy [moved from src/test/groovy/org/onap/cps/ncmp/rest/controller/DmiRestControllerSpec.groovy with 96% similarity]
src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy [moved from src/test/groovy/org/onap/cps/ncmp/service/DmiServiceImplSpec.groovy with 96% similarity]
src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy [new file with mode: 0644]
src/test/java/org/onap/cps/ncmp/dmi/rest/controller/TestController.java [moved from src/test/java/org/onap/cps/ncmp/rest/controller/TestController.java with 96% similarity]

diff --git a/lombok.config b/lombok.config
new file mode 100644 (file)
index 0000000..8e900f6
--- /dev/null
@@ -0,0 +1,20 @@
+#  ============LICENSE_START=======================================================
+#  Copyright (C) 2021 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=========================================================
+
+config.stopBubbling = true
+lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 26a7b4e..ffa1434 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -38,7 +38,7 @@
     <name>ncmp-dmi-plugin</name>
     <description>DMI Plugin Service</description>
     <properties>
-        <app>org.onap.cps.ncmp.Application</app>
+        <app>org.onap.cps.ncmp.dmi.Application</app>
         <base.image>${docker.pull.registry}/onap/integration-java11:8.0.0</base.image>
         <cps.version>1.1.0-SNAPSHOT</cps.version>
         <image.tag>${project.version}-${maven.build.timestamp}</image.tag>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.micrometer</groupId>
             <artifactId>micrometer-registry-prometheus</artifactId>
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp;
+package org.onap.cps.ncmp.dmi;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/config/CpsConfiguration.java b/src/main/java/org/onap/cps/ncmp/dmi/config/CpsConfiguration.java
new file mode 100644 (file)
index 0000000..a9970da
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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.cps.ncmp.dmi.config;
+
+import lombok.Getter;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Provides access to cps base url and cps authentication.
+ */
+@Configuration
+public class CpsConfiguration {
+
+    @Getter
+    @Component
+    public static class CpsProperties {
+
+        @Value("${cps-core.baseUrl}")
+        private String baseUrl;
+        @Value("${cps-core.dmiRegistrationUrl}")
+        private String dmiRegistrationUrl;
+        @Value("${cps-core.auth.username}")
+        private String authUsername;
+        @Value("${cps-core.auth.password}")
+        private String authPassword;
+    }
+
+    @Bean
+    public RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder) {
+        return restTemplateBuilder.build();
+    }
+}
\ No newline at end of file
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.config;
+package org.onap.cps.ncmp.dmi.config;
 
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.config;
+package org.onap.cps.ncmp.dmi.config;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.rest.controller;
+package org.onap.cps.ncmp.dmi.rest.controller;
 
+import org.onap.cps.ncmp.dmi.service.DmiService;
 import org.onap.cps.ncmp.rest.api.DmiPluginApi;
-import org.onap.cps.ncmp.service.DmiService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.service;
+package org.onap.cps.ncmp.dmi.service;
 
 /**
  * Interface for handling Dmi plugin Data.
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.service;
+package org.onap.cps.ncmp.dmi.service;
 
 import org.springframework.stereotype.Service;
 
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/service/client/NcmpRestClient.java b/src/main/java/org/onap/cps/ncmp/dmi/service/client/NcmpRestClient.java
new file mode 100644 (file)
index 0000000..2158e83
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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.cps.ncmp.dmi.service.client;
+
+import org.onap.cps.ncmp.dmi.config.CpsConfiguration.CpsProperties;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriComponentsBuilder;
+
+@Component
+public class NcmpRestClient {
+
+    private CpsProperties cpsProperties;
+    private RestTemplate restTemplate;
+
+    public NcmpRestClient(final CpsProperties cpsProperties, final RestTemplate restTemplate) {
+        this.cpsProperties = cpsProperties;
+        this.restTemplate = restTemplate;
+    }
+
+    /**
+     * Register a cmHandle with NCMP using a HTTP call.
+     * @param jsonData json data
+     * @return the response entity
+     */
+    public ResponseEntity<String> registerCmHandlesWithNcmp(final String jsonData) {
+        final var ncmpRegistrationUrl = buildNcmpRegistrationUrl();
+        final var httpHeaders = new HttpHeaders();
+        httpHeaders.setBasicAuth(cpsProperties.getAuthUsername(), cpsProperties.getAuthPassword());
+        httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
+        final var httpEntity = new HttpEntity<>(jsonData, httpHeaders);
+        return restTemplate.postForEntity(ncmpRegistrationUrl, httpEntity, String.class);
+    }
+
+    private String buildNcmpRegistrationUrl() {
+        return UriComponentsBuilder
+            .fromHttpUrl(cpsProperties.getBaseUrl())
+            .path(cpsProperties.getDmiRegistrationUrl())
+            .toUriString();
+    }
+}
\ No newline at end of file
index ad1c8a3..6a0cf97 100644 (file)
@@ -45,3 +45,12 @@ management:
       # kubernetes probes: liveness and readiness
       probes:
         enabled: true
+    loggers:
+      enabled: true
+
+cps-core:
+  baseUrl: http://${CPS_CORE_HOST}:${CPS_CORE_PORT}
+  dmiRegistrationUrl : /cps-ncmp/api/ncmp-dmi/v1/ch
+  auth:
+    username: ${CPS_CORE_USERNAME}
+    password: ${CPS_CORE_PASSWORD}
\ No newline at end of file
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.rest.controller
+package org.onap.cps.ncmp.dmi.rest.controller
 
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
 
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.rest.controller
+package org.onap.cps.ncmp.dmi.rest.controller
+
+import org.onap.cps.ncmp.dmi.service.DmiService
 
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
 
-import org.onap.cps.ncmp.service.DmiService
 import org.spockframework.spring.SpringBean
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
 import org.springframework.http.HttpStatus
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.service
+package org.onap.cps.ncmp.dmi.service
 
 
 import spock.lang.Specification
diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy
new file mode 100644 (file)
index 0000000..4f92986
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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.cps.ncmp.dmi.service.client
+
+import org.onap.cps.ncmp.dmi.config.CpsConfiguration
+import org.springframework.http.ResponseEntity
+import org.springframework.web.client.RestTemplate
+import spock.lang.Specification
+
+class NcmpRestClientSpec extends Specification {
+    def objectUnderTest = new NcmpRestClient(mockCpsProperties, mockRestTemplate)
+    def mockCpsProperties = Mock(CpsConfiguration.CpsProperties)
+    def mockRestTemplate = Mock(RestTemplate)
+
+    def setup() {
+        objectUnderTest.cpsProperties = mockCpsProperties
+        objectUnderTest.restTemplate = mockRestTemplate
+    }
+
+    def 'Register a cm handle.'() {
+        given: 'json data'
+            def jsonData = 'some json'
+        and: 'configuration data'
+            mockCpsProperties.baseUrl >> 'http://some-uri'
+            mockCpsProperties.dmiRegistrationUrl >> 'some-url'
+            mockCpsProperties.authUsername >> 'some-username'
+            mockCpsProperties.authPassword >> 'some-password'
+        and: 'the rest template returns a valid response entity'
+            def mockResponseEntity = Mock(ResponseEntity)
+        when: 'registerCmHandle is invoked'
+            def result = objectUnderTest.registerCmHandlesWithNcmp(jsonData)
+        then: 'the rest template is called with the correct uri and json in the body'
+            1 * mockRestTemplate.postForEntity({ it.toString() == 'http://some-uri/some-url' },
+                    { it.body.contains(jsonData) }, String.class) >> mockResponseEntity
+        and: 'the output of the method is the same as the output from the test template'
+            result == mockResponseEntity
+    }
+}
\ No newline at end of file
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.rest.controller;
+package org.onap.cps.ncmp.dmi.rest.controller;
 
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;