-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathURLBuilderTest.java
More file actions
144 lines (113 loc) · 5.31 KB
/
URLBuilderTest.java
File metadata and controls
144 lines (113 loc) · 5.31 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.meilisearch.sdk.http;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.jupiter.api.Test;
public class URLBuilderTest {
private final URLBuilder classToTest = new URLBuilder();
@Test
void addSubroute() {
classToTest.addSubroute("route");
assertThat(classToTest.getRoutes().toString(), is(equalTo("/route")));
}
@Test
void addSubrouteWithMultipleRoutes() {
classToTest
.addSubroute("route1")
.addSubroute("route2")
.addSubroute("route3")
.addSubroute("route4");
assertThat(classToTest.getRoutes().toString(), is(equalTo("/route1/route2/route3/route4")));
}
@Test
void addSubrouteWithNullOrEmptyRoute() {
assertThat(
assertThrows(IllegalArgumentException.class, () -> classToTest.addSubroute(""))
.getMessage(),
is(equalTo("route segment must not be null or empty")));
assertThat(
assertThrows(IllegalArgumentException.class, () -> classToTest.addSubroute(null))
.getMessage(),
is(equalTo("route segment must not be null or empty")));
}
@Test
void addParameterStringInt() {
classToTest.addParameter("parameter1", 1);
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1")));
classToTest.addParameter("parameter2", 2);
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1¶meter2=2")));
classToTest.addParameter("parameter3", 3);
assertThat(
classToTest.getParams().toString(),
is(equalTo("?parameter1=1¶meter2=2¶meter3=3")));
}
@Test
void addParameterStringString() {
classToTest.addParameter("parameter1", "1");
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1")));
classToTest.addParameter("parameter2", "2");
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1¶meter2=2")));
classToTest.addParameter("parameter3", "3");
assertThat(
classToTest.getParams().toString(),
is(equalTo("?parameter1=1¶meter2=2¶meter3=3")));
}
@Test
void addParameterStringStringArray() {
classToTest.addParameter("parameter1", new String[] {"1", "a"});
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1,a")));
classToTest.addParameter("parameter2", new String[] {"2", "b"});
assertThat(
classToTest.getParams().toString(), is(equalTo("?parameter1=1,a¶meter2=2,b")));
classToTest.addParameter("parameter3", new String[] {"3", "c"});
assertThat(
classToTest.getParams().toString(),
is(equalTo("?parameter1=1,a¶meter2=2,b¶meter3=3,c")));
}
@Test
void addParameterStringIntArray() {
classToTest.addParameter("parameter1", new int[] {1, 2});
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=1,2")));
classToTest.addParameter("parameter2", new int[] {3, 4});
assertThat(
classToTest.getParams().toString(), is(equalTo("?parameter1=1,2¶meter2=3,4")));
classToTest.addParameter("parameter3", new int[] {5, 6});
assertThat(
classToTest.getParams().toString(),
is(equalTo("?parameter1=1,2¶meter2=3,4¶meter3=5,6")));
}
@Test
void addParameterStringDate() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date = format.parse("2042-01-30T10:30:00-05:00");
classToTest.addParameter("parameter1", date);
String parameterDate1 = classToTest.getParams().substring(12);
assertDoesNotThrow(() -> format.parse(parameterDate1));
assertEquals(classToTest.getParams().toString(), "?parameter1=2042-01-30T15:30:00Z");
classToTest.addParameter("parameter2", date);
String parameterDate2 = classToTest.getParams().substring(44);
assertDoesNotThrow(() -> format.parse(parameterDate2));
assertEquals(
classToTest.getParams().toString(),
"?parameter1=2042-01-30T15:30:00Z¶meter2=2042-01-30T15:30:00Z");
}
@Test
void addParameterStringDateWithPositiveTimezoneOffset() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date = format.parse("2042-01-30T17:30:00+02:00");
classToTest.addParameter("parameter1", date);
assertEquals("?parameter1=2042-01-30T15:30:00Z", classToTest.getParams().toString());
}
@Test
void getURL() {
assertThat(classToTest.getURL(), is(equalTo("")));
classToTest.addSubroute("routes");
classToTest.addParameter("parameter", "value");
assertThat(classToTest.getURL(), is(equalTo("/routes?parameter=value")));
}
}