Skip to content

Commit de37cc8

Browse files
committed
Fixed LiveTests for migration of spring-boot module to Spring Boot 2
1 parent 47735dc commit de37cc8

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎spring-boot/src/test/java/com/baeldung/kong/KongAdminAPILiveTest.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() {
5555
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
5656
restTemplate.delete("http://localhost:8001/apis/stock-api");
5757

58-
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/");
58+
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:9090", "/");
5959
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
6060
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
6161

@@ -69,7 +69,7 @@ public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Excep
6969

7070
HttpHeaders headers = new HttpHeaders();
7171
headers.set("Host", "stock.api");
72-
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
72+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
7373
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
7474

7575
assertEquals("10000", stockPriceResp.getBody());
@@ -126,22 +126,26 @@ public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception
126126
givenKongAdminAPI_whenAddAPIConsumer_thenAdded();
127127
}
128128

129+
PluginObject authPlugin = new PluginObject("key-auth");
130+
ResponseEntity<String> enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class);
131+
assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode());
132+
129133
final String consumerKey = "eugenp.pass";
130134
KeyAuthObject keyAuth = new KeyAuthObject(consumerKey);
131135
ResponseEntity<String> keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class);
132-
136+
133137
assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode());
134138

135139
HttpHeaders headers = new HttpHeaders();
136140
headers.set("Host", "stock.api");
137141
headers.set("apikey", consumerKey);
138-
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
142+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
139143
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
140144

141145
assertEquals("10000", stockPriceResp.getBody());
142146

143147
headers.set("apikey", "wrongpass");
144-
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
148+
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
145149
stockPriceResp = restTemplate.exchange(requestEntity, String.class);
146150
assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode());
147151
}

‎spring-boot/src/test/java/com/baeldung/kong/KongLoadBalanceLiveTest.java‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
package com.baeldung.kong;
22

3-
import com.baeldung.kong.domain.APIObject;
4-
import com.baeldung.kong.domain.TargetObject;
5-
import com.baeldung.kong.domain.UpstreamObject;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
6+
7+
import java.net.URI;
8+
69
import org.junit.Before;
710
import org.junit.Test;
811
import org.junit.runner.RunWith;
912
import org.springframework.beans.factory.annotation.Autowired;
1013
import org.springframework.boot.test.context.SpringBootTest;
1114
import org.springframework.boot.test.web.client.TestRestTemplate;
12-
import org.springframework.http.*;
15+
import org.springframework.http.HttpEntity;
16+
import org.springframework.http.HttpHeaders;
17+
import org.springframework.http.HttpMethod;
18+
import org.springframework.http.HttpStatus;
19+
import org.springframework.http.RequestEntity;
20+
import org.springframework.http.ResponseEntity;
1321
import org.springframework.test.context.junit4.SpringRunner;
1422

15-
import java.net.URI;
16-
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
19-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
23+
import com.baeldung.kong.domain.APIObject;
24+
import com.baeldung.kong.domain.TargetObject;
25+
import com.baeldung.kong.domain.UpstreamObject;
2026

2127
/**
2228
* @author aiet
2329
*/
2430
@RunWith(SpringRunner.class)
25-
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
31+
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class, properties = "server.servlet.contextPath=/springbootapp")
2632
public class KongLoadBalanceLiveTest {
2733

2834
@Before
@@ -55,13 +61,13 @@ public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Excep
5561
HttpHeaders headers = new HttpHeaders();
5662
headers.set("Host", "balanced.stock.api");
5763
for (int i = 0; i < 1000; i++) {
58-
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
64+
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
5965
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
6066
assertEquals("10000", stockPriceResp.getBody());
6167
}
6268

63-
int releaseCount = restTemplate.getForObject("http://localhost:9090/stock/reqcount", Integer.class);
64-
int testCount = restTemplate.getForObject("http://localhost:8080/stock/reqcount", Integer.class);
69+
int releaseCount = restTemplate.getForObject("http://localhost:9090/springbootapp/stock/reqcount", Integer.class);
70+
int testCount = restTemplate.getForObject("http://localhost:8080/springbootapp/stock/reqcount", Integer.class);
6571

6672
assertTrue(Math.round(releaseCount * 1.0 / testCount) == 4);
6773
}

0 commit comments

Comments
 (0)