Skip to content

Commit 831692b

Browse files
committed
Added tests and creates Hakster Client
1 parent 9b3b8dd commit 831692b

13 files changed

Lines changed: 325 additions & 0 deletions

hackster-service-client/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.lohika.jclub.hackster.client</groupId>
7+
<artifactId>hackster-service-client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>hackster-service-client</name>
12+
<description>Client library for Hackster Service</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-eureka</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-feign</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-hystrix</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-hateoas</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.projectlombok</groupId>
48+
<artifactId>lombok</artifactId>
49+
<optional>true</optional>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.lohika.jclub</groupId>
59+
<artifactId>discovery-server</artifactId>
60+
<version>0.0.1-SNAPSHOT</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.testcontainers</groupId>
65+
<artifactId>testcontainers</artifactId>
66+
<version>1.3.0</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.lohika.jclub</groupId>
71+
<artifactId>hackster-service</artifactId>
72+
<version>0.0.1-SNAPSHOT</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
77+
<dependencyManagement>
78+
<dependencies>
79+
<dependency>
80+
<groupId>org.springframework.cloud</groupId>
81+
<artifactId>spring-cloud-dependencies</artifactId>
82+
<version>${spring-cloud.version}</version>
83+
<type>pom</type>
84+
<scope>import</scope>
85+
</dependency>
86+
</dependencies>
87+
</dependencyManagement>
88+
89+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
4+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.hateoas.config.EnableHypermediaSupport;
7+
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
18+
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
19+
@EnableFeignClients(clients = {HacksterServiceClient.class})
20+
@Import({FeignMappingDefaultConfiguration.class, HacksterServiceClientConfiguration.class})
21+
@EnableDiscoveryClient
22+
public @interface EnableHacksterServiceClient {
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4+
import org.springframework.boot.autoconfigure.web.WebMvcRegistrations;
5+
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
6+
import org.springframework.cloud.netflix.feign.FeignClient;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.core.annotation.AnnotationUtils;
10+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
11+
12+
import feign.Feign;
13+
14+
/**
15+
* https://github.com/spring-cloud/spring-cloud-netflix/issues/466
16+
*/
17+
@Configuration
18+
@ConditionalOnClass({Feign.class})
19+
public class FeignMappingDefaultConfiguration {
20+
@Bean
21+
public WebMvcRegistrations feignWebRegistrations() {
22+
return new WebMvcRegistrationsAdapter() {
23+
@Override
24+
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
25+
return new FeignFilterRequestMappingHandlerMapping();
26+
}
27+
};
28+
}
29+
30+
private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
31+
@Override
32+
protected boolean isHandler(Class<?> beanType) {
33+
return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null);
34+
}
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
8+
@FeignClient(
9+
value = "hackster-service",
10+
url = "${hackster-service.ribbon.servers:}",
11+
decode404 = true,
12+
fallback = HacksterServiceClientFallback.class)
13+
@RequestMapping("/hackster")
14+
public interface HacksterServiceClient {
15+
16+
@GetMapping(value = "/{phone}")
17+
Boolean isHackster(@PathVariable("phone") String phone);
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class HacksterServiceClientConfiguration {
8+
9+
@Bean
10+
public HacksterServiceClientFallback hacksterServiceClientFallback() {
11+
return new HacksterServiceClientFallback();
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
@Slf4j
6+
public class HacksterServiceClientFallback implements HacksterServiceClient {
7+
8+
@Override
9+
public Boolean isHackster(String phone) {
10+
log.error("Can't check is hackster");
11+
return true;
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest(classes = HacksterServiceClientTestApplication.class)
11+
public class HacksterServiceClientApplicationTests {
12+
13+
@Autowired
14+
private HacksterServiceClientFallback hacksterServiceClientFallback;
15+
16+
@Test
17+
public void contextLoads() {
18+
}
19+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.junit.ClassRule;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.util.EnvironmentTestUtils;
9+
import org.springframework.context.ApplicationContextInitializer;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.testcontainers.containers.GenericContainer;
14+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
15+
16+
import static org.hamcrest.Matchers.equalTo;
17+
import static org.hamcrest.Matchers.notNullValue;
18+
import static org.junit.Assert.assertThat;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(classes = HacksterServiceClientTestApplication.class)
22+
@ContextConfiguration(initializers = HacksterServiceClientTest.Initializer.class)
23+
public class HacksterServiceClientTest {
24+
25+
@ClassRule
26+
public static GenericContainer HacksterService = new GenericContainer("hackster-service:latest")
27+
.withExposedPorts(8082)
28+
.withEnv("maxAllowedApartmentsPerRealtor", "4")
29+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started HacksterServiceApplication in.*\\s"));
30+
31+
@Autowired
32+
private HacksterServiceClient hacksterServiceClient;
33+
34+
@Test
35+
public void checkIfNewNumberIsNotHackster() {
36+
boolean isHackster = hacksterServiceClient.isHackster("123123123");
37+
38+
assertThat(isHackster, equalTo(false));
39+
}
40+
41+
@Test
42+
public void checkIfOldNumberIsNotHackster() {
43+
boolean isHacksterFalse = hacksterServiceClient.isHackster("321321312");
44+
45+
for (int i = 0; i < 5; i++) {
46+
hacksterServiceClient.isHackster("321321312");
47+
}
48+
49+
boolean isHacksterTrue = hacksterServiceClient.isHackster("321321312");
50+
51+
assertThat(isHacksterFalse, equalTo(false));
52+
assertThat(isHacksterTrue, equalTo(true));
53+
}
54+
55+
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
56+
@Override
57+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
58+
59+
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
60+
"hackster-service.ribbon.servers=http://" + HacksterService.getContainerIpAddress() + ":"
61+
+ HacksterService.getMappedPort(8082) + "/"
62+
);
63+
}
64+
}
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.hackster.client;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@EnableHacksterServiceClient
7+
@SpringBootApplication
8+
public class HacksterServiceClientTestApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(HacksterServiceClientTestApplication.class, args);
12+
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.application.name=hackster-service-client-testing
2+
feign.hystrix.enabled=true
3+
hackster-service.ribbon.servers=http://localhost:6666/
4+
eureka.client.enabled=false

0 commit comments

Comments
 (0)