Repository navigation
Expand file tree
/
Copy pathHeaderTemplateTest.java
More file actions
114 lines (100 loc) · 4.35 KB
/
Copy pathHeaderTemplateTest.java
File metadata and controls
114 lines (100 loc) · 4.35 KB
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
/**
* Copyright 2012-2020 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.template;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class HeaderTemplateTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test(expected = IllegalArgumentException.class)
public void it_should_throw_exception_when_name_is_null() {
HeaderTemplate.create(null, Collections.singletonList("test"));
exception.expectMessage("name is required.");
}
@Test(expected = IllegalArgumentException.class)
public void it_should_throw_exception_when_name_is_empty() {
HeaderTemplate.create("", Collections.singletonList("test"));
exception.expectMessage("name is required.");
}
@Test(expected = IllegalArgumentException.class)
public void it_should_throw_exception_when_value_is_null() {
HeaderTemplate.create("test", null);
exception.expectMessage("values are required");
}
@Test(expected = IllegalArgumentException.class)
public void it_should_throw_exception_when_value_is_null_for_chunks() {
HeaderTemplate.from("test", null);
exception.expectMessage("values are required");
}
@Test
public void it_should_return_name() {
HeaderTemplate headerTemplate =
HeaderTemplate.create("test", Arrays.asList("test 1", "test 2"));
assertEquals("test", headerTemplate.getName());
}
@Test
public void it_should_return_expanded() {
HeaderTemplate headerTemplate =
HeaderTemplate.create("hello", Arrays.asList("emre", "savci", "{name}", "{missing}"));
assertEquals("hello emre, savci", headerTemplate.expand(Collections.emptyMap()));
assertEquals("hello emre, savci, firsts",
headerTemplate.expand(Collections.singletonMap("name", "firsts")));
}
@Test
public void it_should_return_expanded_literals() {
HeaderTemplate headerTemplate =
HeaderTemplate.create("hello", Arrays.asList("emre", "savci", "{replace_me}"));
assertEquals("hello emre, savci, {}",
headerTemplate.expand(Collections.singletonMap("replace_me", "{}")));
}
@Test
public void create_should_preserve_order() {
/*
* Since Java 7, HashSet order is stable within a since JVM process, so one of these assertions
* should fail if a HashSet is used.
*/
HeaderTemplate headerTemplateWithFirstOrdering =
HeaderTemplate.create("hello", Arrays.asList("test 1", "test 2"));
assertThat(new ArrayList<>(headerTemplateWithFirstOrdering.getValues()),
equalTo(Arrays.asList("test 1", "test 2")));
HeaderTemplate headerTemplateWithSecondOrdering =
HeaderTemplate.create("hello", Arrays.asList("test 2", "test 1"));
assertThat(new ArrayList<>(headerTemplateWithSecondOrdering.getValues()),
equalTo(Arrays.asList("test 2", "test 1")));
}
@Test
public void append_should_preserve_order() {
/*
* Since Java 7, HashSet order is stable within a since JVM process, so one of these assertions
* should fail if a HashSet is used.
*/
HeaderTemplate headerTemplateWithFirstOrdering =
HeaderTemplate.append(HeaderTemplate.create("hello", Collections.emptyList()),
Arrays.asList("test 1", "test 2"));
assertThat(new ArrayList<>(headerTemplateWithFirstOrdering.getValues()),
equalTo(Arrays.asList("test 1", "test 2")));
HeaderTemplate headerTemplateWithSecondOrdering =
HeaderTemplate.append(HeaderTemplate.create("hello", Collections.emptyList()),
Arrays.asList("test 2", "test 1"));
assertThat(new ArrayList<>(headerTemplateWithSecondOrdering.getValues()),
equalTo(Arrays.asList("test 2", "test 1")));
}
}