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