2 * ============LICENSE_START==========================================
\r
4 * ===================================================================
\r
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017-2018 Amdocs
\r
7 * ===================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END============================================
\r
21 package org.onap.aai.modelloader.entity.catalog;
\r
23 import static org.junit.Assert.assertTrue;
\r
24 import static org.junit.Assert.fail;
\r
26 import java.io.IOException;
\r
27 import java.nio.file.Files;
\r
28 import java.nio.file.Paths;
\r
29 import java.util.ArrayList;
\r
30 import java.util.List;
\r
31 import java.util.Properties;
\r
33 import org.junit.Test;
\r
34 import org.junit.runner.RunWith;
\r
35 import org.mockito.ArgumentCaptor;
\r
36 import org.mockito.Mockito;
\r
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
\r
38 import org.onap.aai.modelloader.entity.Artifact;
\r
39 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
\r
40 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
\r
41 import org.onap.aai.modelloader.restclient.AaiRestClient;
\r
42 import org.onap.aai.modelloader.restclient.AaiRestClient.MimeType;
\r
43 import org.powermock.api.mockito.PowerMockito;
\r
44 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
45 import org.powermock.modules.junit4.PowerMockRunner;
\r
47 import com.sun.jersey.api.client.ClientResponse;
\r
49 @RunWith(PowerMockRunner.class)
\r
50 @PrepareForTest({ VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class })
\r
51 public class VnfCatalogArtifactHandlerTest {
\r
53 protected static String CONFIG_FILE = "model-loader.properties";
\r
56 public void testWithMocks() throws Exception {
\r
58 Properties configProperties = new Properties();
\r
60 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
\r
61 } catch (IOException e) {
\r
64 ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
\r
66 ClientResponse mockGetResp = PowerMockito.mock(ClientResponse.class);
\r
67 PowerMockito.when(mockGetResp.getStatus()).thenReturn(200).thenReturn(200).thenReturn(404)
\r
68 .thenReturn(404).thenReturn(200); // only second two will be PUT
\r
69 ClientResponse mockPutResp = PowerMockito.mock(ClientResponse.class);
\r
70 PowerMockito.when(mockPutResp.getStatus()).thenReturn(201);
\r
72 AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class);
\r
73 PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient);
\r
74 PowerMockito.when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(),
\r
75 Mockito.any(MimeType.class))).thenReturn(mockGetResp);
\r
76 PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(),
\r
77 Mockito.anyString(), Mockito.any(MimeType.class))).thenReturn(mockPutResp);
\r
79 VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);
\r
81 String examplePath = "src/test/resources/vnfcatalogexample.xml";
\r
83 byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
\r
84 String payload = new String(encoded, "utf-8");
\r
86 VnfCatalogArtifact artifact = new VnfCatalogArtifact(payload);
\r
87 List<Artifact> artifacts = new ArrayList<Artifact>();
\r
88 artifacts.add(artifact);
\r
90 String distributionID = "test";
\r
92 assertTrue(vnfCAH.pushArtifacts(artifacts, distributionID));
\r
93 // times(2) bc with above get returns should only get to this part twice
\r
94 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
\r
95 Mockito.verify(mockRestClient, Mockito.times(2)).putResource(Mockito.anyString(),
\r
96 argument.capture(), Mockito.anyString(), Mockito.any(MimeType.class));
\r
97 assertTrue(argument.getAllValues().get(0).contains("5.2.5"));
\r
98 assertTrue(argument.getAllValues().get(1).contains("5.2.4"));
\r