[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / TODO / ImportCapabilityTypeCITest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.ci.tests.execute.TODO;
22
23 import org.testng.annotations.AfterClass;
24 import org.testng.annotations.Test;
25 import org.testng.AssertJUnit;
26 import java.io.File;
27 import java.io.IOException;
28
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.entity.mime.MultipartEntityBuilder;
32 import org.apache.http.entity.mime.content.FileBody;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.openecomp.sdc.ci.tests.api.Urls;
37 import org.openecomp.sdc.ci.tests.config.Config;
38 import org.openecomp.sdc.ci.tests.utils.DbUtils;
39 import org.openecomp.sdc.ci.tests.utils.Utils;
40 import org.openecomp.sdc.ci.tests.utils.DbUtils.TitanState;
41
42 import fj.data.Either;
43
44 public class ImportCapabilityTypeCITest {
45         public static final DbUtils DbUtils = new DbUtils();
46
47         @AfterClass
48         public static void afterClass() {
49                 DbUtils.shutDowntitan();
50         }
51
52         static Config config = Config.instance();
53
54         // private final String IMPORT_CAPABILITY_TYPES_PATH =
55         // "src/test/resources/CI/importResourceTests/import_capabilitiesTypes/";
56
57         @Test
58         public void testAddingCapabilityTypes() throws IOException {
59                 TitanState originalState = DbUtils.getCurrentTitanState();
60
61                 String importResourceDir = config.getImportResourceConfigDir();
62
63                 String capabilityTypes = importResourceDir + File.separator + "capabilityTypesCi.zip";
64                 // importCapabilityType("src/test/resources/CI/importResource/capabilityTypesCi.zip");
65                 importCapabilityType(capabilityTypes);
66                 Either<Vertex, Boolean> eitherVertex = DbUtils.getVertexByUId("tosca.capabilities.Test.Ci");
67                 AssertJUnit.assertTrue(eitherVertex.isLeft());
68                 DbUtils.restoreToTitanState(originalState);
69                 eitherVertex = DbUtils.getVertexByUId("tosca.capabilities.Test.Ci");
70                 AssertJUnit.assertTrue(eitherVertex.isRight());
71         }
72
73         @Test
74         public void AddingCapabilityNotFound() throws IOException {
75                 TitanState originalState = DbUtils.getCurrentTitanState();
76                 String importResourceTestsDir = config.getImportResourceTestsConfigDir();
77                 String capabilitiesTests = importResourceTestsDir + File.separator + "capabilityTypesCi.zip";
78                 importCapabilityType(capabilitiesTests);
79                 Either<Vertex, Boolean> eitherVertex = DbUtils.getVertexByUId("tosca.capabilities.NonExsitingCapability");
80                 AssertJUnit.assertTrue(eitherVertex.isRight());
81                 DbUtils.restoreToTitanState(originalState);
82         }
83
84         public static Integer importAllCapabilityTypes() throws IOException {
85
86                 String importResourceDir = config.getImportResourceConfigDir() + File.separator + "capabilityTypes.zip";
87                 // return
88                 // importCapabilityType("src/test/resources/CI/importResource/capabilityTypes.zip");
89                 return importCapabilityType(importResourceDir);
90         }
91
92         private static Integer importCapabilityType(String filePath) throws IOException {
93                 Config config = Utils.getConfig();
94                 CloseableHttpResponse response = null;
95                 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
96
97                 mpBuilder.addPart("capabilityTypeZip", new FileBody(new File(filePath)));
98
99                 String url = String.format(Urls.IMPORT_CAPABILITY_TYPE, config.getCatalogBeHost(), config.getCatalogBePort());
100
101                 CloseableHttpClient client = HttpClients.createDefault();
102                 try {
103                         HttpPost httpPost = new HttpPost(url);
104                         httpPost.addHeader("USER_ID", "jh0003");
105                         httpPost.setEntity(mpBuilder.build());
106                         response = client.execute(httpPost);
107                         return response.getStatusLine().getStatusCode();
108                 } finally {
109                         closeResponse(response);
110                         closeHttpClient(client);
111
112                 }
113         }
114
115         private static void closeHttpClient(CloseableHttpClient client) {
116                 try {
117                         if (client != null) {
118                                 client.close();
119                         }
120                 } catch (IOException e) {
121                         System.out.println("failed to close client or response: " + e.getMessage());
122                 }
123         }
124
125         private static void closeResponse(CloseableHttpResponse response) {
126                 try {
127                         if (response != null) {
128                                 response.close();
129                         }
130                 } catch (IOException e) {
131                         System.out.println("failed to close client or response: " + e.getMessage());
132                 }
133         }
134
135 }