From 27315afad4ad6113367d9e2ebcc3beaedabdb179 Mon Sep 17 00:00:00 2001 From: Lukasz Rys Date: Fri, 24 Jan 2020 22:00:06 +0100 Subject: [PATCH] Feature/bael 338 introduction dropwizard (#8572) * [ BAEL-3388 ] : Introduction Dropwizard * BAEL-3388: Fix formatting --- dropwizard/README.md | 1 + dropwizard/pom.xml | 68 +++++++++++++++++++ .../introduction/IntroductionApplication.java | 51 ++++++++++++++ .../configuration/ApplicationHealthCheck.java | 10 +++ .../configuration/BasicConfiguration.java | 20 ++++++ .../dropwizard/introduction/domain/Brand.java | 19 ++++++ .../repository/BrandRepository.java | 32 +++++++++ .../introduction/resource/BrandResource.java | 35 ++++++++++ .../main/resources/introduction-config.yml | 1 + .../repository/BrandRepositoryUnitTest.java | 47 +++++++++++++ pom.xml | 2 + 11 files changed, 286 insertions(+) create mode 100644 dropwizard/README.md create mode 100644 dropwizard/pom.xml create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/IntroductionApplication.java create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/ApplicationHealthCheck.java create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/BasicConfiguration.java create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/domain/Brand.java create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/repository/BrandRepository.java create mode 100644 dropwizard/src/main/java/com/baeldung/dropwizard/introduction/resource/BrandResource.java create mode 100644 dropwizard/src/main/resources/introduction-config.yml create mode 100644 dropwizard/src/test/java/com/baeldung/dropwizard/introduction/repository/BrandRepositoryUnitTest.java diff --git a/dropwizard/README.md b/dropwizard/README.md new file mode 100644 index 0000000000..e713b2f1e6 --- /dev/null +++ b/dropwizard/README.md @@ -0,0 +1 @@ +# Dropwizard \ No newline at end of file diff --git a/dropwizard/pom.xml b/dropwizard/pom.xml new file mode 100644 index 0000000000..ddc9aa1949 --- /dev/null +++ b/dropwizard/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + dropwizard + 0.0.1-SNAPSHOT + dropwizard + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + io.dropwizard + dropwizard-core + ${dropwizard.version} + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + package + + shade + + + + + + com.baeldung.dropwizard.introduction.IntroductionApplication + + + + + + + + + + + 2.0.0 + + + \ No newline at end of file diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/IntroductionApplication.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/IntroductionApplication.java new file mode 100644 index 0000000000..d9af590017 --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/IntroductionApplication.java @@ -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 { + + 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 bootstrap) { + bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider()); + super.initialize(bootstrap); + } + + private List initBrands() { + final List brands = new ArrayList<>(); + brands.add(new Brand(1L, "Brand1")); + brands.add(new Brand(2L, "Brand2")); + brands.add(new Brand(3L, "Brand3")); + + return brands; + } +} diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/ApplicationHealthCheck.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/ApplicationHealthCheck.java new file mode 100644 index 0000000000..bf4b710937 --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/ApplicationHealthCheck.java @@ -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(); + } +} diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/BasicConfiguration.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/BasicConfiguration.java new file mode 100644 index 0000000000..5098f89d62 --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/configuration/BasicConfiguration.java @@ -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; + } +} diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/domain/Brand.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/domain/Brand.java new file mode 100644 index 0000000000..c83f67bb6e --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/domain/Brand.java @@ -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; + } +} diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/repository/BrandRepository.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/repository/BrandRepository.java new file mode 100644 index 0000000000..3f187df3de --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/repository/BrandRepository.java @@ -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 brands; + + public BrandRepository(final List brands) { + this.brands = ImmutableList.copyOf(brands); + } + + public List findAll(final int size) { + return brands + .stream() + .limit(size) + .collect(Collectors.toList()); + } + + public Optional findById(final Long id) { + return brands + .stream() + .filter(brand -> brand + .getId() + .equals(id)) + .findFirst(); + } +} diff --git a/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/resource/BrandResource.java b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/resource/BrandResource.java new file mode 100644 index 0000000000..5f97e26faf --- /dev/null +++ b/dropwizard/src/main/java/com/baeldung/dropwizard/introduction/resource/BrandResource.java @@ -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 getBrands(@QueryParam("size") final Optional 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); + } +} diff --git a/dropwizard/src/main/resources/introduction-config.yml b/dropwizard/src/main/resources/introduction-config.yml new file mode 100644 index 0000000000..02ff36de05 --- /dev/null +++ b/dropwizard/src/main/resources/introduction-config.yml @@ -0,0 +1 @@ +defaultSize: 5 \ No newline at end of file diff --git a/dropwizard/src/test/java/com/baeldung/dropwizard/introduction/repository/BrandRepositoryUnitTest.java b/dropwizard/src/test/java/com/baeldung/dropwizard/introduction/repository/BrandRepositoryUnitTest.java new file mode 100644 index 0000000000..b996883ee5 --- /dev/null +++ b/dropwizard/src/test/java/com/baeldung/dropwizard/introduction/repository/BrandRepositoryUnitTest.java @@ -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 result = brandRepository.findAll(size); + + assertEquals(size, result.size()); + } + + @Test + void givenId_whenFindingById_thenReturnFoundBrand() { + final Long id = BRAND_1.getId(); + + final Optional result = brandRepository.findById(id); + + Assertions.assertTrue(result.isPresent()); + assertEquals(BRAND_1, result.get()); + } + + + private List getBrands() { + final List brands = new ArrayList<>(); + brands.add(BRAND_1); + brands.add(new Brand(2L, "Brand2")); + brands.add(new Brand(3L, "Brand3")); + + return brands; + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8e484640f0..e5cddd5e21 100644 --- a/pom.xml +++ b/pom.xml @@ -412,6 +412,7 @@ disruptor dozer drools + dropwizard dubbo ethereum @@ -962,6 +963,7 @@ disruptor dozer drools + dropwizard dubbo ethereum