Skip to content

Commit 7180d15

Browse files
lukaszrysmaibin
authored andcommitted
Feature/bael 338 introduction dropwizard (eugenp#8572)
* [ BAEL-3388 ] : Introduction Dropwizard * BAEL-3388: Fix formatting
1 parent 6d43a51 commit 7180d15

File tree

11 files changed

+286
-0
lines changed

11 files changed

+286
-0
lines changed

dropwizard/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dropwizard

dropwizard/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>dropwizard</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>dropwizard</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.dropwizard</groupId>
20+
<artifactId>dropwizard-core</artifactId>
21+
<version>${dropwizard.version}</version>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-shade-plugin</artifactId>
30+
<configuration>
31+
<createDependencyReducedPom>true</createDependencyReducedPom>
32+
<filters>
33+
<filter>
34+
<artifact>*:*</artifact>
35+
<excludes>
36+
<exclude>META-INF/*.SF</exclude>
37+
<exclude>META-INF/*.DSA</exclude>
38+
<exclude>META-INF/*.RSA</exclude>
39+
</excludes>
40+
</filter>
41+
</filters>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<phase>package</phase>
46+
<goals>
47+
<goal>shade</goal>
48+
</goals>
49+
<configuration>
50+
<transformers>
51+
<transformer
52+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
53+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
54+
<mainClass>com.baeldung.dropwizard.introduction.IntroductionApplication</mainClass>
55+
</transformer>
56+
</transformers>
57+
</configuration>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
<properties>
65+
<dropwizard.version>2.0.0</dropwizard.version>
66+
</properties>
67+
68+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.dropwizard.introduction;
2+
3+
import com.baeldung.dropwizard.introduction.configuration.ApplicationHealthCheck;
4+
import com.baeldung.dropwizard.introduction.configuration.BasicConfiguration;
5+
import com.baeldung.dropwizard.introduction.domain.Brand;
6+
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
7+
import com.baeldung.dropwizard.introduction.resource.BrandResource;
8+
import io.dropwizard.Application;
9+
import io.dropwizard.configuration.ResourceConfigurationSourceProvider;
10+
import io.dropwizard.setup.Bootstrap;
11+
import io.dropwizard.setup.Environment;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class IntroductionApplication extends Application<BasicConfiguration> {
17+
18+
public static void main(final String[] args) throws Exception {
19+
new IntroductionApplication().run("server", "introduction-config.yml");
20+
}
21+
22+
@Override
23+
public void run(final BasicConfiguration basicConfiguration, final Environment environment) {
24+
final int defaultSize = basicConfiguration.getDefaultSize();
25+
final BrandRepository brandRepository = new BrandRepository(initBrands());
26+
final BrandResource brandResource = new BrandResource(defaultSize, brandRepository);
27+
environment
28+
.jersey()
29+
.register(brandResource);
30+
31+
final ApplicationHealthCheck healthCheck = new ApplicationHealthCheck();
32+
environment
33+
.healthChecks()
34+
.register("application", healthCheck);
35+
}
36+
37+
@Override
38+
public void initialize(final Bootstrap<BasicConfiguration> bootstrap) {
39+
bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
40+
super.initialize(bootstrap);
41+
}
42+
43+
private List<Brand> initBrands() {
44+
final List<Brand> brands = new ArrayList<>();
45+
brands.add(new Brand(1L, "Brand1"));
46+
brands.add(new Brand(2L, "Brand2"));
47+
brands.add(new Brand(3L, "Brand3"));
48+
49+
return brands;
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.dropwizard.introduction.configuration;
2+
3+
import com.codahale.metrics.health.HealthCheck;
4+
5+
public class ApplicationHealthCheck extends HealthCheck {
6+
@Override
7+
protected Result check() throws Exception {
8+
return Result.healthy();
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.dropwizard.introduction.configuration;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import io.dropwizard.Configuration;
6+
7+
import javax.validation.constraints.NotNull;
8+
9+
public class BasicConfiguration extends Configuration {
10+
@NotNull private final int defaultSize;
11+
12+
@JsonCreator
13+
public BasicConfiguration(@JsonProperty("defaultSize") final int defaultSize) {
14+
this.defaultSize = defaultSize;
15+
}
16+
17+
public int getDefaultSize() {
18+
return defaultSize;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.dropwizard.introduction.domain;
2+
3+
public class Brand {
4+
private final Long id;
5+
private final String name;
6+
7+
public Brand(final Long id, final String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
12+
public Long getId() {
13+
return id;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.dropwizard.introduction.repository;
2+
3+
import com.baeldung.dropwizard.introduction.domain.Brand;
4+
import com.google.common.collect.ImmutableList;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Collectors;
9+
10+
public class BrandRepository {
11+
private final List<Brand> brands;
12+
13+
public BrandRepository(final List<Brand> brands) {
14+
this.brands = ImmutableList.copyOf(brands);
15+
}
16+
17+
public List<Brand> findAll(final int size) {
18+
return brands
19+
.stream()
20+
.limit(size)
21+
.collect(Collectors.toList());
22+
}
23+
24+
public Optional<Brand> findById(final Long id) {
25+
return brands
26+
.stream()
27+
.filter(brand -> brand
28+
.getId()
29+
.equals(id))
30+
.findFirst();
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.dropwizard.introduction.resource;
2+
3+
import com.baeldung.dropwizard.introduction.domain.Brand;
4+
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
5+
6+
import javax.ws.rs.*;
7+
import javax.ws.rs.core.MediaType;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
@Path("/brands")
12+
@Produces(MediaType.APPLICATION_JSON)
13+
public class BrandResource {
14+
private final int defaultSize;
15+
private final BrandRepository brandRepository;
16+
17+
public BrandResource(final int defaultSize, final BrandRepository brandRepository) {
18+
this.defaultSize = defaultSize;
19+
this.brandRepository = brandRepository;
20+
21+
}
22+
23+
@GET
24+
public List<Brand> getBrands(@QueryParam("size") final Optional<Integer> size) {
25+
return brandRepository.findAll(size.orElse(defaultSize));
26+
}
27+
28+
@GET
29+
@Path("/{id}")
30+
public Brand getById(@PathParam("id") final Long id) {
31+
return brandRepository
32+
.findById(id)
33+
.orElseThrow(RuntimeException::new);
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
defaultSize: 5
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.dropwizard.introduction.repository;
2+
3+
import com.baeldung.dropwizard.introduction.domain.Brand;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class BrandRepositoryUnitTest {
14+
15+
private static final Brand BRAND_1 = new Brand(1L, "Brand1");
16+
17+
private final BrandRepository brandRepository = new BrandRepository(getBrands());
18+
19+
@Test
20+
void givenSize_whenFindingAll_thenReturnList() {
21+
final int size = 2;
22+
23+
final List<Brand> result = brandRepository.findAll(size);
24+
25+
assertEquals(size, result.size());
26+
}
27+
28+
@Test
29+
void givenId_whenFindingById_thenReturnFoundBrand() {
30+
final Long id = BRAND_1.getId();
31+
32+
final Optional<Brand> result = brandRepository.findById(id);
33+
34+
Assertions.assertTrue(result.isPresent());
35+
assertEquals(BRAND_1, result.get());
36+
}
37+
38+
39+
private List<Brand> getBrands() {
40+
final List<Brand> brands = new ArrayList<>();
41+
brands.add(BRAND_1);
42+
brands.add(new Brand(2L, "Brand2"));
43+
brands.add(new Brand(3L, "Brand3"));
44+
45+
return brands;
46+
}
47+
}

0 commit comments

Comments
 (0)