2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.components.impl;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.openecomp.sdc.be.components.csar.CsarInfo;
29 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
30 import org.openecomp.sdc.be.model.PropertyDefinition;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.test.util.TestResourcesHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.Optional;
41 import static org.hamcrest.CoreMatchers.is;
42 import static org.junit.Assert.assertThat;
43 import static org.junit.Assert.fail;
44 import static org.mockito.Mockito.when;
46 @RunWith(MockitoJUnitRunner.class)
47 public class SoftwareInformationBusinessLogicTest {
49 private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationBusinessLogicTest.class);
50 private final String softwareInformationPath = "Artifact/Informational/SW_INFORMATION";
53 private PropertyBusinessLogic propertyBusinessLogic;
55 private CsarInfo csarInfo;
57 private Resource resource;
59 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
63 softwareInformationBusinessLogic = new SoftwareInformationBusinessLogic(propertyBusinessLogic);
67 private void mockCsarInfo() {
68 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml");
69 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.of(softwareInformationPath));
73 public void testRemoveSoftwareInformationFile() {
74 boolean result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
75 assertThat("The software information file should be removed", result, is(true));
76 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
77 result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
78 assertThat("The software information file should not be removed", result, is(false));
82 public void testSetSoftwareInformation() throws BusinessLogicException {
83 final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
84 mockResource(propertyDefinition);
85 when(propertyBusinessLogic.updateComponentProperty(Mockito.any(), Mockito.any()))
86 .thenReturn(propertyDefinition);
87 final Optional<PropertyDefinition> actualPropertyDefinition = softwareInformationBusinessLogic
88 .setSoftwareInformation(resource, csarInfo);
89 assertThat("The updated property should be present", actualPropertyDefinition.isPresent(), is(true));
90 actualPropertyDefinition.ifPresent(propertyDefinition1 -> {
91 assertThat("The updated property should have the expected name", propertyDefinition1.getName(),
92 is("software_versions"));
93 assertThat("The updated property should have the expected value", propertyDefinition1.getValue(),
94 is("[\"version1\",\"version2\"]"));
99 public void testSetSoftwareInformationWithInvalidArtifact() throws BusinessLogicException {
101 final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
102 mockResource(propertyDefinition);
103 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-corrupt.yaml");
105 assertNotPresentPropertyDefinition();
108 mockCsarFileMap("artifacts/pnfSoftwareInformation/invalid.yaml");
110 assertNotPresentPropertyDefinition();
113 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-1.yaml");
115 assertNotPresentPropertyDefinition();
118 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-2.yaml");
120 assertNotPresentPropertyDefinition();
123 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-3.yaml");
125 assertNotPresentPropertyDefinition();
128 private void assertNotPresentPropertyDefinition() throws BusinessLogicException {
129 final Optional<PropertyDefinition> actualPropertyDefinition =
130 softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
131 assertThat("The updated property should not be present",
132 actualPropertyDefinition.isPresent(), is(false));
136 public void testSetSoftwareInformationWithNoResourceSoftwareInformationProperty() throws BusinessLogicException {
138 assertNotPresentPropertyDefinition();
142 public void testSetSoftwareInformationWithNoCsarSoftwareInformation() throws BusinessLogicException {
144 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
146 assertNotPresentPropertyDefinition();
149 private void mockCsarFileMap(final String softwareInformationArtifactPath) {
150 final byte[] softwareInformationFile;
152 softwareInformationFile = TestResourcesHandler.getResourceAsByteArray(softwareInformationArtifactPath);
153 } catch (final IOException e) {
154 final String errorMsg = "Could not find software information artifact " + softwareInformationArtifactPath;
155 LOGGER.error(errorMsg, e);
159 final HashMap<String, byte[]> csarFileMap = new HashMap<>();
160 csarFileMap.put(softwareInformationPath, softwareInformationFile);
161 when(csarInfo.getCsar()).thenReturn(csarFileMap);
164 private PropertyDefinition mockSoftwareInformationPropertyDefinition() {
165 final PropertyDefinition propertyDefinition = new PropertyDefinition();
166 propertyDefinition.setName("software_versions");
167 return propertyDefinition;
170 private void mockResource(final PropertyDefinition... propertyDefinition) {
171 when(resource.getProperties()).thenReturn(Arrays.asList(propertyDefinition));