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