99bccfc566e456377ec349e240f42e7a926ebac7
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / SoftwareInformationBusinessLogicTest.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.components.impl;
21
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;
26
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class SoftwareInformationBusinessLogicTest {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationBusinessLogicTest.class);
49     private final String softwareInformationPath = "Artifact/Informational/SW_INFORMATION";
50
51     @Mock
52     private PropertyBusinessLogic propertyBusinessLogic;
53     @Mock
54     private CsarInfo csarInfo;
55     @Mock
56     private Resource resource;
57
58     private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
59
60     @Before
61     public void setup() {
62         softwareInformationBusinessLogic = new SoftwareInformationBusinessLogic(propertyBusinessLogic);
63         mockCsarInfo();
64     }
65
66     private void mockCsarInfo() {
67         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml");
68         when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.of(softwareInformationPath));
69     }
70
71     @Test
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));
78     }
79
80     @Test
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\"]"));
94         });
95     }
96
97     @Test
98     public void testSetSoftwareInformationWithInvalidArtifact() throws BusinessLogicException {
99         //given
100         final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
101         mockResource(propertyDefinition);
102         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-corrupt.yaml");
103         //when and then
104         assertNotPresentPropertyDefinition();
105
106         //given
107         mockCsarFileMap("artifacts/pnfSoftwareInformation/invalid.yaml");
108         //when and then
109         assertNotPresentPropertyDefinition();
110
111         //given
112         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-1.yaml");
113         //when and then
114         assertNotPresentPropertyDefinition();
115
116         //given
117         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-2.yaml");
118         //when and then
119         assertNotPresentPropertyDefinition();
120
121         //given
122         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-3.yaml");
123         //when and then
124         assertNotPresentPropertyDefinition();
125     }
126
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));
132     }
133
134     @Test
135     public void testSetSoftwareInformationWithNoResourceSoftwareInformationProperty() throws BusinessLogicException {
136         //when and then
137         assertNotPresentPropertyDefinition();
138     }
139
140     @Test
141     public void testSetSoftwareInformationWithNoCsarSoftwareInformation() throws BusinessLogicException {
142         //given
143         when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
144         //when and then
145         assertNotPresentPropertyDefinition();
146     }
147
148     private void mockCsarFileMap(final String softwareInformationArtifactPath) {
149         final byte[] softwareInformationFile;
150         try {
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);
155             fail(errorMsg);
156             return;
157         }
158         final HashMap<String, byte[]> csarFileMap = new HashMap<>();
159         csarFileMap.put(softwareInformationPath, softwareInformationFile);
160         when(csarInfo.getCsar()).thenReturn(csarFileMap);
161     }
162
163     private PropertyDefinition mockSoftwareInformationPropertyDefinition() {
164         final PropertyDefinition propertyDefinition = new PropertyDefinition();
165         propertyDefinition.setName("software_versions");
166         return propertyDefinition;
167     }
168
169     private void mockResource(final PropertyDefinition... propertyDefinition) {
170         when(resource.getProperties()).thenReturn(Arrays.asList(propertyDefinition));
171     }
172
173 }