Skip to content

Commit 9c46e93

Browse files
Sasa BerberovicSasa Berberovic
authored andcommitted
Make intagration test succeed again
1 parent 1c84e52 commit 9c46e93

1 file changed

Lines changed: 85 additions & 94 deletions

File tree

src/test/java/eu/openanalytics/containerproxy/test/proxy/TestConcurrentUsers.java

Lines changed: 85 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
*/
2121
package eu.openanalytics.containerproxy.test.proxy;
2222

23-
import java.util.HashMap;
24-
import java.util.Map;
23+
import static org.junit.Assert.assertEquals;
24+
25+
import java.util.HashSet;
26+
import java.util.Set;
2527

2628
import javax.inject.Inject;
2729

@@ -30,132 +32,121 @@
3032
import org.junit.runner.RunWith;
3133
import org.springframework.boot.test.context.SpringBootTest;
3234
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
35+
import org.springframework.boot.test.web.client.TestRestTemplate;
36+
import org.springframework.boot.web.server.LocalServerPort;
3337
import org.springframework.core.env.Environment;
38+
import org.springframework.http.HttpEntity;
39+
import org.springframework.http.HttpHeaders;
40+
import org.springframework.http.HttpMethod;
3441
import org.springframework.http.MediaType;
42+
import org.springframework.http.ResponseEntity;
3543
import org.springframework.test.context.ActiveProfiles;
3644
import org.springframework.test.context.junit4.SpringRunner;
37-
import org.springframework.test.web.reactive.server.WebTestClient;
38-
import org.springframework.web.reactive.function.BodyInserters;
3945

4046
import eu.openanalytics.containerproxy.ContainerProxyApplication;
47+
import eu.openanalytics.containerproxy.model.runtime.Proxy;
48+
import eu.openanalytics.containerproxy.model.runtime.RuntimeSetting;
4149

