Skip to content

Commit a6c6552

Browse files
author
Khrystyna Hospodarysko
committed
Added realtor service
1 parent 983d35d commit a6c6552

8 files changed

Lines changed: 199 additions & 0 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<module>discovery-server</module>
1717
<module>rating-service</module>
1818
<module>hackster-service</module>
19+
<module>realtor-service</module>
1920
</modules>
2021
</project>

rating-service/src/main/java/com/lohika/jclub/RatingController.java

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

3+
import com.netflix.discovery.DiscoveryClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
35
import org.springframework.beans.factory.annotation.Value;
46
import org.springframework.web.bind.annotation.PostMapping;
57
import org.springframework.web.bind.annotation.RequestBody;

realtor-service/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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</groupId>
7+
<artifactId>realtor-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>realtor-service</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.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.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.16.12</version>
47+
</dependency>
48+
</dependencies>
49+
50+
<dependencyManagement>
51+
<dependencies>
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-dependencies</artifactId>
55+
<version>${spring-cloud.version}</version>
56+
<type>pom</type>
57+
<scope>import</scope>
58+
</dependency>
59+
</dependencies>
60+
</dependencyManagement>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
72+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.lohika.jclub;
2+
3+
import lombok.*;
4+
5+
@Builder
6+
@NoArgsConstructor
7+
@AllArgsConstructor
8+
@Data
9+
public class ApartmentRecord {
10+
@NonNull
11+
private String location;
12+
@NonNull
13+
private double price;
14+
@NonNull
15+
private double sqft;
16+
@NonNull
17+
private String phone;
18+
@NonNull
19+
private String realtorName;
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.lohika.jclub;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.cloud.client.ServiceInstance;
6+
import org.springframework.cloud.client.discovery.DiscoveryClient;
7+
import org.springframework.http.HttpMethod;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
import org.springframework.web.client.RestTemplate;
11+
12+
import java.util.List;
13+
14+
@Slf4j
15+
@RestController()
16+
public class RealtorController {
17+
18+
@Autowired
19+
private DiscoveryClient discoveryClient;
20+
21+
@Autowired
22+
private RestTemplate restTemplate;
23+
24+
@PostMapping("/apartments")
25+
public void addApartment(@RequestBody ApartmentRecord apartmentRecord) {
26+
ResponseEntity<Boolean> isHackster =
27+
restTemplate.exchange("http://hackster-service/hackster/{phone}",
28+
HttpMethod.GET, null, Boolean.class, apartmentRecord.getPhone());
29+
log.info("Is hackster " + isHackster);
30+
}
31+
32+
@RequestMapping("/service-instances/{applicationName}")
33+
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
34+
return this.discoveryClient.getInstances(applicationName);
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lohika.jclub;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@EnableDiscoveryClient
11+
@SpringBootApplication
12+
public class RealtorServiceApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(RealtorServiceApplication.class, args);
16+
}
17+
18+
@Bean
19+
@LoadBalanced
20+
public RestTemplate restTemplate() {
21+
return new RestTemplate();
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=realtor-service
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.lohika.jclub;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.mock.web.MockHttpServletResponse;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
15+
16+
import javax.ws.rs.core.MediaType;
17+
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest
22+
@AutoConfigureMockMvc
23+
public class RealtorServiceApplicationTests {
24+
25+
@Autowired
26+
private MockMvc mockMvc;
27+
28+
@Test
29+
public void contextLoads() throws Exception {
30+
ApartmentRecord apartmentRecord = ApartmentRecord.builder()
31+
.phone("123")
32+
.realtorName("Anna Realtor")
33+
.sqft(44)
34+
.price(100)
35+
.location("Lviv").build();
36+
37+
mockMvc.perform(MockMvcRequestBuilders
38+
.post("/apartments")
39+
.contentType(MediaType.APPLICATION_JSON)
40+
.content(new ObjectMapper().writeValueAsBytes(apartmentRecord)))
41+
.andDo(MockMvcResultHandlers.print())
42+
.andExpect(status().isOk());
43+
}
44+
}

0 commit comments

Comments
 (0)