Remove OpenECOMP from license file
[aai/model-loader.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.modelloader.config.ModelLoaderConfig;\r
41 import org.openecomp.modelloader.entity.Artifact;\r
42 import org.openecomp.modelloader.restclient.AaiRestClient;\r
43 import org.openecomp.modelloader.restclient.AaiRestClient.MimeType;\r
44 import org.powermock.api.mockito.PowerMockito;\r
45 import org.powermock.core.classloader.annotations.PrepareForTest;\r
46 import org.powermock.modules.junit4.PowerMockRunner;\r
47 \r
48 import com.sun.jersey.api.client.ClientResponse;\r
49 \r
50 @RunWith(PowerMockRunner.class)\r
51 @PrepareForTest({ VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class })\r
52 public class VnfCatalogArtifactHandlerTest {\r
53 \r
54   protected static String CONFIG_FILE = "model-loader.properties";\r
55 \r
56   @Test\r
57   public void testWithMocks() throws Exception {\r
58 \r
59     Properties configProperties = new Properties();\r
60     try {\r
61       configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));\r
62     } catch (IOException e) {\r
63       fail();\r
64     }\r
65     ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);\r
66 \r
67     ClientResponse mockGetResp = PowerMockito.mock(ClientResponse.class);\r
68     PowerMockito.when(mockGetResp.getStatus()).thenReturn(200).thenReturn(200).thenReturn(404)\r
69         .thenReturn(404).thenReturn(200); // only second two will be PUT\r
70     ClientResponse mockPutResp = PowerMockito.mock(ClientResponse.class);\r
71     PowerMockito.when(mockPutResp.getStatus()).thenReturn(201);\r
72 \r
73     AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class);\r
74     PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient);\r
75     PowerMockito.when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(),\r
76         Mockito.any(MimeType.class))).thenReturn(mockGetResp);\r
77     PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(),\r
78         Mockito.anyString(), Mockito.any(MimeType.class))).thenReturn(mockPutResp);\r
79 \r
80     VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);\r
81 \r
82     String examplePath = "src/test/resources/vnfcatalogexample.xml";\r
83 \r
84     byte[] encoded = Files.readAllBytes(Paths.get(examplePath));\r
85     String payload = new String(encoded, "utf-8");\r
86 \r
87     VnfCatalogArtifact artifact = new VnfCatalogArtifact(payload);\r
88     List<Artifact> artifacts = new ArrayList<Artifact>();\r
89     artifacts.add(artifact);\r
90 \r
91     String distributionID = "test";\r
92 \r
93     assertTrue(vnfCAH.pushArtifacts(artifacts, distributionID));\r
94     // times(2) bc with above get returns should only get to this part twice\r
95     ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);\r
96     Mockito.verify(mockRestClient, Mockito.times(2)).putResource(Mockito.anyString(),\r
97         argument.capture(), Mockito.anyString(), Mockito.any(MimeType.class));\r
98     assertTrue(argument.getAllValues().get(0).contains("5.2.5"));\r
99     assertTrue(argument.getAllValues().get(1).contains("5.2.4"));\r
100   }\r
101 }\r