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 static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertThat;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.when;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Optional;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.sdc.be.components.csar.CsarInfo;
38 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.be.test.util.TestResourcesHandler;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @RunWith(MockitoJUnitRunner.class)
46 public class SoftwareInformationBusinessLogicTest {
48 private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationBusinessLogicTest.class);
49 private final String softwareInformationPath = "Artifact/Informational/SW_INFORMATION";
52 private PropertyBusinessLogic propertyBusinessLogic;
54 private CsarInfo csarInfo;
56 private Resource resource;
58 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
62 softwareInformationBusinessLogic = new SoftwareInformationBusinessLogic(propertyBusinessLogic);
66 private void mockCsarInfo() {
67 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml");
68 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.of(softwareInformationPath));
72 public void testRemoveSoftwareInformationFile() {
73 boolean result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
74 assertThat("The software information file should be removed", result, is(true));
75 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
76 result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
77 assertThat("The software information file should not be removed", result, is(false));
81 public void testSetSoftwareInformation() throws BusinessLogicException {
82 final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
83 mockResource(propertyDefinition);
84 when(propertyBusinessLogic.updateComponentProperty(Mockito.any(), Mockito.any()))
85 .thenReturn(propertyDefinition);
86 final Optional<PropertyDefinition> actualPropertyDefinition = softwareInformationBusinessLogic
87 .setSoftwareInformation(resource, csarInfo);
88 assertThat("The updated property should be present", actualPropertyDefinition.isPresent(), is(true));
89 actualPropertyDefinition.ifPresent(propertyDefinition1 -> {
90 assertThat("The updated property should have the expected name", propertyDefinition1.getName(),
91 is("software_versions"));
92 assertThat("The updated property should have the expected value", propertyDefinition1.getValue(),
93 is("[\"version1\",\"version2\"]"));
98 public void testSetSoftwareInformationWithInvalidArtifact() throws BusinessLogicException {
100 final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
101 mockResource(propertyDefinition);
102 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-corrupt.yaml");
104 assertNotPresentPropertyDefinition();
107 mockCsarFileMap("artifacts/pnfSoftwareInformation/invalid.yaml");
109 assertNotPresentPropertyDefinition();
112 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-1.yaml");
114 assertNotPresentPropertyDefinition();
117 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-2.yaml");
119 assertNotPresentPropertyDefinition();
122 mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-3.yaml");
124 assertNotPresentPropertyDefinition();
127 private void assertNotPresentPropertyDefinition() throws BusinessLogicException {
128 final Optional<PropertyDefinition> actualPropertyDefinition =
129 softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
130 assertThat("The updated property should not be present",
131 actualPropertyDefinition.isPresent(), is(false));
135 public void testSetSoftwareInformationWithNoResourceSoftwareInformationProperty() throws BusinessLogicException {
137 assertNotPresentPropertyDefinition();
141 public void testSetSoftwareInformationWithNoCsarSoftwareInformation() throws BusinessLogicException {
143 when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
145 assertNotPresentPropertyDefinition();
148 private void mockCsarFileMap(final String softwareInformationArtifactPath) {
149 final byte[] softwareInformationFile;
151 softwareInformationFile = TestResourcesHandler.getResourceAsByteArray(softwareInformationArtifactPath);
152 } catch (final IOException e) {
153 final String errorMsg = "Could not find software information artifact " + softwareInformationArtifactPath;
154 LOGGER.error(errorMsg, e);
158 final HashMap<String, byte[]> csarFileMap = new HashMap<>();
159 csarFileMap.put(softwareInformationPath, softwareInformationFile);
160 when(csarInfo.getCsar()).thenReturn(csarFileMap);
163 private PropertyDefinition mockSoftwareInformationPropertyDefinition() {
164 final PropertyDefinition propertyDefinition = new PropertyDefinition();
165 propertyDefinition.setName("software_versions");
166 return propertyDefinition;
169 private void mockResource(final PropertyDefinition... propertyDefinition) {
170 when(resource.getProperties()).thenReturn(Arrays.asList(propertyDefinition));