diff options
author | 2023-07-26 17:49:02 +0100 | |
---|---|---|
committer | 2023-07-31 08:57:30 +0100 | |
commit | e3cdc8a0591553da6d022337fa69c8dd507510f6 (patch) | |
tree | 6c72936bc39e00d2b9821def0622e83165c1cb8d /cps-service/src/main | |
parent | 92bf624e75673f8027ba48bf4f8c2d28b3b01552 (diff) |
Increase code coverage in cps-service module
- After last rebase I had to remove 3 unused recent cloud eventd specific exceptions/constructors
- Moved the only used new exception from SPI to the relevant util package
(please NOTE not all exceptions belong in SPI and always question need for new exception
when there is no specific handling, try to use standard or existign CPS exception instead!)
- Increased cps-service module (line) coverage from 95 to 100%
- Added tests for missing exceptions (handling i.e. thrown up)
- Removed incorrect SPI defined OperationNotYetSupportedException
(replaced with standard java exception instead)
- Fixed some legacy issues with existign test classes I modified
(unnecessary setup, conventions etc)
- Increased coverage for DataNodeBuilder
- Added or modified test to include more spi models
- Added tests for Hazelcast Configs
- Added more tests for json object mapper
- Added test and fixed error handling in YangUtils/XmlFileUtils
(it was incorrectly converting a config exception to a data validation exception)
Issue-ID: CPS-475
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: I5852ba01bc5b33ae361b8f29daae9868f05baa35
Diffstat (limited to 'cps-service/src/main')
7 files changed, 35 insertions, 195 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java index 38f898827..696fd60f8 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java +++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (c) 2021-2022 Bell Canada. - * Modifications Copyright (c) 2022 Nordix Foundation + * Modifications Copyright (c) 2022-2023 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,6 +28,7 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; import lombok.AllArgsConstructor; +import lombok.SneakyThrows; import org.onap.cps.api.CpsDataService; import org.onap.cps.event.model.Content; import org.onap.cps.event.model.CpsDataUpdatedEvent; @@ -44,22 +45,9 @@ import org.springframework.stereotype.Component; @AllArgsConstructor(onConstructor = @__(@Lazy)) public class CpsDataUpdatedEventFactory { - private static final URI EVENT_SCHEMA; - private static final URI EVENT_SOURCE; - private static final String EVENT_TYPE = "org.onap.cps.data-updated-event"; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); - static { - try { - EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1"); - EVENT_SOURCE = new URI("urn:cps:org.onap.cps"); - } catch (final URISyntaxException e) { - // As it is fixed string, I don't expect to see this error - throw new IllegalArgumentException(e); - } - } - @Lazy private final CpsDataService cpsDataService; @@ -82,14 +70,17 @@ public class CpsDataUpdatedEventFactory { return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation); } - private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode, - final OffsetDateTime observedTimestamp, final Operation operation) { - final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent(); + @SneakyThrows(URISyntaxException.class) + private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, + final DataNode dataNode, + final OffsetDateTime observedTimestamp, + final Operation operation) { + final CpsDataUpdatedEvent cpsDataUpdatedEvent = new CpsDataUpdatedEvent(); cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation)); cpsDataUpdatedEvent.withId(UUID.randomUUID().toString()); - cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA); - cpsDataUpdatedEvent.withSource(EVENT_SOURCE); - cpsDataUpdatedEvent.withType(EVENT_TYPE); + cpsDataUpdatedEvent.withSchema(new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1")); + cpsDataUpdatedEvent.withSource(new URI("urn:cps:org.onap.cps")); + cpsDataUpdatedEvent.withType("org.onap.cps.data-updated-event"); return cpsDataUpdatedEvent; } diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/CloudEventConstructionException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/CloudEventConstructionException.java deleted file mode 100644 index 1d520e7b0..000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/CloudEventConstructionException.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Pantheon.tech - * Modifications Copyright (C) 2020 Bell Canada - * Modifications Copyright (C) 2020-2023 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.spi.exceptions; - -public class CloudEventConstructionException extends CpsException { - - private static final long serialVersionUID = 7747941311132087621L; - - /** - * Constructor. - * - * @param message the error message - * @param details the error details - */ - public CloudEventConstructionException(final String message, final String details) { - super(message, details); - } - - /** - * Constructor. - * - * @param message the error message - * @param details the error details - * @param cause the error cause - */ - public CloudEventConstructionException(final String message, final String details, final Throwable cause) { - super(message, details, cause); - } -} diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/OperationNotYetSupportedException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/OperationNotYetSupportedException.java deleted file mode 100644 index 6a4e2a098..000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/OperationNotYetSupportedException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 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.spi.exceptions; - -/** - * Operation Not Yet Supported Exception. - * Indicates the operation is not supported and has intention to be supported in the future. - */ - -public class OperationNotYetSupportedException extends CpsException { - - private static final long serialVersionUID = 1517903069236383746L; - - /** - * Constructor. - * - * @param details reason for the exception - */ - public OperationNotYetSupportedException(final String details) { - super("Operation Not Yet Supported Exception", details); - } -} diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SubscriptionOutcomeTypeNotFoundException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SubscriptionOutcomeTypeNotFoundException.java deleted file mode 100644 index 6b898e853..000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SubscriptionOutcomeTypeNotFoundException.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Pantheon.tech - * Modifications Copyright (C) 2020 Bell Canada - * Modifications Copyright (C) 2020-2023 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.spi.exceptions; - -public class SubscriptionOutcomeTypeNotFoundException extends CpsException { - - private static final long serialVersionUID = 7747941311132087621L; - - /** - * Constructor. - * - * @param message the error message - * @param details the error details - */ - public SubscriptionOutcomeTypeNotFoundException(final String message, final String details) { - super(message, details); - } - - /** - * Constructor. - * - * @param message the error message - * @param details the error details - * @param cause the error cause - */ - public SubscriptionOutcomeTypeNotFoundException(final String message, final String details, final Throwable cause) { - super(message, details, cause); - } -} diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java b/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java index e21293338..b040af5bb 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java +++ b/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java @@ -184,9 +184,8 @@ public class DataNodeBuilder { private DataNode buildFromContainerNode() { final Collection<DataNode> dataNodeCollection = buildCollectionFromContainerNode(); - if (!dataNodeCollection.iterator().hasNext()) { - throw new DataValidationException( - "Unsupported xpath: ", "Unsupported xpath as it is referring to one element"); + if (dataNodeCollection.isEmpty()) { + throw new DataValidationException("Unsupported Normalized Node", "No valid node found"); } return dataNodeCollection.iterator().next(); } @@ -278,5 +277,4 @@ public class DataNodeBuilder { } } - } diff --git a/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java b/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java index 09f2e16c6..98c7947e1 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java +++ b/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022 Deutsche Telekom AG + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +40,6 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import lombok.AccessLevel; import lombok.NoArgsConstructor; -import org.onap.cps.spi.exceptions.DataValidationException; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.w3c.dom.Document; @@ -102,10 +102,8 @@ public class XmlFileUtils { final Map<String, String> rootNodeProperty) throws IOException, SAXException, ParserConfigurationException, TransformerException { final DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); - final StringBuilder xmlStringBuilder = new StringBuilder(); - xmlStringBuilder.append(xmlContent); - final Document document = documentBuilder.parse( - new ByteArrayInputStream(xmlStringBuilder.toString().getBytes(StandardCharsets.UTF_8))); + final Document document = + documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8))); final Element root = document.getDocumentElement(); if (!root.getTagName().equals(rootNodeTagName) && !root.getTagName().equals(YangUtils.DATA_ROOT_NODE_TAG_NAME)) { @@ -143,22 +141,19 @@ public class XmlFileUtils { static Document addDataRootNode(final Element node, final String tagName, final String namespace, - final Map<String, String> rootNodeProperty) { - try { - final DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); - final Document document = documentBuilder.newDocument(); - final Element rootElement = document.createElementNS(namespace, tagName); - for (final Map.Entry<String, String> entry : rootNodeProperty.entrySet()) { - final Element propertyElement = document.createElement(entry.getKey()); - propertyElement.setTextContent(entry.getValue()); - rootElement.appendChild(propertyElement); - } - rootElement.appendChild(document.adoptNode(node)); - document.appendChild(rootElement); - return document; - } catch (final ParserConfigurationException exception) { - throw new DataValidationException("Can't parse XML", "XML can't be parsed", exception); + final Map<String, String> rootNodeProperty) + throws ParserConfigurationException { + final DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); + final Document document = documentBuilder.newDocument(); + final Element rootElement = document.createElementNS(namespace, tagName); + for (final Map.Entry<String, String> entry : rootNodeProperty.entrySet()) { + final Element propertyElement = document.createElement(entry.getKey()); + propertyElement.setTextContent(entry.getValue()); + rootElement.appendChild(propertyElement); } + rootElement.appendChild(document.adoptNode(node)); + document.appendChild(rootElement); + return document; } private static DocumentBuilderFactory getDocumentBuilderFactory() { diff --git a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java index deb5b0575..ca907148d 100644 --- a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java +++ b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java @@ -27,7 +27,6 @@ import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableMap; import io.micrometer.core.annotation.Timed; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -37,7 +36,6 @@ import java.util.Optional; import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.NoArgsConstructor; -import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.exceptions.ModelValidationException; import org.onap.cps.spi.model.ModuleReference; import org.opendaylight.yangtools.yang.common.Revision; @@ -45,7 +43,6 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; @@ -144,23 +141,20 @@ public final class YangTextSchemaSourceSetBuilder { final String resourceName = yangTextSchemaSource.getIdentifier().getName(); try { reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource)); - } catch (final IOException e) { - throw new CpsException("Failed to read yang resource.", - String.format("Exception occurred on reading resource %s.", resourceName), e); - } catch (final YangSyntaxErrorException e) { - throw new ModelValidationException("Yang resource is invalid.", - String.format( - "Yang syntax validation failed for resource %s:%n%s", resourceName, e.getMessage()), e); + } catch (final Exception exception) { + throw new ModelValidationException("Yang resource processing exception.", + String.format("Could not process resource %s:%n%s", resourceName, exception.getMessage()), + exception); } } try { return reactor.buildEffective(); - } catch (final ReactorException e) { + } catch (final ReactorException reactorException) { final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList()); Collections.sort(resourceNames); throw new ModelValidationException("Invalid schema set.", - String.format("Effective schema context build failed for resources %s.", resourceNames.toString()), - e); + String.format("Effective schema context build failed for resources %s.", resourceNames), + reactorException); } } |