2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.entity.catalog;
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;
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.restclient.AaiRestClient;
45 import org.onap.aai.restclient.client.OperationResult;
47 public class TestVnfCatalogArtifactHandler {
49 protected static String CONFIG_FILE = "model-loader.properties";
51 private AaiRestClient mockRestClient = mock(AaiRestClient.class);
54 * Update A&AI with 4 images, 2 of which already exist.
59 public void testUpdateVnfImages() throws Exception {
61 OperationResult mockGetResp = mock(OperationResult.class);
64 when(mockGetResp.getResultCode())
65 .thenReturn(Response.Status.OK.getStatusCode())
66 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
67 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
68 .thenReturn(Response.Status.OK.getStatusCode());
71 when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
72 .thenReturn(mockGetResp);
75 // Example VNF Catalog XML
76 VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
77 assertThat(handler.pushArtifacts(createVnfCatalogArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
80 assertPutOperationsSucceeded();
83 private ModelLoaderConfig createConfig() {
84 Properties configProperties = new Properties();
86 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
87 } catch (IOException e) {
90 ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
95 * Example VNF Catalog based on JSON data (returned by Babel)
97 * @return test Artifacts
99 * @throws UnsupportedEncodingException
101 private List<Artifact> createVnfCatalogArtifact() throws IOException, UnsupportedEncodingException {
102 String examplePath = "src/test/resources/imagedataexample.json";
103 byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
104 List<Artifact> artifacts = new ArrayList<Artifact>();
105 artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
110 * Always return CREATED (success) for a PUT operation.
112 private void mockPutOperations() {
113 OperationResult mockPutResp = mock(OperationResult.class);
114 when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
115 when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
116 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
119 private void assertPutOperationsSucceeded() {
120 // Only two of the VNF images should be pushed
121 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
122 AaiRestClient mockedClient = Mockito.verify(mockRestClient, Mockito.times(2));
123 mockedClient.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(),
124 Mockito.any(MediaType.class));
125 assertThat(argument.getAllValues().get(0), containsString("3.16.9"));
126 assertThat(argument.getAllValues().get(0), containsString("VM00"));
127 assertThat(argument.getAllValues().get(1), containsString("3.16.1"));
128 assertThat(argument.getAllValues().get(1), containsString("VM01"));