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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.runner;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author valerijf
*/
public class FormatInputStreamTest {
@Test(expected=IllegalArgumentException.class)
public void testWithGarbageText() throws IOException {
String streamString = "This is neither XML nor JSON!";
InputStream jsonStream = getInputStreamOf(streamString);
FormatInputStream formatInputStream = new FormatInputStream(jsonStream, Optional.empty(), false);
}
@Test
public void testWithFileInput() throws IOException {
String fileString = "{\"format\": \"json\"}";
File file = File.createTempFile("feeddata", "json");
file.deleteOnExit();
try (FileWriter writer = new FileWriter(file)) {
writer.write(fileString);
}
FormatInputStream formatInputStream = new FormatInputStream(null, Optional.of(file.getAbsolutePath()), false);
assertThat(fileString, is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.JSON));
}
@Test
public void testPreferenceFileOverStream() throws IOException {
String streamString = "something entirely different";
String fileString = "{\"format\": \"json\"}";
InputStream jsonStream = getInputStreamOf(streamString);
File file = File.createTempFile("feeddata", "json");
file.deleteOnExit();
try (FileWriter writer = new FileWriter(file)) {
writer.write(fileString);
}
FormatInputStream formatInputStream = new FormatInputStream(jsonStream, Optional.of(file.getAbsolutePath()), false);
assertThat(fileString, is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.JSON));
}
@Test
public void testSimpleJsonInputStream() throws IOException {
String streamString = "{\"format\": \"json\"}";
InputStream jsonStream = getInputStreamOf(streamString);
FormatInputStream formatInputStream = new FormatInputStream(jsonStream, Optional.empty(), false);
assertThat(streamString, is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.JSON));
}
@Test
public void testSimpleXmlInputStream() throws IOException {
String streamString = "<scope><tag>format</tag><value>xml</value></scope>";
InputStream jsonStream = getInputStreamOf(streamString);
FormatInputStream formatInputStream = new FormatInputStream(jsonStream, Optional.empty(), false);
assertThat(streamString, is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.XML));
}
@Test
public void testSparselyFormattedXml() throws IOException {
String streamString = " \t\t\n<scope>\n\n\n<tag>format</tag><value>xml</value></scope>";
InputStream jsonStream = getInputStreamOf(streamString);
FormatInputStream formatInputStream = new FormatInputStream(jsonStream, Optional.empty(), false);
assertThat(streamString, is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.XML));
}
@Test
public void testAddRootToXml() throws IOException {
String streamString = "some random text";
InputStream textStream = getInputStreamOf(streamString);
FormatInputStream formatInputStream = new FormatInputStream(textStream, Optional.empty(), true);
assertThat("<vespafeed>" + streamString + "</vespafeed>",
is(convertStreamToString(formatInputStream.getInputStream())));
assertThat(formatInputStream.getFormat(), is(FormatInputStream.Format.XML));
}
private static String convertStreamToString(InputStream inputStream) throws IOException {
StringBuilder builder = new StringBuilder();
while (true) {
int character = inputStream.read();
if (character == -1) {
inputStream.close();
return builder.toString();
}
builder.append((char)character);
}
}
private static InputStream getInputStreamOf(String text) {
return new ByteArrayInputStream(text.getBytes());
}
}
|