From 71a61bb124a906e6327e78f45ecc9a0f7b1ec0dc Mon Sep 17 00:00:00 2001 From: niamhcore Date: Tue, 6 Jul 2021 10:32:17 +0100 Subject: Implement Plugin Registration in DMI-Plugin - Refactor ncmp/dmi package Issue-ID: CPS-405 Issue-ID: CPS-499 Signed-off-by: niamhcore Change-Id: I0a7ab9bb0ca3861b171388613c44f7bc6e7f9aa9 --- .../rest/controller/ControllerSecuritySpec.groovy | 63 +++++++++++++++++++++ .../rest/controller/DmiRestControllerSpec.groovy | 64 ++++++++++++++++++++++ .../cps/ncmp/dmi/service/DmiServiceImplSpec.groovy | 34 ++++++++++++ .../dmi/service/client/NcmpRestClientSpec.groovy | 56 +++++++++++++++++++ .../rest/controller/ControllerSecuritySpec.groovy | 63 --------------------- .../rest/controller/DmiRestControllerSpec.groovy | 63 --------------------- .../cps/ncmp/service/DmiServiceImplSpec.groovy | 34 ------------ .../ncmp/dmi/rest/controller/TestController.java | 35 ++++++++++++ .../cps/ncmp/rest/controller/TestController.java | 35 ------------ 9 files changed, 252 insertions(+), 195 deletions(-) create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy delete mode 100644 src/test/groovy/org/onap/cps/ncmp/rest/controller/ControllerSecuritySpec.groovy delete mode 100644 src/test/groovy/org/onap/cps/ncmp/rest/controller/DmiRestControllerSpec.groovy delete mode 100644 src/test/groovy/org/onap/cps/ncmp/service/DmiServiceImplSpec.groovy create mode 100644 src/test/java/org/onap/cps/ncmp/dmi/rest/controller/TestController.java delete mode 100644 src/test/java/org/onap/cps/ncmp/rest/controller/TestController.java (limited to 'src/test') diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy new file mode 100644 index 00000000..14ab7147 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/ControllerSecuritySpec.groovy @@ -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.rest.controller + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.HttpStatus +import org.springframework.test.web.servlet.MockMvc +import spock.lang.Specification + +@WebMvcTest(controllers = TestController.class) +class ControllerSecuritySpec extends Specification { + + @Autowired + MockMvc mvc + + def testEndpoint = '/test' + + def 'Get request with authentication'() { + when: 'request is sent with authentication' + def response = mvc.perform( + get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=') + ).andReturn().response + then: 'HTTP OK status code is returned' + assert response.status == HttpStatus.OK.value() + } + + def 'Get request without authentication'() { + when: 'request is sent without authentication' + def response = mvc.perform(get(testEndpoint)).andReturn().response + then: 'HTTP Unauthorized status code is returned' + assert response.status == HttpStatus.UNAUTHORIZED.value() + } + + def 'Get request with invalid authentication'() { + when: 'request is sent with invalid authentication' + def response = mvc.perform( + get(testEndpoint).header("Authorization", 'Basic invalid auth') + ).andReturn().response + then: 'HTTP Unauthorized status code is returned' + assert response.status == HttpStatus.UNAUTHORIZED.value() + } +} diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy new file mode 100644 index 00000000..f8b4c015 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy @@ -0,0 +1,64 @@ +/* + * ============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.rest.controller + +import org.onap.cps.ncmp.dmi.service.DmiService + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get + +import org.spockframework.spring.SpringBean +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.http.HttpStatus +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.test.web.servlet.MockMvc +import spock.lang.Specification + +@WebMvcTest +@AutoConfigureMockMvc(addFilters = false) +class DmiRestControllerSpec extends Specification { + + @SpringBean + DmiService mockDmiService = Mock() + + @Autowired + private MockMvc mvc + + @Value('${rest.api.dmi-base-path}') + def basePath + + def 'Get Hello World'() { + given: 'hello world endpoint' + def helloWorldEndpoint = "$basePath/v1/helloworld" + + when: 'get hello world api is invoked' + def response = mvc.perform( + get(helloWorldEndpoint) + ).andReturn().response + + then: 'Response Status is OK and contains expected text' + response.status == HttpStatus.OK.value() + then: 'the java API was called with the correct parameters' + 1 * mockDmiService.getHelloWorld() + } + +} diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy new file mode 100644 index 00000000..2124c9b4 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy @@ -0,0 +1,34 @@ +/* + * ============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 + + +import spock.lang.Specification + +class DmiServiceImplSpec extends Specification { + def objectUnderTest = new DmiServiceImpl() + + def 'Retrieve Hello World'() { + expect: 'Hello World is Returned' + objectUnderTest.getHelloWorld() == 'Hello World' + } + +} 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 index 00000000..4f929865 --- /dev/null +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/client/NcmpRestClientSpec.groovy @@ -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 diff --git a/src/test/groovy/org/onap/cps/ncmp/rest/controller/ControllerSecuritySpec.groovy b/src/test/groovy/org/onap/cps/ncmp/rest/controller/ControllerSecuritySpec.groovy deleted file mode 100644 index 6536de7a..00000000 --- a/src/test/groovy/org/onap/cps/ncmp/rest/controller/ControllerSecuritySpec.groovy +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============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.rest.controller - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get - -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest -import org.springframework.http.HttpStatus -import org.springframework.test.web.servlet.MockMvc -import spock.lang.Specification - -@WebMvcTest(controllers = TestController.class) -class ControllerSecuritySpec extends Specification { - - @Autowired - MockMvc mvc - - def testEndpoint = '/test' - - def 'Get request with authentication'() { - when: 'request is sent with authentication' - def response = mvc.perform( - get(testEndpoint).header("Authorization", 'Basic Y3BzdXNlcjpjcHNyMGNrcyE=') - ).andReturn().response - then: 'HTTP OK status code is returned' - assert response.status == HttpStatus.OK.value() - } - - def 'Get request without authentication'() { - when: 'request is sent without authentication' - def response = mvc.perform(get(testEndpoint)).andReturn().response - then: 'HTTP Unauthorized status code is returned' - assert response.status == HttpStatus.UNAUTHORIZED.value() - } - - def 'Get request with invalid authentication'() { - when: 'request is sent with invalid authentication' - def response = mvc.perform( - get(testEndpoint).header("Authorization", 'Basic invalid auth') - ).andReturn().response - then: 'HTTP Unauthorized status code is returned' - assert response.status == HttpStatus.UNAUTHORIZED.value() - } -} diff --git a/src/test/groovy/org/onap/cps/ncmp/rest/controller/DmiRestControllerSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/rest/controller/DmiRestControllerSpec.groovy deleted file mode 100644 index 2271bb46..00000000 --- a/src/test/groovy/org/onap/cps/ncmp/rest/controller/DmiRestControllerSpec.groovy +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============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.rest.controller - -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 -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.test.web.servlet.MockMvc -import spock.lang.Specification - -@WebMvcTest -@AutoConfigureMockMvc(addFilters = false) -class DmiRestControllerSpec extends Specification { - - @SpringBean - DmiService mockDmiService = Mock() - - @Autowired - private MockMvc mvc - - @Value('${rest.api.dmi-base-path}') - def basePath - - def 'Get Hello World'() { - given: 'hello world endpoint' - def helloWorldEndpoint = "$basePath/v1/helloworld" - - when: 'get hello world api is invoked' - def response = mvc.perform( - get(helloWorldEndpoint) - ).andReturn().response - - then: 'Response Status is OK and contains expected text' - response.status == HttpStatus.OK.value() - then: 'the java API was called with the correct parameters' - 1 * mockDmiService.getHelloWorld() - } - -} diff --git a/src/test/groovy/org/onap/cps/ncmp/service/DmiServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/service/DmiServiceImplSpec.groovy deleted file mode 100644 index 85303f4b..00000000 --- a/src/test/groovy/org/onap/cps/ncmp/service/DmiServiceImplSpec.groovy +++ /dev/null @@ -1,34 +0,0 @@ -/* - * ============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.service - - -import spock.lang.Specification - -class DmiServiceImplSpec extends Specification { - def objectUnderTest = new DmiServiceImpl() - - def 'Retrieve Hello World'() { - expect: 'Hello World is Returned' - objectUnderTest.getHelloWorld() == 'Hello World' - } - -} diff --git a/src/test/java/org/onap/cps/ncmp/dmi/rest/controller/TestController.java b/src/test/java/org/onap/cps/ncmp/dmi/rest/controller/TestController.java new file mode 100644 index 00000000..5240e239 --- /dev/null +++ b/src/test/java/org/onap/cps/ncmp/dmi/rest/controller/TestController.java @@ -0,0 +1,35 @@ +/* + * ============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.rest.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + + @GetMapping("/test") + ResponseEntity test() { + return new ResponseEntity<>(HttpStatus.OK); + } +} diff --git a/src/test/java/org/onap/cps/ncmp/rest/controller/TestController.java b/src/test/java/org/onap/cps/ncmp/rest/controller/TestController.java deleted file mode 100644 index 5d6f7b90..00000000 --- a/src/test/java/org/onap/cps/ncmp/rest/controller/TestController.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============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.rest.controller; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TestController { - - @GetMapping("/test") - ResponseEntity test() { - return new ResponseEntity<>(HttpStatus.OK); - } -} -- cgit 1.2.3-korg