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