Catalog alignment
[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 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;
35
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.Optional;
40
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;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class SoftwareInformationBusinessLogicTest {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationBusinessLogicTest.class);
50     private final String softwareInformationPath = "Artifact/Informational/SW_INFORMATION";
51
52     @Mock
53     private PropertyBusinessLogic propertyBusinessLogic;
54     @Mock
55     private CsarInfo csarInfo;
56     @Mock
57     private Resource resource;
58
59     private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
60
61     @Before
62     public void setup() {
63         softwareInformationBusinessLogic = new SoftwareInformationBusinessLogic(propertyBusinessLogic);
64         mockCsarInfo();
65     }
66
67     private void mockCsarInfo() {
68         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml");
69         when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.of(softwareInformationPath));
70     }
71
72     @Test
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));
79     }
80
81     @Test
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\"]"));
95         });
96     }
97
98     @Test
99     public void testSetSoftwareInformationWithInvalidArtifact() throws BusinessLogicException {
100         //given
101         final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
102         mockResource(propertyDefinition);
103         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-corrupt.yaml");
104         //when and then
105         assertNotPresentPropertyDefinition();
106
107         //given
108         mockCsarFileMap("artifacts/pnfSoftwareInformation/invalid.yaml");
109         //when and then
110         assertNotPresentPropertyDefinition();
111
112         //given
113         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-1.yaml");
114         //when and then
115         assertNotPresentPropertyDefinition();
116
117         //given
118         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-2.yaml");
119         //when and then
120         assertNotPresentPropertyDefinition();
121
122         //given
123         mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-3.yaml");
124         //when and then
125         assertNotPresentPropertyDefinition();
126     }
127
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));
133     }
134
135     @Test
136     public void testSetSoftwareInformationWithNoResourceSoftwareInformationProperty() throws BusinessLogicException {
137         //when and then
138         assertNotPresentPropertyDefinition();
139     }
140
141     @Test
142     public void testSetSoftwareInformationWithNoCsarSoftwareInformation() throws BusinessLogicException {
143         //given
144         when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
145         //when and then
146         assertNotPresentPropertyDefinition();
147     }
148
149     private void mockCsarFileMap(final String softwareInformationArtifactPath) {
150         final byte[] softwareInformationFile;
151         try {
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);
156             fail(errorMsg);
157             return;
158         }
159         final HashMap<String, byte[]> csarFileMap = new HashMap<>();
160         csarFileMap.put(softwareInformationPath, softwareInformationFile);
161         when(csarInfo.getCsar()).thenReturn(csarFileMap);
162     }
163
164     private PropertyDefinition mockSoftwareInformationPropertyDefinition() {
165         final PropertyDefinition propertyDefinition = new PropertyDefinition();
166         propertyDefinition.setName("software_versions");
167         return propertyDefinition;
168     }
169
170     private void mockResource(final PropertyDefinition... propertyDefinition) {
171         when(resource.getProperties()).thenReturn(Arrays.asList(propertyDefinition));
172     }
173
174 }