Skip to content

Commit 77781c1

Browse files
author
Roman Tsypuk
committed
added testcontainer, dockerization
1 parent 491ebaf commit 77781c1

File tree

9 files changed

+155
-47
lines changed

9 files changed

+155
-47
lines changed

client-service/pom.xml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<artifactId>storage-service-client</artifactId>
5555
<version>0.0.1-SNAPSHOT</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.testcontainers</groupId>
59+
<artifactId>testcontainers</artifactId>
60+
<version>1.3.0</version>
61+
<scope>test</scope>
62+
</dependency>
5763
</dependencies>
5864

5965
<dependencyManagement>
@@ -74,8 +80,32 @@
7480
<groupId>org.springframework.boot</groupId>
7581
<artifactId>spring-boot-maven-plugin</artifactId>
7682
</plugin>
83+
<plugin>
84+
<groupId>com.spotify</groupId>
85+
<artifactId>docker-maven-plugin</artifactId>
86+
<version>1.0.0</version>
87+
<executions>
88+
<execution>
89+
<id>build-image</id>
90+
<phase>install</phase>
91+
<goals>
92+
<goal>build</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
<configuration>
97+
<imageName>client-service</imageName>
98+
<baseImage>java</baseImage>
99+
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
100+
<resources>
101+
<resource>
102+
<targetPath>/</targetPath>
103+
<directory>${project.build.directory}</directory>
104+
<include>${project.build.finalName}.jar</include>
105+
</resource>
106+
</resources>
107+
</configuration>
108+
</plugin>
77109
</plugins>
78110
</build>
79-
80-
81111
</project>

client-service/src/main/java/com/lohika/jclub/client/ApartmentRecord.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

client-service/src/main/java/com/lohika/jclub/client/ClientController.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.lohika.jclub.storage.client.StorageServiceClient;
55

66
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.data.domain.Pageable;
8-
import org.springframework.data.web.PagedResourcesAssembler;
97
import org.springframework.hateoas.PagedResources;
108
import org.springframework.web.bind.annotation.GetMapping;
119
import org.springframework.web.bind.annotation.RestController;
@@ -29,13 +27,4 @@ public PagedResources<Apartment> getApartments() {
2927
//TODO wrap appratments with resources. generate links to client service.
3028
}
3129

