Implement support for v10 model entities.
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / entity / catalog / VnfCatalogArtifactHandlerTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * Model Loader\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  * http://www.apache.org/licenses/LICENSE-2.0\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  *\r
20  * ECOMP and OpenECOMP are trademarks\r
21  * and service marks of AT&T Intellectual Property.\r
22  */\r
23 package org.openecomp.modelloader.entity.catalog;\r
24 \r
25 import static org.junit.Assert.assertTrue;\r
26 import static org.junit.Assert.fail;\r
27 \r
28 import java.io.IOException;\r
29 import java.nio.file.Files;\r
30 import java.nio.file.Paths;\r
31 import java.util.ArrayList;\r
32 import java.util.List;\r
33 import java.util.Properties;\r
34 \r
35 import org.junit.Test;\r
36 import org.junit.runner.RunWith;\r
37 import org.mockito.ArgumentCaptor;\r
38 import org.mockito.Mockito;\r
39 import org.openecomp.modelloader.config.ModelLoaderConfig;\r
40 import org.openecomp.modelloader.entity.Artifact;\r
41 import org.openecomp.modelloader.restclient.AaiRestClient;\r
42 import org.openecomp.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
46 \r
47 import com.sun.jersey.api.client.ClientResponse;\r
48 \r
49 @RunWith(PowerMockRunner.class)\r
50 @PrepareForTest({ VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class })\r
51 public class VnfCatalogArtifactHandlerTest {\r
52 \r
53   protected static String CONFIG_FILE = "model-loader.properties";\r
54 \r
55   @Test\r
56   public void testWithMocks() throws Exception {\r
57 \r
58     Properties configProperties = new Properties();\r
59     try {\r
60       configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));\r
61     } catch (IOException e) {\r
62       fail();\r
63     }\r
64     ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);\r
65 \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
71 \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
78 \r
79     VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);\r
80 \r
81     String examplePath = "src/test/resources/vnfcatalogexample.xml";\r
82 \r
83     byte[] encoded = Files.readAllBytes(Paths.get(examplePath));\r
84     String payload = new String(encoded, "utf-8");\r
85 \r
86     VnfCatalogArtifact artifact = new VnfCatalogArtifact(payload);\r
87     List<Artifact> artifacts = new ArrayList<Artifact>();\r
88     artifacts.add(artifact);\r
89 \r
90     String distributionID = "test";\r
91 \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
99   }\r
100 }\r