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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.restapi;
import org.apache.http.client.utils.URIBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class RestUriTest {
URI createUri(String path, String query) throws URISyntaxException {
return new URIBuilder()
.addParameter("foo", "bar")
.setHost("host")
.setScheme("http")
.setPort(666)
.setPath(path)
.setCustomQuery(query)
.setFragment("fargment").build();
}
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void testBasic() throws Exception {
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/myid", "query"));
assertThat(restUri.getDocId(), is("myid"));
assertThat(restUri.getDocumentType(), is("doctype"));
assertThat(restUri.getNamespace(), is("namespace"));
assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
assertThat(restUri.generateFullId(), is("id:namespace:doctype::myid"));
}
@Test
public void encodingSlashes() throws Exception {
// Try with slashes encoded.
final String id = " !\"øæåp/:;&,.:;'1Q";
String encodedId = URLEncoder.encode(id, StandardCharsets.UTF_8.name());
RestUri restUri = new RestUri(URI.create("/document/v1/namespace/doctype/docid/" + encodedId));
assertThat(restUri.getDocId(), is(id));
assertThat(restUri.getDocumentType(), is("doctype"));
assertThat(restUri.getNamespace(), is("namespace"));
assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
assertThat(restUri.generateFullId(), is("id:namespace:doctype::" + id));
}
@Test
public void encodingSlashes2() throws Exception {
// This will decode the slashes.
final String id = " !\"øæåp/:;&,.:;'1Q ";
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/" + id, "query"));
assertThat(restUri.getDocId(), is(id));
assertThat(restUri.getDocumentType(), is("doctype"));
assertThat(restUri.getNamespace(), is("namespace"));
assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
assertThat(restUri.generateFullId(), is("id:namespace:doctype::" + id));
}
@Test
public void testVisit() throws Exception {
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/", "query"));
assertThat(restUri.getDocId(), is(""));
assertThat(restUri.getDocumentType(), is("doctype"));
assertThat(restUri.getNamespace(), is("namespace"));
assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
assertThat(restUri.generateFullId(), is("id:namespace:doctype::"));
}
@Test
public void testOneSlashTooMuchWhichIsFine() throws Exception {
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/myid:342:23/wrong", ""));
assertThat(restUri.getDocId(), is("myid:342:23/wrong"));
}
@Test
public void testGroupG() throws Exception {
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/group/group/myid", ""));
assertThat(restUri.getDocId(), is("myid"));
assertThat(restUri.getDocumentType(), is("doctype"));
assertThat(restUri.getGroup().get().name, is('g'));
assertThat(restUri.getGroup().get().value, is("group"));
assertThat(restUri.generateFullId(), is("id:namespace:doctype:g=group:myid"));
}
@Test
public void testGroupN() throws Exception {
RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/number/group/myid", ""));
assertThat(restUri.getGroup().get().name, is('n'));
assertThat(restUri.getGroup().get().value, is("group"));
}
@Test
public void testGroupUnknown() throws Exception {
thrown.expect(RestApiException.class);
new RestUri(createUri("/document/v1/namespace/doctype/Q/myid", ""));
}
}
|