diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2022-04-05 13:54:32 +0100 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2022-04-06 10:10:56 +0100 |
commit | 54d10a74fdd8cfcb89fd1c0ab91abefdf0602367 (patch) | |
tree | 25e5d499b9074577db69a04d844186dc2424c639 /src/main/java/org/onap | |
parent | 39450c583c097c3b083ca79a39c5f99a218f6cdc (diff) |
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 <toine.siebelink@est.tech>
Change-Id: I03c7355a7e9d19f57523a65fbff45c9d8f1c9e07
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r-- | src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java | 50 | ||||
-rw-r--r-- | src/main/java/org/onap/cps/ncmp/dmi/service/operation/SdncOperations.java | 39 |
2 files changed, 33 insertions, 56 deletions
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java index 5544aeb3..653ebf7f 100644 --- a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java +++ b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation + * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,10 +21,15 @@ package org.onap.cps.ncmp.dmi.rest.controller; +import static org.onap.cps.ncmp.dmi.model.DataAccessRequest.OperationEnum; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.validation.Valid; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.dmi.model.CmHandles; import org.onap.cps.ncmp.dmi.model.DataAccessRequest; @@ -44,15 +49,22 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("${rest.api.dmi-base-path}") @RestController @Slf4j +@RequiredArgsConstructor public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { - private DmiService dmiService; + private final DmiService dmiService; + + private final ObjectMapper objectMapper; - private ObjectMapper objectMapper; + private static final Map<OperationEnum, HttpStatus> operationToHttpStatusMap = new HashMap<>(6); - public DmiRestController(final DmiService dmiService, final ObjectMapper objectMapper) { - this.dmiService = dmiService; - this.objectMapper = objectMapper; + static { + operationToHttpStatusMap.put(null, HttpStatus.OK); + operationToHttpStatusMap.put(OperationEnum.READ, HttpStatus.OK); + operationToHttpStatusMap.put(OperationEnum.CREATE, HttpStatus.CREATED); + operationToHttpStatusMap.put(OperationEnum.PATCH, HttpStatus.OK); + operationToHttpStatusMap.put(OperationEnum.UPDATE, HttpStatus.OK); + operationToHttpStatusMap.put(OperationEnum.DELETE, HttpStatus.NO_CONTENT); } @Override @@ -133,7 +145,7 @@ public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { dataAccessRequest.getDataType(), dataAccessRequest.getData()); } - return new ResponseEntity<>(sdncResponse, getHttpStatus(dataAccessRequest)); + return new ResponseEntity<>(sdncResponse, operationToHttpStatusMap.get(dataAccessRequest.getOperation())); } private boolean isReadOperation(final @Valid DataAccessRequest dataAccessRequest) { @@ -141,30 +153,6 @@ public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { || dataAccessRequest.getOperation().equals(DataAccessRequest.OperationEnum.READ); } - private HttpStatus getHttpStatus(final DataAccessRequest dataAccessRequest) { - final HttpStatus httpStatus; - if (dataAccessRequest.getOperation() == null) { - httpStatus = HttpStatus.OK; - } else { - switch (dataAccessRequest.getOperation()) { - case CREATE: - httpStatus = HttpStatus.CREATED; - break; - case READ: - case UPDATE: - case PATCH: - httpStatus = HttpStatus.OK; - break; - case DELETE: - httpStatus = HttpStatus.NO_CONTENT; - break; - default: - httpStatus = HttpStatus.BAD_REQUEST; - } - } - return httpStatus; - } - private List<ModuleReference> convertRestObjectToJavaApiObject( final ModuleResourcesReadRequest moduleResourcesReadRequest) { return objectMapper diff --git a/src/main/java/org/onap/cps/ncmp/dmi/service/operation/SdncOperations.java b/src/main/java/org/onap/cps/ncmp/dmi/service/operation/SdncOperations.java index 7e2443e5..46a332cb 100644 --- a/src/main/java/org/onap/cps/ncmp/dmi/service/operation/SdncOperations.java +++ b/src/main/java/org/onap/cps/ncmp/dmi/service/operation/SdncOperations.java @@ -21,6 +21,8 @@ package org.onap.cps.ncmp.dmi.service.operation; +import static org.onap.cps.ncmp.dmi.model.DataAccessRequest.OperationEnum; + import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPathException; @@ -30,6 +32,7 @@ import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -62,6 +65,16 @@ public class SdncOperations { private static final int QUERY_PARAM_VALUE_INDEX = 1; private static final int QUERY_PARAM_NAME_INDEX = 0; + private static Map<OperationEnum, HttpMethod> operationToHttpMethodMap = new HashMap<>(5); + + static { + operationToHttpMethodMap.put(OperationEnum.READ, HttpMethod.GET); + operationToHttpMethodMap.put(OperationEnum.CREATE, HttpMethod.POST); + operationToHttpMethodMap.put(OperationEnum.PATCH, HttpMethod.PATCH); + operationToHttpMethodMap.put(OperationEnum.UPDATE, HttpMethod.PUT); + operationToHttpMethodMap.put(OperationEnum.DELETE, HttpMethod.DELETE); + } + private final SdncProperties sdncProperties; private final SdncRestconfClient sdncRestconfClient; private final String topologyUrlData; @@ -156,7 +169,7 @@ public class SdncOperations { final var getResourceDataUrl = prepareWriteUrl(nodeId, resourceId); final var httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.parseMediaType(contentType)); - final HttpMethod httpMethod = getHttpMethod(operation); + final HttpMethod httpMethod = operationToHttpMethodMap.get(operation); return sdncRestconfClient.httpOperationWithJsonData(httpMethod, getResourceDataUrl, requestData, httpHeaders); } @@ -230,30 +243,6 @@ public class SdncOperations { } } - private HttpMethod getHttpMethod(final DataAccessRequest.OperationEnum operation) { - HttpMethod httpMethod = null; - switch (operation) { - case READ: - httpMethod = HttpMethod.GET; - break; - case CREATE: - httpMethod = HttpMethod.POST; - break; - case PATCH: - httpMethod = HttpMethod.PATCH; - break; - case UPDATE: - httpMethod = HttpMethod.PUT; - break; - case DELETE: - httpMethod = HttpMethod.DELETE; - break; - default: - //unreachable code but checkstyle made me do this! - } - return httpMethod; - } - private String getTopologyUrlData() { return UriComponentsBuilder.fromUriString(TOPOLOGY_URL_TEMPLATE_DATA) .path("topology={topologyId}") |