JAVA-84: Moved 3 articles to spring-boot-libraries
This commit is contained in:
@@ -12,3 +12,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Generating Barcodes and QR Codes in Java](https://www.baeldung.com/java-generating-barcodes-qr-codes)
|
||||
- [Rate Limiting a Spring API Using Bucket4j](https://www.baeldung.com/spring-bucket4j)
|
||||
- [Spring Boot and Caffeine Cache](https://www.baeldung.com/spring-boot-caffeine-cache)
|
||||
- [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz)
|
||||
- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
|
||||
- [An Introduction to Kong](https://www.baeldung.com/kong)
|
||||
|
||||
@@ -37,6 +37,36 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- togglez -->
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-boot-starter</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-security</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- graphql -->
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-spring-boot-starter</artifactId>
|
||||
<version>${graphql-spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-tools</artifactId>
|
||||
<version>${graphql-java-tools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphiql-spring-boot-starter</artifactId>
|
||||
<version>${graphql-spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Problem Spring Web -->
|
||||
<dependency>
|
||||
@@ -216,7 +246,6 @@
|
||||
<rome.version>1.9.0</rome.version>
|
||||
<chaos.monkey.version>2.0.0</chaos.monkey.version>
|
||||
<graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version>
|
||||
<graphiql-spring-boot-starter.version>5.0.2</graphiql-spring-boot-starter.version>
|
||||
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
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) {
|
||||
System.setProperty("spring.config.name", "demo");
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
public class Author {
|
||||
private String id;
|
||||
private String name;
|
||||
private String thumbnail;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getThumbnail() {
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
public void setThumbnail(String thumbnail) {
|
||||
this.thumbnail = thumbnail;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AuthorDao {
|
||||
private List<Author> authors;
|
||||
|
||||
public AuthorDao(List<Author> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public Optional<Author> getAuthor(String id) {
|
||||
return authors.stream().filter(author -> id.equals(author.getId())).findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
|
||||
public class AuthorResolver implements GraphQLResolver<Author> {
|
||||
private PostDao postDao;
|
||||
|
||||
public AuthorResolver(PostDao postDao) {
|
||||
this.postDao = postDao;
|
||||
}
|
||||
|
||||
public List<Post> getPosts(Author author) {
|
||||
return postDao.getAuthorPosts(author.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class GraphqlConfiguration {
|
||||
@Bean
|
||||
public PostDao postDao() {
|
||||
List<Post> posts = new ArrayList<>();
|
||||
for (int postId = 0; postId < 10; ++postId) {
|
||||
for (int authorId = 0; authorId < 10; ++authorId) {
|
||||
Post post = new Post();
|
||||
post.setId("Post" + authorId + postId);
|
||||
post.setTitle("Post " + authorId + ":" + postId);
|
||||
post.setText("Post " + postId + " + by author " + authorId);
|
||||
post.setAuthorId("Author" + authorId);
|
||||
posts.add(post);
|
||||
}
|
||||
}
|
||||
return new PostDao(posts);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthorDao authorDao() {
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (int authorId = 0; authorId < 10; ++authorId) {
|
||||
Author author = new Author();
|
||||
author.setId("Author" + authorId);
|
||||
author.setName("Author " + authorId);
|
||||
author.setThumbnail("http://example.com/authors/" + authorId);
|
||||
authors.add(author);
|
||||
}
|
||||
return new AuthorDao(authors);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PostResolver postResolver(AuthorDao authorDao) {
|
||||
return new PostResolver(authorDao);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthorResolver authorResolver(PostDao postDao) {
|
||||
return new AuthorResolver(postDao);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Query query(PostDao postDao) {
|
||||
return new Query(postDao);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Mutation mutation(PostDao postDao) {
|
||||
return new Mutation(postDao);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
|
||||
public class Mutation implements GraphQLMutationResolver {
|
||||
private PostDao postDao;
|
||||
|
||||
public Mutation(PostDao postDao) {
|
||||
this.postDao = postDao;
|
||||
}
|
||||
|
||||
public Post writePost(String title, String text, String category, String author) {
|
||||
Post post = new Post();
|
||||
post.setId(UUID.randomUUID().toString());
|
||||
post.setTitle(title);
|
||||
post.setText(text);
|
||||
post.setCategory(category);
|
||||
post.setAuthorId(author);
|
||||
postDao.savePost(post);
|
||||
|
||||
return post;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
public class Post {
|
||||
private String id;
|
||||
private String title;
|
||||
private String text;
|
||||
private String category;
|
||||
private String authorId;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public String getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
|
||||
public void setAuthorId(String authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PostDao {
|
||||
private List<Post> posts;
|
||||
|
||||
public PostDao(List<Post> posts) {
|
||||
this.posts = posts;
|
||||
}
|
||||
|
||||
public List<Post> getRecentPosts(int count, int offset) {
|
||||
return posts.stream().skip(offset).limit(count).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Post> getAuthorPosts(String author) {
|
||||
return posts.stream().filter(post -> author.equals(post.getAuthorId())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void savePost(Post post) {
|
||||
posts.add(0, post);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
|
||||
public class PostResolver implements GraphQLResolver<Post> {
|
||||
private AuthorDao authorDao;
|
||||
|
||||
public PostResolver(AuthorDao authorDao) {
|
||||
this.authorDao = authorDao;
|
||||
}
|
||||
|
||||
public Optional<Author> getAuthor(Post post) {
|
||||
return authorDao.getAuthor(post.getAuthorId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
|
||||
public class Query implements GraphQLQueryResolver {
|
||||
private PostDao postDao;
|
||||
|
||||
public Query(PostDao postDao) {
|
||||
this.postDao = postDao;
|
||||
}
|
||||
|
||||
public List<Post> getRecentPosts(int count, int offset) {
|
||||
return postDao.getRecentPosts(count, offset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.kong;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/stock")
|
||||
public class QueryController {
|
||||
|
||||
private static int REQUEST_COUNTER = 0;
|
||||
|
||||
@GetMapping("/reqcount")
|
||||
public int getReqCount() {
|
||||
return REQUEST_COUNTER;
|
||||
}
|
||||
|
||||
@GetMapping("/{code}")
|
||||
public String getStockPrice(@PathVariable String code) {
|
||||
REQUEST_COUNTER++;
|
||||
if ("BTC".equalsIgnoreCase(code))
|
||||
return "10000";
|
||||
else
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.kong;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class StockApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StockApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
private double salary;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(long id, double salary) {
|
||||
this.id = id;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface FeatureAssociation {
|
||||
MyFeatures value();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class FeaturesAspect {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(FeaturesAspect.class);
|
||||
|
||||
@Around(value = "@within(featureAssociation) || @annotation(featureAssociation)")
|
||||
public Object checkAspect(ProceedingJoinPoint joinPoint, FeatureAssociation featureAssociation) throws Throwable {
|
||||
if (featureAssociation.value().isActive()) {
|
||||
return joinPoint.proceed();
|
||||
} else {
|
||||
LOG.info("Feature " + featureAssociation.value().name() + " is not enabled!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.togglz.core.Feature;
|
||||
import org.togglz.core.activation.SystemPropertyActivationStrategy;
|
||||
import org.togglz.core.annotation.ActivationParameter;
|
||||
import org.togglz.core.annotation.DefaultActivationStrategy;
|
||||
import org.togglz.core.annotation.EnabledByDefault;
|
||||
import org.togglz.core.annotation.Label;
|
||||
import org.togglz.core.context.FeatureContext;
|
||||
|
||||
public enum MyFeatures implements Feature {
|
||||
|
||||
@Label("Employee Management Feature")
|
||||
@EnabledByDefault
|
||||
@DefaultActivationStrategy(id = SystemPropertyActivationStrategy.ID, parameters = { @ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_NAME, value = "employee.feature"),
|
||||
@ActivationParameter(name = SystemPropertyActivationStrategy.PARAM_PROPERTY_VALUE, value = "true") })
|
||||
EMPLOYEE_MANAGEMENT_FEATURE;
|
||||
|
||||
public boolean isActive() {
|
||||
return FeatureContext.getFeatureManager().isActive(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class SalaryController {
|
||||
|
||||
@Autowired
|
||||
SalaryService salaryService;
|
||||
|
||||
@PostMapping(value = "/increaseSalary")
|
||||
@ResponseBody
|
||||
public void increaseSalary(@RequestParam long id) {
|
||||
salaryService.increaseSalary(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SalaryService {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
@FeatureAssociation(value = MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE)
|
||||
public void increaseSalary(long id) {
|
||||
Employee employee = employeeRepository.findById(id).orElse(null);
|
||||
employee.setSalary(employee.getSalary() + employee.getSalary() * 0.1);
|
||||
employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ToggleApplication {
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ToggleApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.toggle;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.togglz.core.manager.EnumBasedFeatureProvider;
|
||||
import org.togglz.core.spi.FeatureProvider;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories("com.baeldung.toggle")
|
||||
@EntityScan("com.baeldung.toggle")
|
||||
public class ToggleConfiguration {
|
||||
|
||||
@Bean
|
||||
public FeatureProvider featureProvider() {
|
||||
return new EnumBasedFeatureProvider(MyFeatures.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user