aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java
blob: 2cac3c9b18f4035bc3de649512bf686dd9b1b036 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing Ltd.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.notification;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile;
import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.aai.babel.service.data.BabelArtifact;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.entity.Artifact;
import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
import org.onap.aai.modelloader.extraction.InvalidArchiveException;
import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
import org.onap.aai.modelloader.util.ArtifactTestUtils;
import org.onap.sdc.api.notification.INotificationData;
import org.springframework.test.util.ReflectionTestUtils;

/**
 * Tests {@link ArtifactDeploymentManager}.
 */
public class TestArtifactDeploymentManager {

    private static final String CONFIG_FILE = "model-loader.properties";
    private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";

    private Properties configProperties;
    private ArtifactDeploymentManager manager;

    private ModelArtifactHandler mockModelArtifactHandler;
    private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;

    @BeforeEach
    public void setup() throws IOException {
        configProperties = new Properties();
        configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));

        mockModelArtifactHandler = mock(ModelArtifactHandler.class);
        mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);

        manager = new ArtifactDeploymentManager(new ModelLoaderConfig(configProperties, null));

        ReflectionTestUtils.setField(manager, "modelArtifactHandler", mockModelArtifactHandler);
        ReflectionTestUtils.setField(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
    }

    @AfterEach
    public void tearDown() {
        configProperties = null;
        mockModelArtifactHandler = null;
        mockVnfCatalogArtifactHandler = null;
        manager = null;
    }

    @Test
    public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException {
        INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
        byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
        List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
        List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);

        when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
                .thenReturn(false);

        assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false));

        Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
                any());
        Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts),
                eq(data.getDistributionID()), any(), any());
        Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
                any());
        Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()),
                eq(data.getDistributionID()), any());
    }

    private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
        List<BabelArtifact> toscaArtifacts = new ArrayList<>();
        org.onap.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);

        BabelArtifact xmlArtifact =
                new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
        toscaArtifacts.add(xmlArtifact);

        return toscaArtifacts;
    }

    @Test
    public void deploy_catalogDeploymentsFailed()
            throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        INotificationData data = getNotificationDataWithCatalogFile();

        List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
        catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));

        when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
        when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
                .thenReturn(false);

        assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false));

        Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
                eq(data.getDistributionID()), any(), any());
        Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
                any(), any());
        Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
                any());
        Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
                eq(data.getDistributionID()), any());
    }

    @Test
    public void testNoArtifactsDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        doFailedCombinedTests(false, false);
    }

    @Test
    public void testModelsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        doFailedCombinedTests(false, true);
    }

    @Test
    public void testCatalogsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        doFailedCombinedTests(true, false);
    }

    private void doFailedCombinedTests(boolean modelsDeployed, boolean catalogsDeployed)
            throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        INotificationData data = getNotificationDataWithOneOfEach();
        byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
        List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
        List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);

        List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
        catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));

        when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
                .thenReturn(catalogsDeployed);
        when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
                .thenReturn(modelsDeployed);

        assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false));

        // Catalog artifacts are only pushed if models are successful.
        Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
                any());
        if (modelsDeployed) {
            Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
                    any(), any());
        }

        if (modelsDeployed && catalogsDeployed) {
            Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
            Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
        } else {
            if (modelsDeployed) {
                Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
                        eq(data.getDistributionID()), any());
                Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
                        eq(data.getDistributionID()), any());
            } else {
                Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
                        eq(data.getDistributionID()), any());
                Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
            }
        }
    }

    /**
     * Deploy both models and VNF images.
     * 
     * @throws IOException
     * @throws BabelArtifactParsingException
     * @throws InvalidArchiveException
     */
    @Test
    public void testDeploySuccess() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
        INotificationData data = getNotificationDataWithOneOfEach();
        byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
        List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
        List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);

        List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
        catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));

        when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
                .thenReturn(true);
        when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
                .thenReturn(true);

        assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true));

        Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
                any(), any());
        Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
                any());
        Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
        Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
    }
}