Feature/bael 338 introduction dropwizard (#8572)

* [ BAEL-3388 ] : Introduction Dropwizard

* BAEL-3388: Fix formatting
This commit is contained in:
Lukasz Rys 2020-01-24 22:00:06 +01:00 committed by maibin
parent a861aa604b
commit 27315afad4
11 changed files with 286 additions and 0 deletions

1
dropwizard/README.md Normal file
View File

@ -0,0 +1 @@
# Dropwizard

68
dropwizard/pom.xml Normal file
View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>dropwizard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dropwizard</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.baeldung.dropwizard.introduction.IntroductionApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<dropwizard.version>2.0.0</dropwizard.version>
</properties>
</project>

View File

@ -0,0 +1,51 @@
package com.baeldung.dropwizard.introduction;
import com.baeldung.dropwizard.introduction.configuration.ApplicationHealthCheck;
import com.baeldung.dropwizard.introduction.configuration.BasicConfiguration;
import com.baeldung.dropwizard.introduction.domain.Brand;
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
import com.baeldung.dropwizard.introduction.resource.BrandResource;
import io.dropwizard.Application;
import io.dropwizard.configuration.ResourceConfigurationSourceProvider;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.util.ArrayList;
import java.util.List;
public class IntroductionApplication extends Application<BasicConfiguration> {
public static void main(final String[] args) throws Exception {
new IntroductionApplication().run("server", "introduction-config.yml");
}
@Override
public void run(final BasicConfiguration basicConfiguration, final Environment environment) {
final int defaultSize = basicConfiguration.getDefaultSize();
final BrandRepository brandRepository = new BrandRepository(initBrands());
final BrandResource brandResource = new BrandResource(defaultSize, brandRepository);
environment
.jersey()
.register(brandResource);
final ApplicationHealthCheck healthCheck = new ApplicationHealthCheck();
environment
.healthChecks()
.register("application", healthCheck);
}
@Override
public void initialize(final Bootstrap<BasicConfiguration> bootstrap) {
bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
super.initialize(bootstrap);
}
private List<Brand> initBrands() {
final List<Brand> brands = new ArrayList<>();
brands.add(new Brand(1L, "Brand1"));
brands.add(new Brand(2L, "Brand2"));
brands.add(new Brand(3L, "Brand3"));
return brands;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.dropwizard.introduction.configuration;
import com.codahale.metrics.health.HealthCheck;
public class ApplicationHealthCheck extends HealthCheck {
@Override
protected Result check() throws Exception {
return Result.healthy();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.dropwizard.introduction.configuration;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import javax.validation.constraints.NotNull;
public class BasicConfiguration extends Configuration {
@NotNull private final int defaultSize;
@JsonCreator
public BasicConfiguration(@JsonProperty("defaultSize") final int defaultSize) {
this.defaultSize = defaultSize;
}
public int getDefaultSize() {
return defaultSize;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.dropwizard.introduction.domain;
public class Brand {
private final Long id;
private final String name;
public Brand(final Long id, final String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.dropwizard.introduction.repository;
import com.baeldung.dropwizard.introduction.domain.Brand;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class BrandRepository {
private final List<Brand> brands;
public BrandRepository(final List<Brand> brands) {
this.brands = ImmutableList.copyOf(brands);
}
public List<Brand> findAll(final int size) {
return brands
.stream()
.limit(size)
.collect(Collectors.toList());
}
public Optional<Brand> findById(final Long id) {
return brands
.stream()
.filter(brand -> brand
.getId()
.equals(id))
.findFirst();
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.dropwizard.introduction.resource;
import com.baeldung.dropwizard.introduction.domain.Brand;
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Optional;
@Path("/brands")
@Produces(MediaType.APPLICATION_JSON)
public class BrandResource {
private final int defaultSize;
private final BrandRepository brandRepository;
public BrandResource(final int defaultSize, final BrandRepository brandRepository) {
this.defaultSize = defaultSize;
this.brandRepository = brandRepository;
}
@GET
public List<Brand> getBrands(@QueryParam("size") final Optional<Integer> size) {
return brandRepository.findAll(size.orElse(defaultSize));
}
@GET
@Path("/{id}")
public Brand getById(@PathParam("id") final Long id) {
return brandRepository
.findById(id)
.orElseThrow(RuntimeException::new);
}
}

View File

@ -0,0 +1 @@
defaultSize: 5

View File

@ -0,0 +1,47 @@
package com.baeldung.dropwizard.introduction.repository;
import com.baeldung.dropwizard.introduction.domain.Brand;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
class BrandRepositoryUnitTest {
private static final Brand BRAND_1 = new Brand(1L, "Brand1");
private final BrandRepository brandRepository = new BrandRepository(getBrands());
@Test
void givenSize_whenFindingAll_thenReturnList() {
final int size = 2;
final List<Brand> result = brandRepository.findAll(size);
assertEquals(size, result.size());
}
@Test
void givenId_whenFindingById_thenReturnFoundBrand() {
final Long id = BRAND_1.getId();
final Optional<Brand> result = brandRepository.findById(id);
Assertions.assertTrue(result.isPresent());
assertEquals(BRAND_1, result.get());
}
private List<Brand> getBrands() {
final List<Brand> brands = new ArrayList<>();
brands.add(BRAND_1);
brands.add(new Brand(2L, "Brand2"));
brands.add(new Brand(3L, "Brand3"));
return brands;
}
}

View File

@ -412,6 +412,7 @@
<module>disruptor</module> <module>disruptor</module>
<module>dozer</module> <module>dozer</module>
<module>drools</module> <module>drools</module>
<module>dropwizard</module>
<module>dubbo</module> <module>dubbo</module>
<module>ethereum</module> <module>ethereum</module>
@ -962,6 +963,7 @@
<module>disruptor</module> <module>disruptor</module>
<module>dozer</module> <module>dozer</module>
<module>drools</module> <module>drools</module>
<module>dropwizard</module>
<module>dubbo</module> <module>dubbo</module>
<module>ethereum</module> <module>ethereum</module>