Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / TestBabelArtifactConverter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.notification;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.junit.jupiter.api.Assertions.fail;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.jupiter.api.Test;
33 import org.onap.aai.babel.service.data.BabelArtifact;
34 import org.onap.aai.modelloader.entity.Artifact;
35 import org.onap.aai.modelloader.entity.ArtifactType;
36 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
37 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
38 import org.onap.aai.modelloader.util.ArtifactTestUtils;
39 import org.onap.sdc.api.notification.IArtifactInfo;
40 import org.onap.sdc.api.notification.INotificationData;
41
42 /**
43  * Tests {@link BabelArtifactConverter}.
44  */
45 public class TestBabelArtifactConverter {
46
47     @Test
48     public void convert_nullToscaFiles() throws BabelArtifactParsingException {
49         assertThrows(NullPointerException.class, () -> {
50             new BabelArtifactConverter().convertToModel(null);
51             fail("An instance of ArtifactGenerationException should have been thrown");
52         });
53     }
54
55     @Test
56     public void testEmptyToscaFiles() throws BabelArtifactParsingException {
57         assertTrue(new BabelArtifactConverter().convertToModel(new ArrayList<>()).isEmpty(),
58                 "Nothing should have been returned");
59     }
60
61     @Test
62     public void testInvalidXml() throws IOException, BabelArtifactParsingException {
63         assertThrows(BabelArtifactParsingException.class, () -> {
64             byte[] problemXml =
65                     "<model xmlns=\"http://org.openecomp.aai.inventory/v10\"><rubbish>This is some xml that should cause the model artifact parser to throw an erorr</rubbish></model>"
66                             .getBytes();
67
68             INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
69
70             List<BabelArtifact> toscaArtifacts = setupTest(problemXml, data);
71
72             new BabelArtifactConverter().convertToModel(toscaArtifacts);
73             fail("An instance of ModelArtifactParsingException should have been thrown");
74         });
75     }
76
77     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
78         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
79         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
80
81         BabelArtifact xmlArtifact =
82                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
83         toscaArtifacts.add(xmlArtifact);
84
85         return toscaArtifacts;
86     }
87
88     @Test
89     public void convert_singleResourceFile() throws BabelArtifactParsingException, IOException {
90         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
91
92         byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
93         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
94
95         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
96
97         assertEquals(1, modelArtifacts.size(), "There should have been 1 artifact");
98         assertEquals(new String(xml), modelArtifacts.get(0).getPayload());
99         assertEquals(ArtifactType.MODEL, modelArtifacts.get(0).getType());
100     }
101 }