summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/SoftwareInformationBusinessLogicTest.java
blob: 7a90991b185bf616ffcba68c61e6ffc701adbafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.openecomp.sdc.be.components.impl;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.csar.CsarInfo;
import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.test.util.TestResourcesHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class SoftwareInformationBusinessLogicTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationBusinessLogicTest.class);
    private final String softwareInformationPath = "Artifact/Informational/SW_INFORMATION";

    @Mock
    private PropertyBusinessLogic propertyBusinessLogic;
    @Mock
    private CsarInfo csarInfo;
    @Mock
    private Resource resource;

    private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;

    @Before
    public void setup() {
        softwareInformationBusinessLogic = new SoftwareInformationBusinessLogic(propertyBusinessLogic);
        mockCsarInfo();
    }

    private void mockCsarInfo() {
        mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml");
        when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.of(softwareInformationPath));
    }

    @Test
    public void testRemoveSoftwareInformationFile() {
        boolean result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
        assertThat("The software information file should be removed", result, is(true));
        when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
        result = softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo);
        assertThat("The software information file should not be removed", result, is(false));
    }

    @Test
    public void testSetSoftwareInformation() throws BusinessLogicException {
        final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
        mockResource(propertyDefinition);
        when(propertyBusinessLogic.updateComponentProperty(Mockito.any(), Mockito.any()))
            .thenReturn(propertyDefinition);
        final Optional<PropertyDefinition> actualPropertyDefinition = softwareInformationBusinessLogic
            .setSoftwareInformation(resource, csarInfo);
        assertThat("The updated property should be present", actualPropertyDefinition.isPresent(), is(true));
        actualPropertyDefinition.ifPresent(propertyDefinition1 -> {
            assertThat("The updated property should have the expected name", propertyDefinition1.getName(),
                is("software_versions"));
            assertThat("The updated property should have the expected value", propertyDefinition1.getValue(),
                is("[\"version1\",\"version2\"]"));
        });
    }

    @Test
    public void testSetSoftwareInformationWithInvalidArtifact() throws BusinessLogicException {
        //given
        final PropertyDefinition propertyDefinition = mockSoftwareInformationPropertyDefinition();
        mockResource(propertyDefinition);
        mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-corrupt.yaml");
        //when and then
        assertNotPresentPropertyDefinition();

        //given
        mockCsarFileMap("artifacts/pnfSoftwareInformation/invalid.yaml");
        //when and then
        assertNotPresentPropertyDefinition();

        //given
        mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-1.yaml");
        //when and then
        assertNotPresentPropertyDefinition();

        //given
        mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-2.yaml");
        //when and then
        assertNotPresentPropertyDefinition();

        //given
        mockCsarFileMap("artifacts/pnfSoftwareInformation/pnf-sw-information-invalid-3.yaml");
        //when and then
        assertNotPresentPropertyDefinition();
    }

    private void assertNotPresentPropertyDefinition() throws BusinessLogicException {
        final Optional<PropertyDefinition> actualPropertyDefinition =
            softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
        assertThat("The updated property should not be present",
            actualPropertyDefinition.isPresent(), is(false));
    }

    @Test
    public void testSetSoftwareInformationWithNoResourceSoftwareInformationProperty() throws BusinessLogicException {
        //when and then
        assertNotPresentPropertyDefinition();
    }

    @Test
    public void testSetSoftwareInformationWithNoCsarSoftwareInformation() throws BusinessLogicException {
        //given
        when(csarInfo.getSoftwareInformationPath()).thenReturn(Optional.empty());
        //when and then
        assertNotPresentPropertyDefinition();
    }

    private void mockCsarFileMap(final String softwareInformationArtifactPath) {
        final byte[] softwareInformationFile;
        try {
            softwareInformationFile = TestResourcesHandler.getResourceAsByteArray(softwareInformationArtifactPath);
        } catch (final IOException e) {
            final String errorMsg = "Could not find software information artifact " + softwareInformationArtifactPath;
            LOGGER.error(errorMsg, e);
            fail(errorMsg);
            return;
        }
        final HashMap<String, byte[]> csarFileMap = new HashMap<>();
        csarFileMap.put(softwareInformationPath, softwareInformationFile);
        when(csarInfo.getCsar()).thenReturn(csarFileMap);
    }

    private PropertyDefinition mockSoftwareInformationPropertyDefinition() {
        final PropertyDefinition propertyDefinition = new PropertyDefinition();
        propertyDefinition.setName("software_versions");
        return propertyDefinition;
    }

    private void mockResource(final PropertyDefinition... propertyDefinition) {
        when(resource.getProperties()).thenReturn(Arrays.asList(propertyDefinition));
    }

}