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