Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
piomin committed Aug 31, 2020
1 parent 8eb842e commit 3e01d1e
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 246 deletions.
224 changes: 112 additions & 112 deletions department-service/pom.xml
Original file line number Diff line number Diff line change
@@ -1,119 +1,119 @@
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<artifactId>department-service</artifactId>
<groupId>pl.piomin.services</groupId>
<version>1.1</version>
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<artifactId>department-service</artifactId>
<groupId>pl.piomin.services</groupId>
<version>1.1</version>

<properties>
<java.version>1.8</java.version>
</properties>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-server-mock</artifactId>
<version>4.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.specto</groupId>
<artifactId>hoverfly-java-junit5</artifactId>
<version>0.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.specto</groupId>
<artifactId>hoverfly-java</artifactId>
<version>0.13.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-server-mock</artifactId>
<version>4.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.specto</groupId>
<artifactId>hoverfly-java-junit5</artifactId>
<version>0.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.specto</groupId>
<artifactId>hoverfly-java</artifactId>
<version>0.13.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.4.0</version>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.4.0</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
@ExtendWith(HoverflyExtension.class)
public class DepartmentAPIAdvancedTest {

@Autowired
TestRestTemplate restTemplate;
@Autowired
KubernetesClient client;

Expand All @@ -38,46 +36,50 @@ static void setup(Hoverfly hoverfly) {
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
"false");
"false");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "default");
hoverfly.simulate(dsl(service("kubernetes.default.svc")
.get("/api/v1/namespaces/default/configmaps/department")
.willReturn(success().body(json(buildConfigMap())))));
.get("/api/v1/namespaces/default/configmaps/department")
.willReturn(success().body(json(buildConfigMap())))));
}

private static ConfigMap buildConfigMap() {
return new ConfigMapBuilder().withNewMetadata()
.withName("department").withNamespace("default")
.endMetadata()
.addToData("application.properties",
"spring.data.mongodb.uri=mongodb://localhost:27017/test")
.build();
}

@Autowired
TestRestTemplate restTemplate;

@Test
void findByOrganizationWithEmployees(Hoverfly hoverfly) {
Department department = new Department(1L, "Test");
department = restTemplate.postForObject("/", department, Department.class);
Assertions.assertNotNull(department);
Assertions.assertNotNull(department.getId());

hoverfly.simulate(dsl(service(prepareUrl())
.get("/api/v1/namespaces/default/endpoints/employee")
.willReturn(success().body(json(buildEndpoints())))),
dsl(service(prepareUrl())
.get("/api/v1/namespaces/default/services/employee")
.willReturn(success().body(json(buildService())))),
dsl(service("employee.default:8080")
.get("/department/" + department.getId())
.willReturn(success().body(json(buildEmployees())))));

Department[] departments =
restTemplate.getForObject("/organization/{organizationId}/with-employees", Department[].class, 1L);
hoverfly.simulate(
dsl(service(prepareUrl())
.get("/api/v1/namespaces/default/endpoints/employee")
.willReturn(success().body(json(buildEndpoints())))),
dsl(service(prepareUrl())
.get("/api/v1/namespaces/default/services/employee")
.willReturn(success().body(json(buildService())))),
dsl(service("employee.default:8080")
.get("/department/" + department.getId())
.willReturn(success().body(json(buildEmployees())))));

Department[] departments = restTemplate
.getForObject("/organization/{organizationId}/with-employees", Department[].class, 1L);
Assertions.assertEquals(1, departments.length);
Assertions.assertEquals(1, departments[0].getEmployees().size());
}

private static ConfigMap buildConfigMap() {
return new ConfigMapBuilder().withNewMetadata()
.withName("department").withNamespace("default")
.endMetadata()
.addToData("application.properties",
"spring.data.mongodb.uri=mongodb://localhost:27017/test")
.build();
}

private Service buildService() {
return new ServiceBuilder().withNewMetadata().withName("employee")
.withNamespace("default").withLabels(new HashMap<>())
Expand All @@ -87,10 +89,12 @@ private Service buildService() {

private Endpoints buildEndpoints() {
return new EndpointsBuilder().withNewMetadata()
.withName("employee").withNamespace("default").endMetadata().addNewSubset().addNewAddress()
.withIp("employee.default").endAddress().addNewPort().withName("http")
.withPort(8080).endPort().endSubset()
.build();
.withName("employee").withNamespace("default")
.endMetadata()
.addNewSubset().addNewAddress()
.withIp("employee.default").endAddress().addNewPort().withName("http")
.withPort(8080).endPort().endSubset()
.build();
}

private List<Employee> buildEmployees() {
Expand All @@ -106,8 +110,8 @@ private List<Employee> buildEmployees() {

private String prepareUrl() {
return client.getConfiguration().getMasterUrl()
.replace("/", "")
.replace("https:", "");
.replace("/", "")
.replace("https:", "");
}

}
Loading

0 comments on commit 3e01d1e

Please sign in to comment.