-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloWorldTest.java
125 lines (85 loc) · 3.5 KB
/
HelloWorldTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package io.wcm.caravan.rhyme.examples.spring.helloworld;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import io.wcm.caravan.hal.resource.Link;
import io.wcm.caravan.rhyme.api.client.HalApiClient;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class HelloWorldTest {
private final HelloWorldResource helloWorld;
HelloWorldTest(@Autowired ServletWebServerApplicationContext server) {
HalApiClient client = HalApiClient.create();
String entryPointUrl = "http://localhost:" + server.getWebServer().getPort();
helloWorld = client.getRemoteResource(entryPointUrl, HelloWorldResource.class);
}
@Test
void default_message_can_be_fetched() {
String defaultMsg = helloWorld.getText();
assertThat(defaultMsg)
.isEqualTo("Hello World!");
}
@Test
void custom_message_can_be_fetched() {
String customMsg = helloWorld.withCustomMessage("Foo Bar!").getText();
assertThat(customMsg)
.isEqualTo("Foo Bar!");
}
@Test
void translated_messages_can_be_fetched() {
Stream<String> translations = helloWorld.getTranslations().map(HelloWorldResource::getText);
assertThat(translations)
.containsExactly("Hallo Welt", "Bonjour le monde", "Ciao mondo");
}
@Test
void links_to_translated_messages_should_have_language_as_link_names() {
Stream<Link> translationLinks = helloWorld.getTranslations().map(HelloWorldResource::createLink);
assertThat(translationLinks)
.extracting(Link::getName)
.containsExactly("de", "fr", "it");
}
@Test
void translated_message_should_have_only_links_to_other_translations() {
HelloWorldResource firstTranslation = helloWorld.getTranslations().findFirst().get();
String firstLanguage = firstTranslation.createLink().getName();
assertThat(firstTranslation.getTranslations())
.hasSize(2)
.extracting(resource -> resource.createLink().getName())
.doesNotContain(firstLanguage);
}
@Test
void custom_message_should_not_have_links_to_translations() {
Stream<HelloWorldResource> translations = helloWorld.withCustomMessage("Foo Bar!").getTranslations();
assertThat(translations)
.isEmpty();
}
@Test
void custom_message_resource_should_have_link_back_to_default_message() {
Optional<HelloWorldResource> defaultLink = helloWorld.withCustomMessage("Foo Bar!").withDefaultMessage();
assertThat(defaultLink)
.isPresent()
.get()
.extracting(HelloWorldResource::getText)
.isEqualTo("Hello World!");
}
@Test
void translated_message_resource_should_have_link_back_to_default_message() {
HelloWorldResource firstTranslation = helloWorld.getTranslations().findFirst().get();
Optional<HelloWorldResource> defaultLink = firstTranslation.withDefaultMessage();
assertThat(defaultLink)
.isPresent()
.get()
.extracting(HelloWorldResource::getText)
.isEqualTo("Hello World!");
}
@Test
void default_message_should_not_have_default_link() {
Optional<HelloWorldResource> defaultLink = helloWorld.withDefaultMessage();
assertThat(defaultLink)
.isNotPresent();
}
}