32-
33-
@GetMapping("/apartmentsPaged")
34-
public PagedResources<Apartment> getApartmentsPaged(Pageable pageable,
35-
PagedResourcesAssembler assembler) {
36-
PagedResources<Apartment> list = storageServiceClient.list();
37-
Collection<Apartment> content = list.getContent();
38-
return new PagedResources<>(content, list.getMetadata());
39-
40-
}
4130
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
spring.application.name=client-service
2-
server.port=8083
2+
server.port=8083
3+
eureka.instance.prefer-ip-address=true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#eureka:
2+
# client:
3+
# serviceUrl:
4+
# defaultZone: ${EUREKA_URI:http://${EUREKA_IP_PORT}}/eureka}
5+
# instance:
6+
# preferIpAddress: true
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.lohika.jclub.client;
2+
3+
import com.lohika.jclub.storage.client.StorageServiceClient;
4+
5+
import org.junit.ClassRule;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.junit.rules.RuleChain;
9+
import org.junit.rules.TestRule;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.util.EnvironmentTestUtils;
15+
import org.springframework.context.ApplicationContextInitializer;
16+
import org.springframework.context.ConfigurableApplicationContext;
17+
import org.springframework.test.context.ContextConfiguration;
18+
import org.springframework.test.context.junit4.SpringRunner;
19+
import org.springframework.test.web.servlet.MockMvc;
20+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
21+
import org.testcontainers.containers.GenericContainer;
22+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
23+
24+
import java.time.Duration;
25+
26+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
27+
28+
@RunWith(SpringRunner.class)
29+
@SpringBootTest(classes = ClientServiceApplication.class)
30+
@ContextConfiguration(initializers = ClientServiceTest.Initializer.class)
31+
@AutoConfigureMockMvc
32+
public class ClientServiceTest {
33+
34+
@ClassRule
35+
public static GenericContainer discoveryService = new GenericContainer("discovery-service:latest")
36+
// .waitingFor(new LogMessageWaitStrategy().withRegEx(".*Registered instance STORAGE-SERVICE.*\\s"))
37+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started EurekaServerApplication in.*\\s"))
38+
.withExposedPorts(8761);
39+
40+
@Rule
41+
public GenericContainer storageService = new GenericContainer("storage-service:latest")
42+
// .withStartupTimeout(Duration.ofSeconds(20))
43+
.withExposedPorts(8091)
44+
.withEnv("eureka.client.serviceUrl.defaultZone", "http://" + discoveryService.getContainerIpAddress() + ":" + discoveryService.getMappedPort(8761) + "/eureka")
45+
.withEnv("eureka.instance.preferIpAddress", "true")
46+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started EurekaServerApplication in.*\\s"));
47+
// .waitingFor(new LogMessageWaitStrategy().withRegEx(".*DiscoveryClient_STORAGE-SERVICE.*registration status: 204.*\\s"));
48+
49+
// @ClassRule
50+
// public static TestRule chain = RuleChain
51+
// .outerRule(discoveryService)
52+
// .around(storageService);
53+
54+
@Autowired
55+
private StorageServiceClient storageServiceClient;
56+
57+
@Autowired
58+
private MockMvc mockMvc;
59+
60+
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
61+
@Override
62+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
63+
64+
// EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
65+
// "storage-service.ribbon.servers=http://" + storageService.getContainerIpAddress() + ":"
66+
// + storageService.getMappedPort(8091) + "/"
67+
// );
68+
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
69+
"eureka.client.serviceUrl.defaultZone=http://" + discoveryService.getContainerIpAddress() +
70+
":" + discoveryService.getMappedPort(8761) + "/eureka");
71+
72+
73+
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
74+
"eureka.instance.preferIpAddress=true");
75+
76+
}
77+
}
78+
79+
@Test
80+
public void testGetApartments() throws Exception {
81+
// Given
82+
83+
// When
84+
mockMvc.perform(MockMvcRequestBuilders.get("/apartments"))
85+
.andDo(print());
86+
87+
// Then
88+
}
89+
}

discovery-server/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@
5656
<groupId>org.springframework.boot</groupId>
5757
<artifactId>spring-boot-maven-plugin</artifactId>
5858
</plugin>
59+
<plugin>
60+
<groupId>com.spotify</groupId>
61+
<artifactId>docker-maven-plugin</artifactId>
62+
<version>1.0.0</version>
63+
<executions>
64+
<execution>
65+
<id>build-image</id>
66+
<phase>install</phase>
67+
<goals>
68+
<goal>build</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
<configuration>
73+
<imageName>discovery-service</imageName>
74+
<baseImage>java</baseImage>
75+
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
76+
<resources>
77+
<resource>
78+
<targetPath>/</targetPath>
79+
<directory>${project.build.directory}</directory>
80+
<include>${project.build.finalName}.jar</include>
81+
</resource>
82+
</resources>
83+
</configuration>
84+
</plugin>
5985
</plugins>
6086
</build>
6187

rating-service/pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@
4242
<groupId>org.springframework.cloud</groupId>
4343
<artifactId>spring-cloud-starter-eureka</artifactId>
4444
</dependency>
45-
4645
<dependency>
4746
<groupId>org.projectlombok</groupId>
4847
<artifactId>lombok</artifactId>
4948
<version>1.16.16</version>
5049
</dependency>
51-
52-
5350
<dependency>
5451
<groupId>org.springframework.boot</groupId>
5552
<artifactId>spring-boot-starter-test</artifactId>

storage-service-client/src/main/java/com/lohika/jclub/storage/client/Apartment.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.lohika.jclub.storage.client;
22

3-
import org.springframework.hateoas.ResourceSupport;
4-
53
import lombok.AllArgsConstructor;
64
import lombok.Builder;
75
import lombok.Data;

0 commit comments

Comments
 (0)