Fixed LiveTests for migration of spring-boot module to Spring Boot 2

This commit is contained in:
geroza 2018-11-17 01:14:10 -02:00
parent 47735dcd70
commit de37cc8af1
2 changed files with 28 additions and 18 deletions

View File

@ -55,7 +55,7 @@ public class KongAdminAPILiveTest {
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
restTemplate.delete("http://localhost:8001/apis/stock-api");
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/");
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:9090", "/");
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
@ -69,7 +69,7 @@ public class KongAdminAPILiveTest {
HttpHeaders headers = new HttpHeaders();
headers.set("Host", "stock.api");
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
assertEquals("10000", stockPriceResp.getBody());
@ -126,22 +126,26 @@ public class KongAdminAPILiveTest {
givenKongAdminAPI_whenAddAPIConsumer_thenAdded();
}
PluginObject authPlugin = new PluginObject("key-auth");
ResponseEntity<String> enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class);
assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode());
final String consumerKey = "eugenp.pass";
KeyAuthObject keyAuth = new KeyAuthObject(consumerKey);
ResponseEntity<String> keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class);
assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode());
HttpHeaders headers = new HttpHeaders();
headers.set("Host", "stock.api");
headers.set("apikey", consumerKey);
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
assertEquals("10000", stockPriceResp.getBody());
headers.set("apikey", "wrongpass");
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
stockPriceResp = restTemplate.exchange(requestEntity, String.class);
assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode());
}

View File

@ -1,28 +1,34 @@
package com.baeldung.kong;
import com.baeldung.kong.domain.APIObject;
import com.baeldung.kong.domain.TargetObject;
import com.baeldung.kong.domain.UpstreamObject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import java.net.URI;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import com.baeldung.kong.domain.APIObject;
import com.baeldung.kong.domain.TargetObject;
import com.baeldung.kong.domain.UpstreamObject;
/**
* @author aiet
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class, properties = "server.servlet.contextPath=/springbootapp")
public class KongLoadBalanceLiveTest {
@Before
@ -55,13 +61,13 @@ public class KongLoadBalanceLiveTest {
HttpHeaders headers = new HttpHeaders();
headers.set("Host", "balanced.stock.api");
for (int i = 0; i < 1000; i++) {
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/springbootapp/stock/btc"));
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
assertEquals("10000", stockPriceResp.getBody());
}
int releaseCount = restTemplate.getForObject("http://localhost:9090/stock/reqcount", Integer.class);
int testCount = restTemplate.getForObject("http://localhost:8080/stock/reqcount", Integer.class);
int releaseCount = restTemplate.getForObject("http://localhost:9090/springbootapp/stock/reqcount", Integer.class);
int testCount = restTemplate.getForObject("http://localhost:8080/springbootapp/stock/reqcount", Integer.class);
assertTrue(Math.round(releaseCount * 1.0 / testCount) == 4);
}