|
20 | 20 | */ |
21 | 21 | package eu.openanalytics.containerproxy.test.proxy; |
22 | 22 |
|
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; |
25 | 27 |
|
26 | 28 | import javax.inject.Inject; |
27 | 29 |
|
|
30 | 32 | import org.junit.runner.RunWith; |
31 | 33 | import org.springframework.boot.test.context.SpringBootTest; |
32 | 34 | 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; |
33 | 37 | import org.springframework.core.env.Environment; |
| 38 | +import org.springframework.http.HttpEntity; |
| 39 | +import org.springframework.http.HttpHeaders; |
| 40 | +import org.springframework.http.HttpMethod; |
34 | 41 | import org.springframework.http.MediaType; |
| 42 | +import org.springframework.http.ResponseEntity; |
35 | 43 | import org.springframework.test.context.ActiveProfiles; |
36 | 44 | import org.springframework.test.context.junit4.SpringRunner; |
37 | | -import org.springframework.test.web.reactive.server.WebTestClient; |
38 | | -import org.springframework.web.reactive.function.BodyInserters; |
39 | 45 |
|
40 | 46 | import eu.openanalytics.containerproxy.ContainerProxyApplication; |
| 47 | +import eu.openanalytics.containerproxy.model.runtime.Proxy; |
| 48 | +import eu.openanalytics.containerproxy.model.runtime.RuntimeSetting; |
41 | 49 |
|
42 | | -@SpringBootTest(classes= {ContainerProxyApplication.class}, webEnvironment=WebEnvironment.RANDOM_PORT) |
| 50 | +@SpringBootTest(classes = { ContainerProxyApplication.class }, webEnvironment = WebEnvironment.RANDOM_PORT) |
43 | 51 | @RunWith(SpringRunner.class) |
44 | 52 | @ActiveProfiles("test") |
45 | 53 | public class TestConcurrentUsers { |
46 | 54 |
|
47 | 55 | @Inject |
48 | | - private WebTestClient webClient; |
49 | | - |
| 56 | + private TestRestTemplate restTemplate; |
| 57 | + |
50 | 58 | @Inject |
51 | 59 | private Environment environment; |
52 | | - |
| 60 | + |
| 61 | + @LocalServerPort |
| 62 | + private int port; |
| 63 | + |
53 | 64 | private String[] user1; |
54 | 65 | private String[] user2; |
55 | 66 | private String[] specIds; |
56 | | - |
| 67 | + |
57 | 68 | @Before |
58 | 69 | 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") }; |
71 | 76 | } |
72 | | - |
| 77 | + |
73 | 78 | @Test |
74 | 79 | 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]); |
79 | 84 | } |
80 | | - |
| 85 | + |
81 | 86 | @Test |
82 | 87 | 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]); |
87 | 92 | } |
88 | | - |
| 93 | + |
89 | 94 | @Test |
90 | 95 | 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]); |
95 | 100 | } |
96 | | - |
| 101 | + |
97 | 102 | @Test |
98 | 103 | 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]); |
103 | 108 | } |
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(); |
108 | 124 | 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(); |
142 | 129 | } |
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()); |
151 | 140 | } |
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(); |
160 | 151 | } |
161 | 152 | } |
0 commit comments