Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / restproperties / ThreadedReadTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.onap.so.client.restproperties;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.junit.Assert.assertThat;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.Future;
34 import java.util.concurrent.ScheduledExecutorService;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.Test;
37 import org.onap.so.client.RestPropertiesLoader;
38 import org.onap.so.client.aai.AAIProperties;
39 import org.onap.so.BaseTest;
40
41 public class ThreadedReadTest {
42     @Test
43     public void allAtOnce() throws InterruptedException {
44         ExecutorService executorService = Executors.newFixedThreadPool(10);
45
46         Callable<AAIProperties> callableTask = () -> {
47             return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
48         };
49         List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
50
51         callableTasks.add(callableTask);
52         callableTasks.add(callableTask);
53         callableTasks.add(callableTask);
54         callableTasks.add(callableTask);
55         callableTasks.add(callableTask);
56
57         List<Future<AAIProperties>> futures = executorService.invokeAll(callableTasks);
58
59         Set<AAIProperties> results = new HashSet<>();
60         futures.forEach(item -> {
61             try {
62                 results.add(item.get());
63             } catch (InterruptedException | ExecutionException e) {
64                 throw new RuntimeException(e);
65             }
66         });
67
68         assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
69
70     }
71
72     @Test
73     public void executeOverTime() {
74         ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
75
76         Callable<AAIProperties> callableTask = () -> {
77             TimeUnit.MILLISECONDS.sleep(500);
78             return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
79         };
80         List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
81
82         callableTasks.add(callableTask);
83         callableTasks.add(callableTask);
84         callableTasks.add(callableTask);
85         callableTasks.add(callableTask);
86         callableTasks.add(callableTask);
87
88         Set<AAIProperties> results = new HashSet<>();
89         callableTasks.forEach(callable -> {
90             try {
91                 TimeUnit.MILLISECONDS.sleep(300);
92                 Future<AAIProperties> result = executorService.submit(callable);
93                 results.add(result.get());
94             } catch (InterruptedException | ExecutionException e) {
95                 throw new RuntimeException(e);
96             }
97         });
98
99         assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
100     }
101
102 }