Skip to content

Commit ee18384

Browse files
authored
Merge pull request #5 from lvivJavaClub/fallback-support-added
feign Fallback support added
2 parents b1d69a8 + e216eb2 commit ee18384

File tree

10 files changed

+125
-11
lines changed

10 files changed

+125
-11
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
spring.application.name=realtor-service
1+
spring.application.name=realtor-service
2+
feign.hystrix.enabled=true

storage-service-client/src/main/java/com/lohika/jclub/storage/EnableStorageServiceClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
44
import org.springframework.cloud.netflix.feign.EnableFeignClients;
5+
import org.springframework.context.annotation.Import;
56
import org.springframework.hateoas.config.EnableHypermediaSupport;
67

78
import java.lang.annotation.Documented;
@@ -15,7 +16,8 @@
1516
@Documented
1617

1718
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
18-
@EnableFeignClients(clients = StorageServiceClient.class)
19+
@EnableFeignClients(clients = {StorageServiceClient.class})
20+
@Import({FeignMappingDefaultConfiguration.class, StorageServiceClientConfiguration.class})
1921
@EnableDiscoveryClient
2022
public @interface EnableStorageServiceClient {
2123
}

realtor-service/src/main/java/com/lohika/jclub/realtor/FeignMappingDefaultConfiguration.java renamed to storage-service-client/src/main/java/com/lohika/jclub/storage/FeignMappingDefaultConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lohika.jclub.realtor;
1+
package com.lohika.jclub.storage;
22

33
import feign.Feign;
44

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lohika.jclub.storage;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class StorageServiceClientConfiguration {
8+
9+
@Bean
10+
public StorageServiceClientFallback storageServiceClientFallback() {
11+
return new StorageServiceClientFallback();
12+
}
13+
}

storage-service-client/src/main/java/com/lohika/jclub/storage/StorageServiceClientFallback.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import org.springframework.hateoas.PagedResources;
66

7+
import static java.util.Collections.emptyList;
8+
79
@Slf4j
810
public class StorageServiceClientFallback implements StorageServiceClient {
911

1012
@Override
1113
public PagedResources<Apartment> list() {
1214
log.error("Can not get list");
13-
return null;
15+
return new PagedResources<>(emptyList(), new PagedResources.PageMetadata(0, 0, 0));
1416
}
1517

1618
@Override
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lohika.jclub.storage;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest(classes = StorageServiceClientTestApplication.class)
10+
public class StorageServiceClientApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.lohika.jclub.storage;
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.hateoas.PagedResources;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
import org.testcontainers.containers.GenericContainer;
11+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
12+
13+
import static org.hamcrest.Matchers.hasSize;
14+
import static org.hamcrest.Matchers.notNullValue;
15+
import static org.hamcrest.Matchers.nullValue;
16+
import static org.junit.Assert.assertThat;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(classes = StorageServiceClientTestApplication.class)
20+
public class StorageServiceClientFallbackTest {
21+
22+
@ClassRule
23+
public static GenericContainer storageService = new GenericContainer("storage-service:latest")
24+
.withExposedPorts(8091)
25+
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Started StorageServiceApplication in.*\\s"));
26+
27+
@Autowired
28+
private StorageServiceClient storageServiceClient;
29+
30+
@Autowired
31+
private StorageServiceClientFallback storageServiceClientFallback;
32+
33+
private Apartment apartment = Apartment.builder()
34+
.location("location")
35+
.mail("mail")
36+
.phone("phone")
37+
.price(1.5d)
38+
.realtorName("realtorName")
39+
.sqft(1.3d)
40+
.build();
41+
42+
@Test
43+
public void list() {
44+
PagedResources<Apartment> actual = storageServiceClient.list();
45+
46+
assertThat(actual, notNullValue());
47+
assertThat(actual.getContent(), notNullValue());
48+
49+
assertThat(actual.getContent(), hasSize(0));
50+
}
51+
52+
@Test
53+
public void create() {
54+
Apartment actual = storageServiceClient.create(apartment);
55+
56+
assertThat(actual, nullValue());
57+
}
58+
59+
@Test
60+
public void get() {
61+
Apartment actual = storageServiceClient.get("not-existing-id");
62+
63+
assertThat(actual, nullValue());
64+
}
65+
66+
@Test
67+
public void update() {
68+
Apartment actual = storageServiceClient.update("not-existing-id", apartment);
69+
70+
assertThat(actual, nullValue());
71+
}
72+
73+
@Test
74+
public void delete() {
75+
storageServiceClient.delete("not-existing-id");
76+
}
77+
}

storage-service-client/src/test/java/com/lohika/jclub/storage/StorageServiceClientTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class StorageServiceClientTest {
3333
@Autowired
3434
private StorageServiceClient storageServiceClient;
3535

36+
@Autowired
37+
private StorageServiceClientFallback storageServiceClientFallback;
38+
3639
private Apartment apartment = Apartment.builder()
3740
.location("location")
3841
.mail("mail")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
spring.application.name=storage-service-client-testing
2+
feign.hystrix.enabled=true
3+
storage-service.ribbon.servers=http://localhost:6666/
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
_____ __ _
2-
/ ___// /_____ _________ _____ ____ ________ ______ __(_)_______
3-
\__ \/ __/ __ \/ ___/ __ `/ __ `/ _ \ / ___/ _ \/ ___/ | / / / ___/ _ \
4-
___/ / /_/ /_/ / / / /_/ / /_/ / __/ (__ ) __/ / | |/ / / /__/ __/
5-
/____/\__/\____/_/ \__,_/\__, /\___/ /____/\___/_/ |___/_/\___/\___/
6-
/____/
7-
v.${application.version}
1+
_______________________ _____ __ _ ___ __
2+
/_ __/ ____/ ___/_ __/ / ___// /_____ _________ _____ ____ ________ ______ __(_)_______ _____/ (_)__ ____ / /_
3+
/ / / __/ \__ \ / / \__ \/ __/ __ \/ ___/ __ `/ __ `/ _ \ / ___/ _ \/ ___/ | / / / ___/ _ \ / ___/ / / _ \/ __ \/ __/
4+
/ / / /___ ___/ // / ___/ / /_/ /_/ / / / /_/ / /_/ / __/ (__ ) __/ / | |/ / / /__/ __/ / /__/ / / __/ / / / /_
5+
/_/ /_____//____//_/ /____/\__/\____/_/ \__,_/\__, /\___/ /____/\___/_/ |___/_/\___/\___/ \___/_/_/\___/_/ /_/\__/
6+
/____/

0 commit comments

Comments
 (0)