diff --git a/README.md b/README.md index 8c80f2c..9213d6c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config-server/pom.xml b/config-server/pom.xml index 3c95320..d49e884 100644 --- a/config-server/pom.xml +++ b/config-server/pom.xml @@ -30,6 +30,7 @@ org.springframework.boot spring-boot-starter-actuator + org.springframework.cloud spring-cloud-config-server @@ -40,6 +41,11 @@ spring-boot-starter-test test + + + org.springframework.cloud + spring-cloud-starter-eureka + diff --git a/config-server/src/main/java/com/lohika/jclub/ConfigserverApplication.java b/config-server/src/main/java/com/lohika/jclub/ConfigServerApplication.java similarity index 51% rename from config-server/src/main/java/com/lohika/jclub/ConfigserverApplication.java rename to config-server/src/main/java/com/lohika/jclub/ConfigServerApplication.java index f559560..14a0d57 100644 --- a/config-server/src/main/java/com/lohika/jclub/ConfigserverApplication.java +++ b/config-server/src/main/java/com/lohika/jclub/ConfigServerApplication.java @@ -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); + } } diff --git a/config-server/src/main/resources/application.properties b/config-server/src/main/resources/application.properties index b5b58c5..4cf2381 100644 --- a/config-server/src/main/resources/application.properties +++ b/config-server/src/main/resources/application.properties @@ -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 \ No newline at end of file + +spring.cloud.config.discovery.enabled=true +spring.application.name=config-server + +server.port=8888 diff --git a/config-server/src/test/java/com/lohika/jclub/ConfigserverApplicationTests.java b/config-server/src/test/java/com/lohika/jclub/ConfigServerApplicationTests.java similarity index 87% rename from config-server/src/test/java/com/lohika/jclub/ConfigserverApplicationTests.java rename to config-server/src/test/java/com/lohika/jclub/ConfigServerApplicationTests.java index 60327b3..f38b62a 100644 --- a/config-server/src/test/java/com/lohika/jclub/ConfigserverApplicationTests.java +++ b/config-server/src/test/java/com/lohika/jclub/ConfigServerApplicationTests.java @@ -7,7 +7,7 @@ @RunWith(SpringRunner.class) @SpringBootTest -public class ConfigserverApplicationTests { +public class ConfigServerApplicationTests { @Test public void contextLoads() { diff --git a/config/hackster-service.properties b/config/hackster-service.properties new file mode 100644 index 0000000..ca3e968 --- /dev/null +++ b/config/hackster-service.properties @@ -0,0 +1 @@ +maxAllowedApartmentsPerRealtor=5 diff --git a/discovery-server/src/main/resources/application.properties b/discovery-server/src/main/resources/application.properties index 7cd76ab..bac323d 100644 --- a/discovery-server/src/main/resources/application.properties +++ b/discovery-server/src/main/resources/application.properties @@ -2,4 +2,4 @@ spring.application.name=eureka-server server.port=8761 eureka.client.register-with-eureka=false -eureka.client.fetch-registry=false \ No newline at end of file +eureka.client.fetch-registry=false diff --git a/hackster-service-client/pom.xml b/hackster-service-client/pom.xml new file mode 100644 index 0000000..8583e63 --- /dev/null +++ b/hackster-service-client/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + com.lohika.jclub.hackster.client + hackster-service-client + 0.0.1-SNAPSHOT + jar + + hackster-service-client + Client library for Hackster Service + + + org.springframework.boot + spring-boot-starter-parent + 1.5.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + Dalston.RELEASE + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-feign + + + org.springframework.cloud + spring-cloud-starter-hystrix + + + org.springframework.boot + spring-boot-starter-hateoas + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + com.lohika.jclub + discovery-server + 0.0.1-SNAPSHOT + test + + + org.testcontainers + testcontainers + 1.3.0 + test + + + com.lohika.jclub + hackster-service + 0.0.1-SNAPSHOT + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + diff --git a/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/EnableHacksterServiceClient.java b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/EnableHacksterServiceClient.java new file mode 100644 index 0000000..e651856 --- /dev/null +++ b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/EnableHacksterServiceClient.java @@ -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 { +} diff --git a/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/FeignMappingDefaultConfiguration.java b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/FeignMappingDefaultConfiguration.java new file mode 100644 index 0000000..a9dba11 --- /dev/null +++ b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/FeignMappingDefaultConfiguration.java @@ -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); + } + } +} diff --git a/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClient.java b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClient.java new file mode 100644 index 0000000..4e4be33 --- /dev/null +++ b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClient.java @@ -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); +} diff --git a/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientConfiguration.java b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientConfiguration.java new file mode 100644 index 0000000..71ff48b --- /dev/null +++ b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientConfiguration.java @@ -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(); + } +} diff --git a/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientFallback.java b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientFallback.java new file mode 100644 index 0000000..fdda50a --- /dev/null +++ b/hackster-service-client/src/main/java/com/lohika/jclub/hackster/client/HacksterServiceClientFallback.java @@ -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; + } +} diff --git a/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientApplicationTests.java b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientApplicationTests.java new file mode 100644 index 0000000..d67716a --- /dev/null +++ b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientApplicationTests.java @@ -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() { + } +} diff --git a/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTest.java b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTest.java new file mode 100644 index 0000000..5377ccb --- /dev/null +++ b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTest.java @@ -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 { + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + + EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(), + "hackster-service.ribbon.servers=http://" + HacksterService.getContainerIpAddress() + ":" + + HacksterService.getMappedPort(8082) + "/" + ); + } + } +} diff --git a/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTestApplication.java b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTestApplication.java new file mode 100644 index 0000000..a581699 --- /dev/null +++ b/hackster-service-client/src/test/java/com/lohika/jclub/hackster/client/HacksterServiceClientTestApplication.java @@ -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); + } +} diff --git a/hackster-service-client/src/test/resources/application.properties b/hackster-service-client/src/test/resources/application.properties new file mode 100644 index 0000000..6be9bb7 --- /dev/null +++ b/hackster-service-client/src/test/resources/application.properties @@ -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 diff --git a/hackster-service-client/src/test/resources/banner.txt b/hackster-service-client/src/test/resources/banner.txt new file mode 100644 index 0000000..56cd009 --- /dev/null +++ b/hackster-service-client/src/test/resources/banner.txt @@ -0,0 +1,5 @@ + _______________________ + /_ __/ ____/ ___/_ __/ + / / / __/ \__ \ / / + / / / /___ ___/ // / +/_/ /_____//____//_/ diff --git a/hackster-service/pom.xml b/hackster-service/pom.xml index 9b39ea6..4b21f6c 100644 --- a/hackster-service/pom.xml +++ b/hackster-service/pom.xml @@ -39,11 +39,17 @@ org.springframework.boot spring-boot-starter-data-jpa + org.springframework.cloud spring-cloud-starter-eureka + + org.springframework.cloud + spring-cloud-starter-config + + com.h2database h2 @@ -79,6 +85,32 @@ org.springframework.boot spring-boot-maven-plugin + + com.spotify + docker-maven-plugin + 1.0.0 + + + build-image + install + + build + + + + + hackster-service + java + ["java", "-jar", "/${project.build.finalName}.jar"] + + + / + ${project.build.directory} + ${project.build.finalName}.jar + + + + diff --git a/hackster-service/src/main/java/com/lohika/jclub/Hackster.java b/hackster-service/src/main/java/com/lohika/jclub/Hackster.java index 34027b2..f8f7c1e 100644 --- a/hackster-service/src/main/java/com/lohika/jclub/Hackster.java +++ b/hackster-service/src/main/java/com/lohika/jclub/Hackster.java @@ -1,22 +1,26 @@ package com.lohika.jclub; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - @Data @NoArgsConstructor @RequiredArgsConstructor @Entity public class Hackster { - @Id @GeneratedValue + @Id + @GeneratedValue private long id; @NonNull private String phone; + + @NonNull + private int numberOfApartments; } diff --git a/hackster-service/src/main/java/com/lohika/jclub/HacksterController.java b/hackster-service/src/main/java/com/lohika/jclub/HacksterController.java index f1e9fc3..ba03389 100644 --- a/hackster-service/src/main/java/com/lohika/jclub/HacksterController.java +++ b/hackster-service/src/main/java/com/lohika/jclub/HacksterController.java @@ -11,10 +11,16 @@ public class HacksterController { @Autowired - private HacksterRepository hacksterRepository; + private HacksterService hacksterService; + /** + * Check if current number correspond to hackster Rrealtor. + * + * @param phone Realtor phone number. + * @return Is current number correspond to hackster realtor. + */ @GetMapping(path = "/{phone}") public boolean isHackster(@PathVariable(name = "phone") String phone) { - return hacksterRepository.findByPhone(phone).isPresent(); + return hacksterService.isHackster(phone); } } diff --git a/hackster-service/src/main/java/com/lohika/jclub/HacksterService.java b/hackster-service/src/main/java/com/lohika/jclub/HacksterService.java new file mode 100644 index 0000000..b574184 --- /dev/null +++ b/hackster-service/src/main/java/com/lohika/jclub/HacksterService.java @@ -0,0 +1,39 @@ +package com.lohika.jclub; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class HacksterService { + + @Value("${maxAllowedApartmentsPerRealtor}") + private int maxAllowedApartmentsPerRealtor; + + @Autowired + private HacksterRepository hacksterRepository; + + /** + * Check if current number exists in database and is the number of apartments related to this number + * satisfy our limit. If number exists and didn't satisfy condition then this number will correspond + * to hackster realtor otherwise no. If phone number doesn't exist then it will be added with apartment + * amount = 1; + * + * @param phone Phone number of Realtor. + * @return Is this phone number correspond to Hackster Realtor or no. + */ + public boolean isHackster(String phone) { + Hackster hackster = hacksterRepository.findByPhone(phone) + .map(this::incrementApartmentCounterAndGet) + .orElse(new Hackster(phone, 1)); + + hacksterRepository.save(hackster); + + return hackster.getNumberOfApartments() > maxAllowedApartmentsPerRealtor; + } + + private Hackster incrementApartmentCounterAndGet(Hackster hackster) { + hackster.setNumberOfApartments(hackster.getNumberOfApartments() + 1); + return hackster; + } +} diff --git a/hackster-service/src/main/java/com/lohika/jclub/HacksterServiceApplication.java b/hackster-service/src/main/java/com/lohika/jclub/HacksterServiceApplication.java index c7fdefb..6b3bcf2 100644 --- a/hackster-service/src/main/java/com/lohika/jclub/HacksterServiceApplication.java +++ b/hackster-service/src/main/java/com/lohika/jclub/HacksterServiceApplication.java @@ -1,38 +1,13 @@ package com.lohika.jclub; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.stereotype.Component; - -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; @EnableDiscoveryClient @SpringBootApplication public class HacksterServiceApplication { - - public static void main(String[] args) { - SpringApplication.run(HacksterServiceApplication.class, args); - } - - // dummy data - @Component - class DummyCLR implements CommandLineRunner { - - @Autowired - private HacksterRepository hacksterRepository; - - @Override - public void run(String... strings) throws Exception { - List hacksters = Stream.of("123", "456", "789").map(Hackster::new).collect(Collectors.toList()); - hacksterRepository.save(hacksters); - - - hacksterRepository.findAll().forEach(System.out::println); - } - } + public static void main(String[] args) { + SpringApplication.run(HacksterServiceApplication.class, args); + } } diff --git a/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceApplicationTests.java b/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceApplicationTests.java index 9298141..d5a3b33 100644 --- a/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceApplicationTests.java +++ b/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceApplicationTests.java @@ -2,14 +2,22 @@ import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import static junit.framework.TestCase.assertTrue; + @RunWith(SpringRunner.class) @SpringBootTest +@AutoConfigureMockMvc public class HacksterServiceApplicationTests { + @Autowired + private HacksterService hacksterService; - @Test - public void contextLoads() { - } + @Test + public void testContextLoad() throws Exception { + assertTrue(hacksterService != null); + } } diff --git a/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceTests.java b/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceTests.java new file mode 100644 index 0000000..469e89c --- /dev/null +++ b/hackster-service/src/test/java/com/lohika/jclub/HacksterServiceTests.java @@ -0,0 +1,46 @@ +package com.lohika.jclub; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class HacksterServiceTests { + @Value("${maxAllowedApartmentsPerRealtor}") + private int maxAllowedApartmentsPerRealtor; + + @Autowired + private MockMvc mockMvc; + + @Test + public void testIfNewPhoneNumberIsNotHakster() throws Exception { + mockMvc.perform(get("/hackster/123123123")) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().isOk()) + .andExpect(content().string("false")); + } + + @Test + public void testIfPhoneNumberBecomeAHakster() throws Exception { + for (int i = 0; i < maxAllowedApartmentsPerRealtor; i++) { + mockMvc.perform(get("/hackster/321321")); + } + + mockMvc.perform(get("/hackster/321321")) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().isOk()) + .andExpect(content().string("true")); + } +} diff --git a/hackster-service/src/test/resources/application.properties b/hackster-service/src/test/resources/application.properties new file mode 100644 index 0000000..4067d5e --- /dev/null +++ b/hackster-service/src/test/resources/application.properties @@ -0,0 +1,2 @@ +maxAllowedApartmentsPerRealtor=10 +eureka.client.enabled=false diff --git a/pom.xml b/pom.xml index 4f40370..8a25d39 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ rating-service rating-service-client hackster-service + hackster-service-client realtor-service storage-service storage-service-client diff --git a/rating-service/src/main/java/com/lohika/jclub/rating/service/RatingService.java b/rating-service/src/main/java/com/lohika/jclub/rating/service/RatingService.java index 3d34f1b..0d867af 100644 --- a/rating-service/src/main/java/com/lohika/jclub/rating/service/RatingService.java +++ b/rating-service/src/main/java/com/lohika/jclub/rating/service/RatingService.java @@ -7,6 +7,8 @@ import java.math.MathContext; import java.math.RoundingMode; +import javax.annotation.PostConstruct; + @Service public class RatingService { diff --git a/rating-service/src/main/resources/application.properties b/rating-service/src/main/resources/application.properties index cdedb9d..fc1c858 100644 --- a/rating-service/src/main/resources/application.properties +++ b/rating-service/src/main/resources/application.properties @@ -1,2 +1,2 @@ server.port=8081 -spring.application.name=rating-service \ No newline at end of file +spring.application.name=rating-service