Refactor babel-related code to not update parameter values
[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.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
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40
41 import org.junit.jupiter.api.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Mockito;
44 import org.onap.aai.modelloader.config.ModelLoaderConfig;
45 import org.onap.aai.modelloader.entity.Artifact;
46 import org.onap.aai.modelloader.entity.ArtifactType;
47 import org.onap.aai.modelloader.restclient.AaiRestClient;
48 import org.onap.aai.restclient.client.OperationResult;
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         OperationResult mockGetResp = mock(OperationResult.class);
65
66         // @formatter:off
67         when(mockGetResp.getResultCode())
68                 .thenReturn(Response.Status.OK.getStatusCode())
69                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
70                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
71                 .thenReturn(Response.Status.OK.getStatusCode());
72         // @formatter:on
73
74         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
75                 .thenReturn(mockGetResp);
76         mockPutOperations();
77
78         // Example VNF Catalog XML
79         VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
80         assertThat(handler.pushArtifacts(createVnfCatalogArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
81                 is(true));
82
83         assertPutOperationsSucceeded();
84     }
85
86     @Test
87     public void testUpdateVnfImagesFromXml() throws Exception {
88         // GET operation
89         OperationResult mockGetResp = mock(OperationResult.class);
90
91         // @formatter:off
92         when(mockGetResp.getResultCode())
93                 .thenReturn(Response.Status.OK.getStatusCode())
94                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
95                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
96                 .thenReturn(Response.Status.OK.getStatusCode());
97         // @formatter:on
98
99         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
100                 .thenReturn(mockGetResp);
101         mockPutOperations();
102
103         // Example VNF Catalog XML
104         VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
105         assertThat(
106                 handler.pushArtifacts(createVnfCatalogXmlArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
107                 is(true));
108
109         // Only two of the VNF images should be pushed
110         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
111         AaiRestClient client = Mockito.verify(mockRestClient, Mockito.times(2));
112         client.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class));
113         assertThat(argument.getAllValues().size(), is(2));
114         assertThat(argument.getAllValues().get(0), containsString("5.2.5"));
115         assertThat(argument.getAllValues().get(0), containsString("VM00"));
116         assertThat(argument.getAllValues().get(1), containsString("5.2.4"));
117         assertThat(argument.getAllValues().get(1), containsString("VM00"));
118     }
119
120     private ModelLoaderConfig createConfig() {
121         Properties configProperties = new Properties();
122         try {
123             configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
124         } catch (IOException e) {
125             fail();
126         }
127         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
128         return config;
129     }
130
131     /**
132      * Example VNF Catalog based on JSON data (returned by Babel)
133      * 
134      * @return test Artifacts
135      * @throws IOException
136      * @throws UnsupportedEncodingException
137      */
138     private List<Artifact> createVnfCatalogArtifact() throws IOException, UnsupportedEncodingException {
139         String examplePath = "src/test/resources/imagedataexample.json";
140         byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
141         List<Artifact> artifacts = new ArrayList<Artifact>();
142         artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
143         return artifacts;
144     }
145
146     /**
147      * Example VNF Catalog based on VNF_CATALOG XML
148      * 
149      * @return test Artifacts
150      * @throws IOException
151      * @throws UnsupportedEncodingException
152      */
153     private List<Artifact> createVnfCatalogXmlArtifact() throws IOException, UnsupportedEncodingException {
154         byte[] encoded = Files.readAllBytes(Paths.get("src/test/resources/xmlFiles/fortigate.xml"));
155         List<Artifact> artifacts = new ArrayList<Artifact>();
156         artifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, new String(encoded, "utf-8")));
157         return artifacts;
158     }
159
160     /**
161      * Always return CREATED (success) for a PUT operation.
162      */
163     private void mockPutOperations() {
164         OperationResult mockPutResp = mock(OperationResult.class);
165         when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
166         when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
167                 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
168     }
169
170     private void assertPutOperationsSucceeded() {
171         // Only two of the VNF images should be pushed
172         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
173         AaiRestClient mockedClient = Mockito.verify(mockRestClient, Mockito.times(2));
174         mockedClient.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(),
175                 Mockito.any(MediaType.class));
176         assertThat(argument.getAllValues().get(0), containsString("3.16.9"));
177         assertThat(argument.getAllValues().get(0), containsString("VM00"));
178         assertThat(argument.getAllValues().get(1), containsString("3.16.1"));
179         assertThat(argument.getAllValues().get(1), containsString("VM01"));
180     }
181 }