Merge "Update model-loader dependencies"
[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.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.Test;
31 import org.onap.aai.babel.service.data.BabelArtifact;
32 import org.onap.aai.modelloader.entity.Artifact;
33 import org.onap.aai.modelloader.entity.ArtifactType;
34 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
35 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
36 import org.onap.aai.modelloader.util.ArtifactTestUtils;
37 import org.onap.sdc.api.notification.IArtifactInfo;
38 import org.onap.sdc.api.notification.INotificationData;
39
40 /**
41  * Tests {@link BabelArtifactConverter}.
42  */
43 public class TestBabelArtifactConverter {
44
45     @Test(expected = NullPointerException.class)
46     public void convert_nullToscaFiles() throws BabelArtifactParsingException {
47         new BabelArtifactConverter().convertToModel(null);
48         fail("An instance of ArtifactGenerationException should have been thrown");
49     }
50
51     @Test
52     public void testEmptyToscaFiles() throws BabelArtifactParsingException {
53         assertTrue("Nothing should have been returned",
54                 new BabelArtifactConverter().convertToModel(new ArrayList<>()).isEmpty());
55     }
56
57     @Test(expected = BabelArtifactParsingException.class)
58     public void testInvalidXml() throws IOException, BabelArtifactParsingException {
59         byte[] problemXml =
60                 "<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>"
61                         .getBytes();
62
63         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
64
65         List<BabelArtifact> toscaArtifacts = setupTest(problemXml, data);
66
67         new BabelArtifactConverter().convertToModel(toscaArtifacts);
68         fail("An instance of ModelArtifactParsingException should have been thrown");
69     }
70
71     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
72         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
73         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
74
75         BabelArtifact xmlArtifact =
76                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
77         toscaArtifacts.add(xmlArtifact);
78
79         return toscaArtifacts;
80     }
81
82     @Test
83     public void convert_singleResourceFile() throws BabelArtifactParsingException, IOException {
84         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
85
86         byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
87         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
88
89         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
90
91         assertEquals("There should have been 1 artifact", 1, modelArtifacts.size());
92         assertEquals(new String(xml), modelArtifacts.get(0).getPayload());
93         assertEquals(ArtifactType.MODEL, modelArtifacts.get(0).getType());
94     }
95 }