Stop installing librairies during tests
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / test / MultiThreadedTestCase.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.escape.verify.test;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Random;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.Future;
22
23 import javax.ws.rs.core.Response;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27
28 import com.fasterxml.jackson.core.JsonParseException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 import it.polito.escape.verify.client.VerifyClient;
33 import it.polito.escape.verify.client.VerifyClientException;
34 import it.polito.escape.verify.model.Configuration;
35 import it.polito.escape.verify.model.Graph;
36 import it.polito.escape.verify.model.Node;
37
38 public class MultiThreadedTestCase {
39
40         private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
41                 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
42
43                 Response retrieveGraphResponse = verifyClient.retrieveGraph(1);
44                 String responseString = retrieveGraphResponse.readEntity(String.class);
45                 System.out.println(responseString);
46
47                 Graph graph = new ObjectMapper().readValue(responseString, Graph.class);
48
49                 UpdateGraph task = new UpdateGraph(verifyClient, 1, graph);
50
51                 List<MultiThreadedTestCase.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
52                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
53
54                 List<Future<Response>> futures = executorService.invokeAll(tasks);
55                 List<Integer> resultList = new ArrayList<Integer>(futures.size());
56
57                 // Check for exceptions
58                 for (Future<Response> future : futures) {
59                         // Throws an exception if an exception was thrown by the task.
60                         resultList.add(future.get().getStatus());
61                 }
62                 // Validate the dimensions
63                 Assert.assertEquals(threadCount, futures.size());
64                 List<Integer> expectedList = new ArrayList<Integer>(threadCount);
65                 for (int i = 1; i <= threadCount; i++) {
66                         expectedList.add(200);
67                 }
68                 // Validate expected results
69                 Assert.assertEquals(expectedList, resultList);
70         }
71
72         private void testUpdateGraph(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
73                 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
74
75                 Response retrieveGraphResponse = verifyClient.retrieveGraph(2L);
76                 String responseString = retrieveGraphResponse.readEntity(String.class);
77                 System.out.println(responseString);
78
79                 Graph graph = new ObjectMapper().readValue(responseString, Graph.class);
80
81                 Node nodeToEdit = graph.getNodes().get(1L);
82                 nodeToEdit.setFunctional_type("endpoint");
83                 nodeToEdit.setConfiguration(new Configuration(nodeToEdit.getName(), "", new ObjectMapper().createArrayNode()));
84
85                 String graphAsString = new ObjectMapper().writeValueAsString(graph);
86                 System.out.println(graphAsString);
87
88                 UpdateGraph task = new UpdateGraph(verifyClient, 2, graph);
89
90                 List<MultiThreadedTestCase.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
91                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
92
93                 List<Future<Response>> futures = executorService.invokeAll(tasks);
94                 List<String> resultList = new ArrayList<String>(futures.size());
95
96                 // Check for exceptions
97                 for (Future<Response> future : futures) {
98                         // Throws an exception if an exception was thrown by the task.
99                         resultList.add(future.get().readEntity(String.class));
100                 }
101                 // Validate dimensions
102                 Assert.assertEquals(threadCount, futures.size());
103
104                 List<String> expectedList = new ArrayList<String>(threadCount);
105                 for (int i = 1; i <= threadCount; i++) {
106                         expectedList.add(graphAsString);
107                 }
108                 // Validate expected results
109                 Assert.assertEquals(expectedList, resultList);
110         }
111
112         private void testCreateGraphStatus(final int threadCount, Graph graph) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
113                 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
114
115                 CreateGraph task = new CreateGraph(verifyClient, graph);
116
117                 List<MultiThreadedTestCase.CreateGraph> tasks = Collections.nCopies(threadCount, task);
118                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
119
120                 List<Future<Response>> futures = executorService.invokeAll(tasks);
121                 List<Integer> resultList = new ArrayList<Integer>(futures.size());
122
123                 // Check for exceptions
124                 for (Future<Response> future : futures) {
125                         // Throws an exception if an exception was thrown by the task.
126                         resultList.add(future.get().getStatus());
127                 }
128                 // Validate the IDs
129                 Assert.assertEquals(threadCount, futures.size());
130
131                 List<Integer> expectedList = new ArrayList<Integer>(threadCount);
132                 for (int i = 1; i <= threadCount; i++) {
133                         expectedList.add(201);
134                 }
135                 // Validate expected results
136                 Assert.assertEquals(expectedList, resultList);
137         }
138
139         private int randInt(int min, int max){
140                 Random rand = new Random();
141             int randomNum = rand.nextInt((max - min) + 1) + min;
142             return randomNum;
143         }
144
145         @Test
146         public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
147                 testUpdateGraphStatus(64);
148         }
149
150         @Test
151         public void updateGraphResponseCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
152                 testUpdateGraph(16);
153         }
154
155         @Test
156         public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
157                 Graph graph = new Graph();
158                 testCreateGraphStatus(8, graph);
159         }
160
161         class UpdateGraph implements Callable<Response> {
162
163                 private VerifyClient    verifyClient;
164
165                 private int                             graphId;
166
167                 private Graph                   graph;
168
169                 public UpdateGraph(VerifyClient verifyClient, int graphId, Graph graph) {
170                         this.graphId = graphId;
171                         this.graph = graph;
172                         this.verifyClient = verifyClient;
173                 }
174
175                 @Override
176                 public Response call() throws Exception {
177                         Thread.sleep(randInt(0,2000));
178                         Response updateGraphResponse = this.verifyClient.updateGraph(this.graphId, this.graph);
179                         return updateGraphResponse;
180                 }
181         }
182
183         class CreateGraph implements Callable<Response> {
184
185                 private VerifyClient    verifyClient;
186
187                 private Graph                   graph;
188
189                 public CreateGraph(VerifyClient verifyClient, Graph graph) {
190                         this.graph = graph;
191                         this.verifyClient = verifyClient;
192                 }
193
194                 @Override
195                 public Response call() throws Exception {
196                         Thread.sleep(randInt(0,2000));
197                         return this.verifyClient.createGraph(this.graph);
198                 }
199                 
200         }
201 }