Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ Sandbox to play with spring cloud features

# Dev

## Before

- Set up **JAVA_CLUB_SRC_HOME** environment variable that point to folder with project `spring-cloud`

- If you have issues with test containers (docker images) then check if you have `DOCKER_HOST` environment variable.
If no then add it using this : `export DOCKER_HOST=unix:///var/run/docker.sock` (example for *Docker For Mac*)


## Build

```bash
mvn clean install
```

**Notice** : if you have issues with test containers (docker images) then check if you have `DOCKER_HOST` environment variable.
If no then add it using this : `export DOCKER_HOST=unix:///var/run/docker.sock` (example for *Docker For Mac*)

## Run

```bash
Expand Down
6 changes: 6 additions & 0 deletions config-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
Expand All @@ -40,6 +41,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
6 changes: 5 additions & 1 deletion config-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
spring.cloud.config.server.git.uri=${JAVA_CLUB_SRC_HOME}/spring-cloud
spring.cloud.config.server.git.search-paths=config
server.port=8888

spring.cloud.config.discovery.enabled=true
spring.application.name=config-server

server.port=8888
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigserverApplicationTests {
public class ConfigServerApplicationTests {

@Test
public void contextLoads() {
Expand Down
1 change: 1 addition & 0 deletions config/hackster-service.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maxAllowedApartmentsPerRealtor=5
2 changes: 1 addition & 1 deletion discovery-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ spring.application.name=eureka-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.fetch-registry=false
90 changes: 90 additions & 0 deletions hackster-service-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<groupId>com.lohika.jclub.hackster.client</groupId>
<artifactId>hackster-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>hackster-service-client</name>
<description>Client library for Hackster Service</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lohika.jclub</groupId>
<artifactId>discovery-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lohika.jclub</groupId>
<artifactId>hackster-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.lohika.jclub.hackster.client;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Import;
import org.springframework.hateoas.config.EnableHypermediaSupport;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@EnableFeignClients(clients = {HacksterServiceClient.class})
@Import({FeignMappingDefaultConfiguration.class, HacksterServiceClientConfiguration.class})
@EnableDiscoveryClient
public @interface EnableHacksterServiceClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lohika.jclub.hackster.client;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.WebMvcRegistrations;
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import feign.Feign;

/**
* https://github.com/spring-cloud/spring-cloud-netflix/issues/466
*/
@Configuration
@ConditionalOnClass({Feign.class})
public class FeignMappingDefaultConfiguration {
@Bean
public WebMvcRegistrations feignWebRegistrations() {
return new WebMvcRegistrationsAdapter() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new FeignFilterRequestMappingHandlerMapping();
}
};
}

private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected boolean isHandler(Class<?> beanType) {
return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.lohika.jclub.hackster.client;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(
value = "hackster-service",
url = "${hackster-service.ribbon.servers:}",
decode404 = true,
fallback = HacksterServiceClientFallback.class)
@RequestMapping("/hackster")
public interface HacksterServiceClient {

@GetMapping(value = "/{phone}")
Boolean isHackster(@PathVariable("phone") String phone);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lohika.jclub.hackster.client;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HacksterServiceClientConfiguration {

@Bean
public HacksterServiceClientFallback hacksterServiceClientFallback() {
return new HacksterServiceClientFallback();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lohika.jclub.hackster.client;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HacksterServiceClientFallback implements HacksterServiceClient {

@Override
public Boolean isHackster(String phone) {
log.error("Can't check is hackster");
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lohika.jclub.hackster.client;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HacksterServiceClientTestApplication.class)
public class HacksterServiceClientApplicationTests {

@Autowired
private HacksterServiceClientFallback hacksterServiceClientFallback;

@Test
public void contextLoads() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.lohika.jclub.hackster.client;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.LogMessageWaitStrategy;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HacksterServiceClientTestApplication.class)
@ContextConfiguration(initializers = HacksterServiceClientTest.Initializer.class)
public class HacksterServiceClientTest {
private static final int MAX_ALLOWED_APARTMENTS_PER_REALTOR = 4;

@ClassRule
public static GenericContainer HacksterService = new GenericContainer("hackster-service:latest")
.withExposedPorts(8082)
.withEnv("maxAllowedApartmentsPerRealtor", Integer.toString(MAX_ALLOWED_APARTMENTS_PER_REALTOR))
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started HacksterServiceApplication in.*\\s"));

@Autowired
private HacksterServiceClient hacksterServiceClient;

@Test
public void testIfNewNumberIsNotHackster() {
boolean isHackster = hacksterServiceClient.isHackster("123123123");

assertThat(isHackster, equalTo(false));
}

@Test
public void testIfNumberBecomeAHackster() {
for (int i = 0; i < MAX_ALLOWED_APARTMENTS_PER_REALTOR; i++) {
hacksterServiceClient.isHackster("321321312");
}

boolean isHacksterTrue = hacksterServiceClient.isHackster("321321312");

assertThat(isHacksterTrue, equalTo(true));
}

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
"hackster-service.ribbon.servers=http://" + HacksterService.getContainerIpAddress() + ":"
+ HacksterService.getMappedPort(8082) + "/"
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lohika.jclub.hackster.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableHacksterServiceClient
@SpringBootApplication
public class HacksterServiceClientTestApplication {

public static void main(String[] args) {
SpringApplication.run(HacksterServiceClientTestApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.application.name=hackster-service-client-testing
feign.hystrix.enabled=true
hackster-service.ribbon.servers=http://localhost:6666/
eureka.client.enabled=false
5 changes: 5 additions & 0 deletions hackster-service-client/src/test/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_______________________
/_ __/ ____/ ___/_ __/
/ / / __/ \__ \ / /
/ / / /___ ___/ // /
/_/ /_____//____//_/
Loading