JAVA-84: Moved 1 article to spring-boot-testing
This commit is contained in:
parent
52721a78d6
commit
5749d7ad23
@ -14,3 +14,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Embedded Redis Server with Spring Boot Test](https://www.baeldung.com/spring-embedded-redis)
|
||||
- [Testing Spring Boot @ConfigurationProperties](https://www.baeldung.com/spring-boot-testing-configurationproperties)
|
||||
- [Prevent ApplicationRunner or CommandLineRunner Beans From Executing During Junit Testing](https://www.baeldung.com/spring-junit-prevent-runner-beans-testing-execution)
|
||||
- [Testing in Spring Boot](https://www.baeldung.com/spring-boot-testing)
|
||||
|
@ -34,6 +34,14 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -4,4 +4,6 @@ spring.redis.port= 6379
|
||||
|
||||
# security
|
||||
spring.security.user.name=john
|
||||
spring.security.user.password=123
|
||||
spring.security.user.password=123
|
||||
|
||||
spring.main.allow-bean-definition-overriding=true
|
@ -0,0 +1,8 @@
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=sa
|
||||
jdbc.pass=sa
|
||||
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
@ -1,19 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.verification.VerificationModeFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@ -25,8 +10,28 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.verification.VerificationModeFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRestController;
|
||||
import com.baeldung.boot.testing.EmployeeService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(EmployeeRestController.class)
|
||||
@WebMvcTest(value = EmployeeRestController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
public class EmployeeControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
@ -1,7 +1,5 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import com.baeldung.demo.boottest.Employee;
|
||||
import com.baeldung.demo.boottest.EmployeeRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -9,6 +7,9 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
@ -14,11 +14,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.demo.DemoApplication;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -27,9 +28,14 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
||||
// @TestPropertySource(locations = "classpath:application-integrationtest.properties")
|
||||
@AutoConfigureTestDatabase
|
||||
public class EmployeeRestControllerIntegrationTest {
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -6,10 +6,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.demo.boottest.Employee;
|
||||
import com.baeldung.demo.boottest.EmployeeRepository;
|
||||
import com.baeldung.demo.boottest.EmployeeService;
|
||||
import com.baeldung.demo.boottest.EmployeeServiceImpl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -21,6 +17,11 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
import com.baeldung.boot.testing.EmployeeService;
|
||||
import com.baeldung.boot.testing.EmployeeServiceImpl;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class EmployeeServiceImplIntegrationTest {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.demo.boottest;
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
@ -3,4 +3,6 @@ spring.redis.host= localhost
|
||||
spring.redis.port= 6370
|
||||
# security
|
||||
spring.security.user.name=john
|
||||
spring.security.user.password=123
|
||||
spring.security.user.password=123
|
||||
|
||||
spring.main.allow-bean-definition-overriding=true
|
@ -14,12 +14,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Guide to Internationalization in Spring Boot](https://www.baeldung.com/spring-boot-internationalization)
|
||||
- [Dynamic DTO Validation Config Retrieved from the Database](https://www.baeldung.com/spring-dynamic-dto-validation)
|
||||
- [Custom Information in Spring Boot Info Endpoint](https://www.baeldung.com/spring-boot-info-actuator-custom)
|
||||
- [Testing in Spring Boot](https://www.baeldung.com/spring-boot-testing)
|
||||
- [How to Get All Spring-Managed Beans?](https://www.baeldung.com/spring-show-all-beans)
|
||||
- [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz)
|
||||
- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
|
||||
- [Guide to Spring Type Conversions](https://www.baeldung.com/spring-type-conversions)
|
||||
- [An Introduction to Kong](https://www.baeldung.com/kong)
|
||||
- [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class)
|
||||
- [A Quick Intro to the SpringBootServletInitializer](https://www.baeldung.com/spring-boot-servlet-initializer)
|
||||
- [Spring Shutdown Callbacks](https://www.baeldung.com/spring-shutdown-callbacks)
|
@ -1,13 +1,9 @@
|
||||
package com.baeldung.demo;
|
||||
|
||||
import com.baeldung.graphql.GraphqlConfiguration;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(GraphqlConfiguration.class)
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -1,170 +0,0 @@
|
||||
package com.baeldung.kong;
|
||||
|
||||
import com.baeldung.kong.domain.APIObject;
|
||||
import com.baeldung.kong.domain.ConsumerObject;
|
||||
import com.baeldung.kong.domain.KeyAuthObject;
|
||||
import com.baeldung.kong.domain.PluginObject;
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
|
||||
public class KongAdminAPILiveTest {
|
||||
|
||||
private String getStockPrice(String code) {
|
||||
try {
|
||||
return restTemplate.getForObject(new URI("http://localhost:8080/stock/" + code), String.class);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() {
|
||||
String response = getStockPrice("btc");
|
||||
assertEquals("10000", response);
|
||||
|
||||
response = getStockPrice("eth");
|
||||
assertEquals("N/A", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
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:9090", "/");
|
||||
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
|
||||
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||
|
||||
assertEquals(HttpStatus.CREATED, addAPIResp.getStatusCode());
|
||||
|
||||
addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||
assertEquals(HttpStatus.CONFLICT, addAPIResp.getStatusCode());
|
||||
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||
|
||||
assertTrue(apiListResp.contains("stock-api"));
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Host", "stock.api");
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKongAdminAPI_whenAddAPIConsumer_thenAdded() {
|
||||
restTemplate.delete("http://localhost:8001/consumers/eugenp");
|
||||
|
||||
ConsumerObject consumer = new ConsumerObject("eugenp");
|
||||
HttpEntity<ConsumerObject> addConsumerEntity = new HttpEntity<>(consumer);
|
||||
ResponseEntity<String> addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers/", addConsumerEntity, String.class);
|
||||
|
||||
assertEquals(HttpStatus.CREATED, addConsumerResp.getStatusCode());
|
||||
|
||||
addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers", addConsumerEntity, String.class);
|
||||
assertEquals(HttpStatus.CONFLICT, addConsumerResp.getStatusCode());
|
||||
|
||||
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
|
||||
assertTrue(consumerListResp.contains("eugenp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPI_whenEnableAuth_thenAnonymousDenied() throws Exception {
|
||||
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||
if (!apiListResp.contains("stock-api")) {
|
||||
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
String pluginsResp = restTemplate.getForObject("http://localhost:8001/apis/stock-api/plugins", String.class);
|
||||
assertTrue(pluginsResp.contains("key-auth"));
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Host", "stock.api");
|
||||
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, stockPriceResp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception {
|
||||
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||
if (!apiListResp.contains("stock-api")) {
|
||||
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
|
||||
}
|
||||
|
||||
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
|
||||
if (!consumerListResp.contains("eugenp")) {
|
||||
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/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/springbootapp/stock/btc"));
|
||||
stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||
assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAdminAPIProxy_whenAddAPIViaProxy_thenAPIAdded() throws Exception {
|
||||
APIObject adminAPI = new APIObject("admin-api", "admin.api", "http://localhost:8001", "/admin-api");
|
||||
HttpEntity<APIObject> apiEntity = new HttpEntity<>(adminAPI);
|
||||
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||
|
||||
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Host", "admin.api");
|
||||
APIObject baeldungAPI = new APIObject("baeldung-api", "baeldung.com", "http://ww.baeldung.com", "/");
|
||||
RequestEntity<APIObject> requestEntity = new RequestEntity<>(baeldungAPI, headers, HttpMethod.POST, new URI("http://localhost:8000/admin-api/apis"));
|
||||
addAPIResp = restTemplate.exchange(requestEntity, String.class);
|
||||
|
||||
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||
}
|
||||
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package com.baeldung.kong;
|
||||
|
||||
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.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 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, properties = "server.servlet.contextPath=/springbootapp")
|
||||
public class KongLoadBalanceLiveTest {
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
|
||||
UpstreamObject upstream = new UpstreamObject("stock.api.service");
|
||||
ResponseEntity<String> addUpstreamResp = restTemplate.postForEntity("http://localhost:8001/upstreams", new HttpEntity<>(upstream), String.class);
|
||||
assertTrue(HttpStatus.CREATED == addUpstreamResp.getStatusCode() || HttpStatus.CONFLICT == addUpstreamResp.getStatusCode());
|
||||
|
||||
TargetObject testTarget = new TargetObject("localhost:8080", 10);
|
||||
ResponseEntity<String> addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(testTarget), String.class);
|
||||
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
|
||||
|
||||
TargetObject releaseTarget = new TargetObject("localhost:9090", 40);
|
||||
addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(releaseTarget), String.class);
|
||||
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
|
||||
|
||||
APIObject stockAPI = new APIObject("balanced-stock-api", "balanced.stock.api", "http://stock.api.service", "/");
|
||||
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
|
||||
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||
|
||||
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/springbootapp/stock/btc"));
|
||||
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||
assertEquals("10000", stockPriceResp.getBody());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class APIObject {
|
||||
|
||||
public APIObject() {
|
||||
}
|
||||
|
||||
public APIObject(String name, String hosts, String upstream_url, String uris) {
|
||||
this.name = name;
|
||||
this.hosts = hosts;
|
||||
this.upstream_url = upstream_url;
|
||||
this.uris = uris;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private String hosts;
|
||||
private String upstream_url;
|
||||
private String uris;
|
||||
|
||||
public String getUris() {
|
||||
return uris;
|
||||
}
|
||||
|
||||
public void setUris(String uris) {
|
||||
this.uris = uris;
|
||||
}
|
||||
|
||||
public String getUpstream_url() {
|
||||
return upstream_url;
|
||||
}
|
||||
|
||||
public void setUpstream_url(String upstream_url) {
|
||||
this.upstream_url = upstream_url;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getHosts() {
|
||||
return hosts;
|
||||
}
|
||||
|
||||
public void setHosts(String hosts) {
|
||||
this.hosts = hosts;
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class ConsumerObject {
|
||||
|
||||
private String username;
|
||||
private String custom_id;
|
||||
|
||||
public ConsumerObject(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public ConsumerObject(String username, String custom_id) {
|
||||
this.username = username;
|
||||
this.custom_id = custom_id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getCustom_id() {
|
||||
return custom_id;
|
||||
}
|
||||
|
||||
public void setCustom_id(String custom_id) {
|
||||
this.custom_id = custom_id;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class KeyAuthObject {
|
||||
|
||||
public KeyAuthObject(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
private String key;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class PluginObject {
|
||||
|
||||
private String name;
|
||||
private String consumer_id;
|
||||
|
||||
public PluginObject(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getConsumer_id() {
|
||||
return consumer_id;
|
||||
}
|
||||
|
||||
public void setConsumer_id(String consumer_id) {
|
||||
this.consumer_id = consumer_id;
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class TargetObject {
|
||||
|
||||
public TargetObject(String target, int weight) {
|
||||
this.target = target;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
private String target;
|
||||
private int weight;
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.baeldung.kong.domain;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class UpstreamObject {
|
||||
|
||||
public UpstreamObject(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = ToggleApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class ToggleIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFeaturePropertyFalse_whenIncreaseSalary_thenNoIncrease() throws Exception {
|
||||
Employee emp = new Employee(1, 2000);
|
||||
employeeRepository.save(emp);
|
||||
|
||||
System.setProperty("employee.feature", "false");
|
||||
|
||||
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
|
||||
|
||||
emp = employeeRepository.findById(1L).orElse(null);
|
||||
assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFeaturePropertyTrue_whenIncreaseSalary_thenIncrease() throws Exception {
|
||||
Employee emp = new Employee(1, 2000);
|
||||
employeeRepository.save(emp);
|
||||
|
||||
System.setProperty("employee.feature", "true");
|
||||
|
||||
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
|
||||
|
||||
emp = employeeRepository.findById(1L).orElse(null);
|
||||
assertEquals("salary incorrect", 2200, emp.getSalary(), 0.5);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user