42-
@SpringBootTest(classes= {ContainerProxyApplication.class}, webEnvironment=WebEnvironment.RANDOM_PORT)
50+
@SpringBootTest(classes = { ContainerProxyApplication.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
4351
@RunWith(SpringRunner.class)
4452
@ActiveProfiles("test")
4553
public class TestConcurrentUsers {
4654

4755
@Inject
48-
private WebTestClient webClient;
49-
56+
private TestRestTemplate restTemplate;
57+
5058
@Inject
5159
private Environment environment;
52-
60+
61+
@LocalServerPort
62+
private int port;
63+
5364
private String[] user1;
5465
private String[] user2;
5566
private String[] specIds;
56-
67+
5768
@Before
5869
public void init() {
59-
user1 = new String[] {
60-
environment.getProperty("proxy.users[0].name"),
61-
environment.getProperty("proxy.users[0].password")
62-
};
63-
user2 = new String[] {
64-
environment.getProperty("proxy.users[1].name"),
65-
environment.getProperty("proxy.users[1].password")
66-
};
67-
specIds = new String[] {
68-
environment.getProperty("proxy.specs[0].id"),
69-
environment.getProperty("proxy.specs[1].id")
70-
};
70+
user1 = new String[] { environment.getProperty("proxy.users[0].name"),
71+
environment.getProperty("proxy.users[0].password") };
72+
user2 = new String[] { environment.getProperty("proxy.users[1].name"),
73+
environment.getProperty("proxy.users[1].password") };
74+
specIds = new String[] { environment.getProperty("proxy.specs[0].id"),
75+
environment.getProperty("proxy.specs[1].id") };
7176
}
72-
77+
7378
@Test
7479
public void test1User2Sessions1Spec() throws Exception {
75-
Map<String,String> proxyInfo1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
76-
Map<String,String> proxyInfo2 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
77-
doDeleteProxy(proxyInfo1.get("proxyId"), proxyInfo1.get("jSessionId"));
78-
doDeleteProxy(proxyInfo2.get("proxyId"), proxyInfo2.get("jSessionId"));
80+
String proxyId1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
81+
String proxyId2 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
82+
doDeleteProxy(proxyId1, user1[0], user1[1]);
83+
doDeleteProxy(proxyId2, user1[0], user1[1]);
7984
}
80-
85+
8186
@Test
8287
public void test1User2Sessions2Specs() throws Exception {
83-
Map<String,String> proxyInfo1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
84-
Map<String,String> proxyInfo2 = loginAndLaunchProxy(user1[0], user1[1], specIds[1]);
85-
doDeleteProxy(proxyInfo1.get("proxyId"), proxyInfo1.get("jSessionId"));
86-
doDeleteProxy(proxyInfo2.get("proxyId"), proxyInfo2.get("jSessionId"));
88+
String proxyId1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
89+
String proxyId2 = loginAndLaunchProxy(user1[0], user1[1], specIds[1]);
90+
doDeleteProxy(proxyId1, user1[0], user1[1]);
91+
doDeleteProxy(proxyId2, user1[0], user1[1]);
8792
}
88-
93+
8994
@Test
9095
public void test2Users2Sessions1Spec() throws Exception {
91-
Map<String,String> proxyInfo1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
92-
Map<String,String> proxyInfo2 = loginAndLaunchProxy(user2[0], user2[1], specIds[0]);
93-
doDeleteProxy(proxyInfo1.get("proxyId"), proxyInfo1.get("jSessionId"));
94-
doDeleteProxy(proxyInfo2.get("proxyId"), proxyInfo2.get("jSessionId"));
96+
String proxyId1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
97+
String proxyId2 = loginAndLaunchProxy(user2[0], user2[1], specIds[0]);
98+
doDeleteProxy(proxyId1, user1[0], user1[1]);
99+
doDeleteProxy(proxyId2, user2[0], user2[1]);
95100
}
96-
101+
97102
@Test
98103
public void test2Users2Sessions2Specs() throws Exception {
99-
Map<String,String> proxyInfo1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
100-
Map<String,String> proxyInfo2 = loginAndLaunchProxy(user2[0], user2[1], specIds[1]);
101-
doDeleteProxy(proxyInfo1.get("proxyId"), proxyInfo1.get("jSessionId"));
102-
doDeleteProxy(proxyInfo2.get("proxyId"), proxyInfo2.get("jSessionId"));
104+
String proxyId1 = loginAndLaunchProxy(user1[0], user1[1], specIds[0]);
105+
String proxyId2 = loginAndLaunchProxy(user2[0], user2[1], specIds[1]);
106+
doDeleteProxy(proxyId1, user1[0], user1[1]);
107+
doDeleteProxy(proxyId2, user2[0], user2[1]);
103108
}
104-
105-
private Map<String,String> loginAndLaunchProxy(String username, String password, String specId) throws Exception {
106-
String jSessionId = doFormLogin(username, password);
107-
Map<?,?> proxyInfo = doLaunchProxy(specId, jSessionId);
109+
110+
private String loginAndLaunchProxy(String username, String password, String specId) throws Exception {
111+
Set<RuntimeSetting> createProxyBody = new HashSet<RuntimeSetting>();
112+
113+
HttpHeaders headers = new HttpHeaders();
114+
headers.setContentType(MediaType.APPLICATION_JSON);
115+
116+
HttpEntity<Set<RuntimeSetting>> createProxyRequest = new HttpEntity<Set<RuntimeSetting>>(createProxyBody,
117+
headers);
118+
119+
ResponseEntity<Proxy> createProxyResponse = this.restTemplate.withBasicAuth(username, password).postForEntity(
120+
"http://localhost:" + port + "/api/proxy/{proxySpecId}", createProxyRequest, Proxy.class, specId);
121+
assertEquals(201, createProxyResponse.getStatusCodeValue());
122+
123+
Proxy createdProxy = createProxyResponse.getBody();
108124
Thread.sleep(1000);
109-
Map<?,?> targets = (Map<?,?>) proxyInfo.get("targets");
110-
String endpoint = targets.keySet().iterator().next().toString() + "/";
111-
doGetEndpoint(endpoint, jSessionId);
112-
return new HashMap<String, String>() {
113-
{
114-
put("proxyId", (String)proxyInfo.get("id"));
115-
put("jSessionId", jSessionId);
116-
}
117-
};
118-
}
119-
120-
private String doFormLogin(String username, String password) {
121-
String setCookie = webClient.post()
122-
.uri("/login")
123-
.body(BodyInserters.fromFormData("username", username).with("password", password))
124-
.exchange()
125-
.expectStatus().isEqualTo(302)
126-
.expectHeader().exists("Set-Cookie")
127-
.returnResult(String.class).getResponseHeaders().getFirst("Set-Cookie");
128-
129-
String jSessionId = setCookie.split("=")[1];
130-
return jSessionId.substring(0, jSessionId.indexOf(';'));
131-
}
132-
133-
private Map<?,?> doLaunchProxy(String specId, String jSessionId) {
134-
return webClient.post()
135-
.uri("/api/proxy/" + specId)
136-
.cookie("JSESSIONID", jSessionId)
137-
.header("Content-Type", MediaType.APPLICATION_JSON.toString())
138-
.body(BodyInserters.fromObject("[]"))
139-
.exchange()
140-
.expectStatus().isEqualTo(201)
141-
.returnResult(Map.class).getResponseBody().blockFirst();
125+
String endpoint = createdProxy.getTargets().keySet().iterator().next().toString() + "/";
126+
doGetEndpoint(endpoint, username, password);
127+
128+
return createdProxy.getId();
142129
}
143-
144-
private void doDeleteProxy(String proxyId, String jSessionId) {
145-
webClient.delete()
146-
.uri("/api/proxy/" + proxyId)
147-
.cookie("JSESSIONID", jSessionId)
148-
.exchange()
149-
.expectStatus().isEqualTo(200)
150-
.returnResult(String.class).getResponseBodyContent();
130+
131+
private void doDeleteProxy(String proxyId, String username, String password) {
132+
HttpHeaders headers = new HttpHeaders();
133+
headers.setContentType(MediaType.APPLICATION_JSON);
134+
135+
ResponseEntity<String> deleteProxyResponse = this.restTemplate
136+
.withBasicAuth(username, password)
137+
.exchange("http://localhost:" + port + "/api/proxy/{proxyId}", HttpMethod.DELETE, null, String.class, proxyId);
138+
139+
assertEquals(200, deleteProxyResponse.getStatusCodeValue());
151140
}
152-
153-
private byte[] doGetEndpoint(String endpoint, String jSessionId) {
154-
return webClient.get()
155-
.uri("/api/route/" + endpoint)
156-
.cookie("JSESSIONID", jSessionId)
157-
.exchange()
158-
.expectStatus().isEqualTo(200)
159-
.returnResult(String.class).getResponseBodyContent();
141+
142+
private byte[] doGetEndpoint(String endpoint, String username, String password) {
143+
HttpHeaders headers = new HttpHeaders();
144+
headers.setContentType(MediaType.APPLICATION_JSON);
145+
146+
ResponseEntity<String> getEndpointResponse = this.restTemplate.withBasicAuth(username, password)
147+
.getForEntity("http://localhost:" + port + "/api/route/" + endpoint, String.class);
148+
assertEquals(200, getEndpointResponse.getStatusCodeValue());
149+
150+
return getEndpointResponse.getBody().getBytes();
160151
}
161152
}

0 commit comments

Comments
 (0)