e9a996c6e3421a0dd8a729a4a077944634359cb2
[sdc.git] /
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.resource;
22
23 import org.junit.Rule;
24 import org.junit.rules.TestName;
25 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
26 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
27 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
28 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
29 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
30 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
31 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
32 import org.testng.annotations.Test;
33
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Executors;
37
38 public class SimultaneousApiTest extends ComponentBaseTest {
39
40         protected static ResourceReqDetails resourceDetails = ElementFactory.getDefaultResource();
41
42         @Rule
43         public static TestName name = new TestName();
44
45         static String httpCspUserId = "km2000";
46         static String userFirstName = "Kot";
47         static String userLastName = "Matroskin";
48         static String email = "km2000@intl.sdc.com";
49         static String role = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getRole();
50
51         public static class WorkerThread implements Runnable {
52                 CountDownLatch countDownLatch;
53                 int threadIndex;
54
55                 public WorkerThread(int threadIndex, CountDownLatch countDownLatch) {
56                         this.threadIndex = threadIndex;
57                         this.countDownLatch = countDownLatch;
58                 }
59
60                 @Override
61                 public void run() {
62                         System.out.println("**** Thread started " + threadIndex);
63                         try {
64                                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails,
65                                                 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
66                                 String id = ResponseParser.getUniqueIdFromResponse(createResource);
67                                 // System.out.println("**** Thread " + threadIndex + " create
68                                 // resource status " + createResource.getErrorCode() + " id = "
69                                 // + id + " error " + createResource.getResponse());
70                                 // assertEquals("**** create resource: " +
71                                 // createResource.getErrorCode() + " thread " + threadIndex,
72                                 // 201, status);
73                         } catch (Exception e) {
74                                 // System.out.println("**** Thread " + threadIndex + " exception
75                                 // " + e);
76                         }
77                         countDownLatch.countDown();
78                         // System.out.println("**** Thread finished " + threadIndex);
79
80                 }
81
82                 // public void run_() {
83                 // System.out.println("**** Thread started " + threadIndex);
84                 // try {
85                 // UserUtils userUtils = new UserUtils();
86                 // User userDetails = new User(userFirstName, userLastName,
87                 // httpCspUserId, email, role , 0L);
88                 // RestResponse response =
89                 // userUtils.createUserTowardsCatalogBe(userDetails,
90                 // userUtils.getUserDetailesAdmin());
91                 // System.out.println("**** Thread " + threadIndex + " create resource
92                 // status " + response.getErrorCode() + " response " +
93                 // response.getResponse());
94                 //// assertEquals("**** create resource: " +
95                 // createResource.getErrorCode() + " thread " + threadIndex, 201,
96                 // status);
97                 // } catch (Exception e) {
98                 // System.out.println("**** Thread " + threadIndex + " exception " + e);
99                 // }
100                 // countDownLatch.countDown();
101                 // System.out.println("**** Thread finished " + threadIndex);
102                 //
103                 // }
104         }
105
106         @Test
107         public void create2Resources() throws InterruptedException {
108                 int threadCount = 5;
109                 CountDownLatch countDownLatch = new CountDownLatch(threadCount);
110                 ExecutorService executor = Executors.newFixedThreadPool(threadCount);
111                 for (int i = 0; i < threadCount; i++) {
112                         Runnable worker = new WorkerThread(i + 1, countDownLatch);
113                         executor.execute(worker);
114                 }
115                 countDownLatch.await();
116                 // System.out.println(" finished ");
117
118         }
119 }