From 54d10a74fdd8cfcb89fd1c0ab91abefdf0602367 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 5 Apr 2022 13:54:32 +0100 Subject: Improve branch coverage - update to oparent 3.3.0 to allow for checkstyle @SupressWarnings (not used in the end) - refactor code to minimize unused branches (no more switch-statements :-)) - Added test for neccessary but uncovered branches Issue-ID: CPS-475 Signed-off-by: ToineSiebelink Change-Id: I03c7355a7e9d19f57523a65fbff45c9d8f1c9e07 --- .../rest/controller/ControllerSecuritySpec.groovy | 13 +++++++++++- .../rest/controller/DmiRestControllerSpec.groovy | 24 ++++++++++------------ 2 files changed, 23 insertions(+), 14 deletions(-) (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 index 14ab7147..8b13fc79 100644 --- 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ package org.onap.cps.ncmp.dmi.rest.controller +import org.onap.cps.ncmp.dmi.config.WebSecurityConfig + import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.beans.factory.annotation.Autowired @@ -60,4 +62,13 @@ class ControllerSecuritySpec extends Specification { then: 'HTTP Unauthorized status code is returned' assert response.status == HttpStatus.UNAUTHORIZED.value() } + + def 'Security Config #scenario permit URIs'() { + expect: 'can create a web security configuration' + new WebSecurityConfig(permitUris,'user','password') + where: 'the following string of permit URIs is provided' + scenario | permitUris + 'with' | 'a,b' + 'without' | '' + } } 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 index b42eb4d2..2f200cfa 100644 --- 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,6 @@ package org.onap.cps.ncmp.dmi.rest.controller -import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.dmi.TestUtils import org.onap.cps.ncmp.dmi.exception.DmiException import org.onap.cps.ncmp.dmi.exception.ModuleResourceNotFoundException @@ -36,7 +35,6 @@ import org.spockframework.spring.SpringBean 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.context.annotation.Import import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.security.test.context.support.WithMockUser @@ -56,15 +54,14 @@ import static org.springframework.http.HttpStatus.OK @WebMvcTest(DmiRestController) @WithMockUser -@Import(ObjectMapper) class DmiRestControllerSpec extends Specification { - @SpringBean - DmiService mockDmiService = Mock() - @Autowired private MockMvc mvc + @SpringBean + DmiService mockDmiService = Mock() + @Value('${rest.api.dmi-base-path}/v1') def basePathV1 @@ -95,18 +92,19 @@ class DmiRestControllerSpec extends Specification { def getModuleUrl = "$basePathV1/ch/node1/modules" and: 'given request body and get modules for cm-handle throws #exceptionClass' def json = '{"cmHandleProperties" : {}}' - mockDmiService.getModulesForCmHandle('node1') >> { throw Mock(exceptionClass) } + mockDmiService.getModulesForCmHandle('node1') >> { throw exception } when: 'post is invoked' def response = mvc.perform( post(getModuleUrl) .contentType(MediaType.APPLICATION_JSON).content(json)) .andReturn().response then: 'response status is #expectedResponse' - response.status == expectedResponse + response.status == expectedResponse.value() where: 'the scenario is #scenario' - scenario | exceptionClass || expectedResponse - 'dmi service exception' | DmiException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() - 'no modules found' | ModulesNotFoundException.class || HttpStatus.NOT_FOUND.value() - 'any other runtime exception' | RuntimeException.class || HttpStatus.INTERNAL_SERVER_ERROR.value() + scenario | exception || expectedResponse + 'dmi service exception' | new DmiException('','') || HttpStatus.INTERNAL_SERVER_ERROR + 'no modules found' | new ModulesNotFoundException('','') || HttpStatus.NOT_FOUND + 'any other runtime exception' | new RuntimeException() || HttpStatus.INTERNAL_SERVER_ERROR + 'runtime exception with cause' | new RuntimeException('', new RuntimeException()) || HttpStatus.INTERNAL_SERVER_ERROR } def 'Register given list.'() { -- cgit 1.2.3-korg