-
Notifications
You must be signed in to change notification settings - Fork 11
/
ExperimentManagerServiceTest.java
196 lines (162 loc) · 11.3 KB
/
ExperimentManagerServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
* Copyright (c) 2018 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.services;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.QueryParameters;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.ExperimentManagerService;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.ExperimentModelStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.stubs.TraceModelStub;
import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.utils.RestServerTest;
import org.junit.Test;
import com.google.common.collect.ImmutableSet;
/**
* Test the {@link ExperimentManagerService}
*
* @author Loic Prieur-Drevon
*/
public class ExperimentManagerServiceTest extends RestServerTest {
private static final String TEST = "test";
private static final @NonNull String EXPERIMENT_UUID = "bb12687f-9866-3a9f-bf55-b4d9da0137ed";
private static final @NonNull ImmutableSet<TraceModelStub> CONTEXT_SWITCH_SET = ImmutableSet.of(CONTEXT_SWITCHES_KERNEL_STUB, CONTEXT_SWITCHES_UST_STUB);
private static final ExperimentModelStub EXPECTED = new ExperimentModelStub(TEST,
UUID.fromString(EXPERIMENT_UUID), 0L, 0L, 0L, "RUNNING", CONTEXT_SWITCH_SET);
/**
* Basic test for the {@link ExperimentManagerService}
*/
@Test
public void testExperiment() {
WebTarget application = getApplicationEndpoint();
WebTarget traces = application.path(TRACES);
WebTarget expTarget = application.path(EXPERIMENTS);
assertPost(traces, CONTEXT_SWITCHES_UST_STUB);
assertPost(traces, CONTEXT_SWITCHES_KERNEL_STUB);
assertEquals(CONTEXT_SWITCH_SET, getTraces(traces));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
List<String> traceUUIDs = new ArrayList<>();
traceUUIDs.add(CONTEXT_SWITCHES_KERNEL_UUID.toString());
traceUUIDs.add(CONTEXT_SWITCHES_UST_UUID.toString());
Map<String, Object> parameters = new HashMap<>();
parameters.put(NAME, EXPECTED.getName());
parameters.put(TRACES, traceUUIDs);
Response response = expTarget.request().post(Entity.json(new QueryParameters(parameters, Collections.emptyList())));
assertEquals("Failed to POST the experiment", EXPECTED, response.readEntity(ExperimentModelStub.class));
assertEquals("Failed to add experiment to set of experiments", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding an experiment should not change the trace set", CONTEXT_SWITCH_SET, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
assertEquals("Failed to DELETE the experiment", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().delete().readEntity(ExperimentModelStub.class));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
assertEquals("Deleting an experiment should not change the trace set", CONTEXT_SWITCH_SET, getTraces(traces));
response.close();
}
/**
* Test posting an experiment twice, it should be OK
*/
@Test
public void testRePost() {
WebTarget application = getApplicationEndpoint();
WebTarget traces = application.path(TRACES);
WebTarget expTarget = application.path(EXPERIMENTS);
assertPost(traces, CONTEXT_SWITCHES_UST_STUB);
assertPost(traces, CONTEXT_SWITCHES_KERNEL_STUB);
assertEquals(CONTEXT_SWITCH_SET, getTraces(traces));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
List<String> traceUUIDs = new ArrayList<>();
traceUUIDs.add(CONTEXT_SWITCHES_KERNEL_UUID.toString());
traceUUIDs.add(CONTEXT_SWITCHES_UST_UUID.toString());
Map<String, Object> parameters = new HashMap<>();
parameters.put(NAME, EXPECTED.getName());
parameters.put(TRACES, traceUUIDs);
Response response = expTarget.request().post(Entity.json(new QueryParameters(parameters, Collections.emptyList())));
assertEquals("Failed to POST the experiment", EXPECTED, response.readEntity(ExperimentModelStub.class));
assertEquals("Failed to add experiment to set of experiments", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding an experiment should not change the trace set", CONTEXT_SWITCH_SET, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
response.close();
// Make a second post with the same name and traces, should return the experiment
Response response2 = expTarget.request().post(Entity.json(new QueryParameters(parameters, Collections.emptyList())));
assertEquals("Status of second post", Status.OK.getStatusCode(), response2.getStatus());
assertEquals("Failed to POST the experiment a second time", EXPECTED, response2.readEntity(ExperimentModelStub.class));
assertEquals("There should still be only one experiment", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding an experiment should not change the trace set", CONTEXT_SWITCH_SET, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
response2.close();
assertEquals("Failed to DELETE the experiment", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().delete().readEntity(ExperimentModelStub.class));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
assertEquals("Deleting an experiment should not change the trace set", CONTEXT_SWITCH_SET, getTraces(traces));
}
/**
* Test posting an experiment with different conflicting situations
*/
@Test
public void testPostConflicts() {
WebTarget application = getApplicationEndpoint();
WebTarget traces = application.path(TRACES);
WebTarget expTarget = application.path(EXPERIMENTS);
assertPost(traces, CONTEXT_SWITCHES_UST_STUB);
assertPost(traces, CONTEXT_SWITCHES_KERNEL_STUB);
assertPost(traces, ARM_64_KERNEL_STUB);
ImmutableSet<TraceModelStub> traceSet = ImmutableSet.of(CONTEXT_SWITCHES_UST_STUB, CONTEXT_SWITCHES_KERNEL_STUB, ARM_64_KERNEL_STUB);
assertEquals(traceSet, getTraces(traces));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
List<String> traceUUIDs = new ArrayList<>();
traceUUIDs.add(CONTEXT_SWITCHES_KERNEL_UUID.toString());
traceUUIDs.add(CONTEXT_SWITCHES_UST_UUID.toString());
Map<String, Object> parameters = new HashMap<>();
parameters.put(NAME, EXPECTED.getName());
parameters.put(TRACES, traceUUIDs);
Response response = expTarget.request().post(Entity.json(new QueryParameters(parameters, Collections.emptyList())));
assertEquals("Failed to POST the experiment", EXPECTED, response.readEntity(ExperimentModelStub.class));
assertEquals("Failed to add experiment to set of experiments", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding an experiment should not change the trace set", traceSet, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
response.close();
// Post same name experiment, but different trace size, should return a conflict status, with the original experiment
List<String> traceUUIDs2 = new ArrayList<>();
traceUUIDs2.add(CONTEXT_SWITCHES_KERNEL_UUID.toString());
Map<String, Object> parameters2 = new HashMap<>();
parameters2.put(NAME, EXPECTED.getName());
parameters2.put(TRACES, traceUUIDs2);
Response response2 = expTarget.request().post(Entity.json(new QueryParameters(parameters2, Collections.emptyList())));
assertEquals("Expected a conflict for posting different experiment", Status.CONFLICT.getStatusCode(), response2.getStatus());
assertEquals("Conflict should return original experiment", EXPECTED, response2.readEntity(ExperimentModelStub.class));
assertEquals("There should still be only one experiment", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding a failed experiment should not change the trace set", traceSet, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
response2.close();
// Post same experiment name, but with traces with the same names, but not the same traces
traceUUIDs2 = new ArrayList<>();
traceUUIDs.add(ARM_64_KERNEL_UUID.toString());
traceUUIDs.add(CONTEXT_SWITCHES_UST_UUID.toString());
parameters2 = new HashMap<>();
parameters2.put(NAME, EXPECTED.getName());
parameters2.put(TRACES, traceUUIDs2);
response2 = expTarget.request().post(Entity.json(new QueryParameters(parameters2, Collections.emptyList())));
assertEquals("Expected a conflict for posting different experiment", Status.CONFLICT.getStatusCode(), response2.getStatus());
assertEquals("Conflict should return original experiment", EXPECTED, response2.readEntity(ExperimentModelStub.class));
assertEquals("There should still be only one experiment", Collections.singleton(EXPECTED), getExperiments(expTarget));
assertEquals("Adding a failed experiment should not change the trace set", traceSet, getTraces(traces));
assertEquals("Failed to get the experiment by its UUID", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().get(ExperimentModelStub.class));
assertEquals("Failed to DELETE the experiment", EXPECTED, expTarget.path(EXPERIMENT_UUID).request().delete().readEntity(ExperimentModelStub.class));
assertEquals("experiment set should be empty at this point", Collections.emptySet(), getExperiments(expTarget));
assertEquals("Deleting an experiment should not change the trace set", traceSet, getTraces(traces));
}
}