Add support for loading VNF Catalog XML files
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / catalog / TestVnfCatalogArtifactHandler.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.entity.catalog;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mockito;
42 import org.onap.aai.modelloader.config.ModelLoaderConfig;
43 import org.onap.aai.modelloader.entity.Artifact;
44 import org.onap.aai.modelloader.entity.ArtifactType;
45 import org.onap.aai.modelloader.restclient.AaiRestClient;
46 import org.onap.aai.restclient.client.OperationResult;
47
48 public class TestVnfCatalogArtifactHandler {
49
50     protected static String CONFIG_FILE = "model-loader.properties";
51
52     private AaiRestClient mockRestClient = mock(AaiRestClient.class);
53
54     /**
55      * Update A&AI with 4 images, 2 of which already exist.
56      * 
57      * @throws Exception
58      */
59     @Test
60     public void testUpdateVnfImages() throws Exception {
61         // GET operation
62         OperationResult mockGetResp = mock(OperationResult.class);
63
64         // @formatter:off
65         when(mockGetResp.getResultCode())
66                 .thenReturn(Response.Status.OK.getStatusCode())
67                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
68                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
69                 .thenReturn(Response.Status.OK.getStatusCode());
70         // @formatter:on
71
72         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
73                 .thenReturn(mockGetResp);
74         mockPutOperations();
75
76         // Example VNF Catalog XML
77         VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
78         assertThat(handler.pushArtifacts(createVnfCatalogArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
79                 is(true));
80
81         assertPutOperationsSucceeded();
82     }
83
84     @Test
85     public void testUpdateVnfImagesFromXml() throws Exception {
86         // GET operation
87         OperationResult mockGetResp = mock(OperationResult.class);
88
89         // @formatter:off
90         when(mockGetResp.getResultCode())
91                 .thenReturn(Response.Status.OK.getStatusCode())
92                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
93                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
94                 .thenReturn(Response.Status.OK.getStatusCode());
95         // @formatter:on
96
97         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
98                 .thenReturn(mockGetResp);
99         mockPutOperations();
100
101         // Example VNF Catalog XML
102         VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
103         assertThat(
104                 handler.pushArtifacts(createVnfCatalogXmlArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
105                 is(true));
106
107         // Only two of the VNF images should be pushed
108         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
109         AaiRestClient client = Mockito.verify(mockRestClient, Mockito.times(2));
110         client.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class));
111         assertThat(argument.getAllValues().size(), is(2));
112         assertThat(argument.getAllValues().get(0), containsString("5.2.5"));
113         assertThat(argument.getAllValues().get(0), containsString("VM00"));
114         assertThat(argument.getAllValues().get(1), containsString("5.2.4"));
115         assertThat(argument.getAllValues().get(1), containsString("VM00"));
116     }
117
118     private ModelLoaderConfig createConfig() {
119         Properties configProperties = new Properties();
120         try {
121             configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
122         } catch (IOException e) {
123             fail();
124         }
125         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
126         return config;
127     }
128
129     /**
130      * Example VNF Catalog based on JSON data (returned by Babel)
131      * 
132      * @return test Artifacts
133      * @throws IOException
134      * @throws UnsupportedEncodingException
135      */
136     private List<Artifact> createVnfCatalogArtifact() throws IOException, UnsupportedEncodingException {
137         String examplePath = "src/test/resources/imagedataexample.json";
138         byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
139         List<Artifact> artifacts = new ArrayList<Artifact>();
140         artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
141         return artifacts;
142     }
143
144     /**
145      * Example VNF Catalog based on VNF_CATALOG XML
146      * 
147      * @return test Artifacts
148      * @throws IOException
149      * @throws UnsupportedEncodingException
150      */
151     private List<Artifact> createVnfCatalogXmlArtifact() throws IOException, UnsupportedEncodingException {
152         byte[] encoded = Files.readAllBytes(Paths.get("src/test/resources/xmlFiles/fortigate.xml"));
153         List<Artifact> artifacts = new ArrayList<Artifact>();
154         artifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, new String(encoded, "utf-8")));
155         return artifacts;
156     }
157
158     /**
159      * Always return CREATED (success) for a PUT operation.
160      */
161     private void mockPutOperations() {
162         OperationResult mockPutResp = mock(OperationResult.class);
163         when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
164         when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
165                 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
166     }
167
168     private void assertPutOperationsSucceeded() {
169         // Only two of the VNF images should be pushed
170         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
171         AaiRestClient mockedClient = Mockito.verify(mockRestClient, Mockito.times(2));
172         mockedClient.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(),
173                 Mockito.any(MediaType.class));
174         assertThat(argument.getAllValues().get(0), containsString("3.16.9"));
175         assertThat(argument.getAllValues().get(0), containsString("VM00"));
176         assertThat(argument.getAllValues().get(1), containsString("3.16.1"));
177         assertThat(argument.getAllValues().get(1), containsString("VM01"));
178     }
179 }