Skip to content

Commit fcdb942

Browse files
valfirstsdstoehr
authored andcommitted
Make HarReader and HarWriter symmetric
New methods: - `HarReader#readFromInputStream(java.io.InputStream)` - `HarReader#readFromInputStream(java.io.InputStream, HarReaderMode)` - `HarReader#readFromBytes(byte[])` - `HarReader#readFromBytes(byte[], HarReaderMode)` - `HarWriter#writeAsString()`
1 parent f7311ba commit fcdb942

File tree

5 files changed

+135
-37
lines changed

5 files changed

+135
-37
lines changed

src/main/java/de/sstoehr/harreader/AbstractHarIO.java

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package de.sstoehr.harreader;
22

3+
import java.io.IOException;
4+
import java.util.function.Function;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
38
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
49
import de.sstoehr.harreader.jackson.MapperFactory;
510

@@ -21,4 +26,20 @@ protected MapperFactory getMapperFactory() {
2126
return mapperFactory;
2227
}
2328

29+
protected static <T, E extends Exception> T wrap(ObjectMapper mapper, IOFunction<ObjectMapper, T> consumer,
30+
Function<IOException, E> exceptionFactory) throws E {
31+
try {
32+
return consumer.apply(mapper);
33+
} catch(IOException thrown) {
34+
throw exceptionFactory.apply(thrown);
35+
}
36+
}
37+
38+
@FunctionalInterface
39+
protected interface IOFunction<T, R> {
40+
41+
R apply(T object) throws IOException;
42+
43+
}
44+
2445
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.sstoehr.harreader;
22

33
import java.io.File;
4-
import java.io.IOException;
4+
import java.io.InputStream;
55

66
import com.fasterxml.jackson.databind.ObjectMapper;
77

@@ -10,6 +10,8 @@
1010

1111
public class HarReader extends AbstractHarIO {
1212

13+
private static final HarReaderMode DEFAULT_READER_MODE = HarReaderMode.STRICT;
14+
1315
public HarReader() {
1416
super();
1517
}
@@ -19,29 +21,68 @@ public HarReader(MapperFactory mapperFactory) {
1921
}
2022

2123
public Har readFromFile(File har) throws HarReaderException {
22-
return this.readFromFile(har, HarReaderMode.STRICT);
24+
return this.readFromFile(har, DEFAULT_READER_MODE);
2325
}
2426

2527
public Har readFromFile(File har, HarReaderMode mode) throws HarReaderException {
26-
ObjectMapper mapper = getMapperFactory().instance(mode);
27-
try {
28-
return mapper.readValue(har, Har.class);
29-
} catch (IOException e) {
30-
throw new HarReaderException(e);
31-
}
28+
return wrap(mode, mapper -> mapper.readValue(har, Har.class));
29+
}
30+
31+
/**
32+
* Deserialize HAR from the given {@link InputStream}. {@link InputStream} is closed after reading.
33+
*
34+
* @param harInputStream {@link InputStream} to read HAR from.
35+
* @return HAR deserialized from {@link InputStream}
36+
* @throws HarReaderException if a low-level I/O problem occurs
37+
*/
38+
public Har readFromInputStream(InputStream harInputStream) throws HarReaderException {
39+
return this.readFromInputStream(harInputStream, DEFAULT_READER_MODE);
40+
}
41+
42+
/**
43+
* Deserialize HAR from the given {@link InputStream}. {@link InputStream} is closed after reading.
44+
*
45+
* @param harInputStream {@link InputStream} to read HAR from.
46+
* @param mode Reading mode
47+
* @return HAR deserialized from {@link InputStream}
48+
* @throws HarReaderException if a low-level I/O problem occurs
49+
*/
50+
public Har readFromInputStream(InputStream harInputStream, HarReaderMode mode) throws HarReaderException {
51+
return wrap(mode, mapper -> mapper.readValue(harInputStream, Har.class));
3252
}
3353

3454
public Har readFromString(String har) throws HarReaderException {
35-
return this.readFromString(har, HarReaderMode.STRICT);
55+
return this.readFromString(har, DEFAULT_READER_MODE);
3656
}
3757

3858
public Har readFromString(String har, HarReaderMode mode) throws HarReaderException {
39-
ObjectMapper mapper = getMapperFactory().instance(mode);
40-
try {
41-
return mapper.readValue(har, Har.class);
42-
} catch (IOException e) {
43-
throw new HarReaderException(e);
44-
}
59+
return wrap(mode, mapper -> mapper.readValue(har, Har.class));
4560
}
4661

62+
/**
63+
* Deserialize HAR from the given byte array.
64+
*
65+
* @param bytes Byte array to deserialize HAR from
66+
* @return HAR deserialized from byte array
67+
* @throws HarReaderException if a low-level I/O problem occurs
68+
*/
69+
public Har readFromBytes(byte[] bytes) throws HarReaderException {
70+
return this.readFromBytes(bytes, DEFAULT_READER_MODE);
71+
}
72+
73+
/**
74+
* Deserialize HAR from the given byte array.
75+
*
76+
* @param bytes Byte array to deserialize HAR from
77+
* @param mode Reading mode
78+
* @return HAR deserialized from byte array
79+
* @throws HarReaderException if a low-level I/O problem occurs
80+
*/
81+
public Har readFromBytes(byte[] bytes, HarReaderMode mode) throws HarReaderException {
82+
return wrap(mode, mapper -> mapper.readValue(bytes, Har.class));
83+
}
84+
85+
private <T> T wrap(HarReaderMode mode, IOFunction<ObjectMapper, T> consumer) throws HarReaderException {
86+
return wrap(getMapperFactory().instance(mode), consumer, HarReaderException::new);
87+
}
4788
}

src/main/java/de/sstoehr/harreader/HarWriter.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.OutputStream;
66
import java.io.Writer;
7+
import java.util.function.Function;
78

89
import com.fasterxml.jackson.databind.ObjectMapper;
910

@@ -20,6 +21,16 @@ public HarWriter(MapperFactory mapperFactory) {
2021
super(mapperFactory);
2122
}
2223

24+
/**
25+
* Serialize HAR as a string. It's functionally equivalent to calling {@link #writeTo(Writer, Har)} with
26+
* {@link java.io.StringWriter} and constructing String, but more efficient.
27+
* @return Serialized HAR as a string
28+
* @throws HarWriterException if a low-level I/O problem occurs
29+
*/
30+
public String writeAsString(Har har) throws HarWriterException {
31+
return wrap(m -> m.writeValueAsString(har));
32+
}
33+
2334
/**
2435
* Serialize HAR as a byte array. It's functionally equivalent to calling {@link #writeTo(OutputStream, Har)} with
2536
* {@link java.io.ByteArrayOutputStream} and getting bytes, but more efficient. Encoding used will be UTF-8.
@@ -52,19 +63,6 @@ public void writeTo(File file, Har har) throws HarWriterException {
5263
}
5364

5465
private <T> T wrap(IOFunction<ObjectMapper, T> consumer) throws HarWriterException {
55-
ObjectMapper mapper = getMapperFactory().instance();
56-
try {
57-
return consumer.apply(mapper);
58-
} catch(IOException thrown) {
59-
throw new HarWriterException(thrown);
60-
}
66+
return wrap(getMapperFactory().instance(), consumer, HarWriterException::new);
6167
}
62-
63-
@FunctionalInterface
64-
private interface IOFunction<T, R> {
65-
66-
R apply(T object) throws IOException;
67-
68-
}
69-
7068
}

src/test/java/de/sstoehr/harreader/HarReaderTest.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,49 @@
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88

99
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
1014

1115
import org.junit.jupiter.api.Test;
1216

1317
class HarReaderTest {
1418

19+
private static final String PATH_TO_VALID_HAR = "src/test/resources/sstoehr.har";
20+
1521
private HarReader harReader = new HarReader();
1622

1723
@Test
18-
void test() throws HarReaderException {
19-
File harFile = new File("src/test/resources/sstoehr.har");
24+
void shouldReadFromFile() throws HarReaderException {
25+
File harFile = new File(PATH_TO_VALID_HAR);
2026
Har har = harReader.readFromFile(harFile);
2127
assertNotNull(har);
2228
}
2329

30+
@Test
31+
void shouldReadFromInputStream() throws HarReaderException, IOException {
32+
File harFile = new File(PATH_TO_VALID_HAR);
33+
InputStream inputStream = Files.newInputStream(harFile.toPath());
34+
Har har = harReader.readFromInputStream(inputStream);
35+
assertNotNull(har);
36+
}
37+
38+
@Test
39+
void shouldReadFromString() throws HarReaderException, IOException {
40+
byte[] bytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
41+
String harAsString = new String(bytes, StandardCharsets.UTF_8);
42+
Har har = harReader.readFromString(harAsString);
43+
assertNotNull(har);
44+
}
45+
46+
@Test
47+
void shouldReadFromBytes() throws HarReaderException, IOException {
48+
byte[] harAsBytes = Files.readAllBytes(new File(PATH_TO_VALID_HAR).toPath());
49+
Har har = harReader.readFromBytes(harAsBytes);
50+
assertNotNull(har);
51+
}
52+
2453
@Test
2554
void missingLog() throws HarReaderException {
2655
Har har = harReader.readFromString("{\"unknown\":\"!\"}");
@@ -60,15 +89,15 @@ void mapperFactoryNotNull() {
6089

6190
@Test
6291
void testEquals() throws HarReaderException {
63-
File harFile = new File("src/test/resources/sstoehr.har");
92+
File harFile = new File(PATH_TO_VALID_HAR);
6493
Har har1 = harReader.readFromFile(harFile);
6594
Har har2 = harReader.readFromFile(harFile);
6695
assertTrue(har1.equals(har2));
6796
}
6897

6998
@Test
7099
void testHashCode() throws HarReaderException {
71-
File harFile = new File("src/test/resources/sstoehr.har");
100+
File harFile = new File(PATH_TO_VALID_HAR);
72101
Har har1 = harReader.readFromFile(harFile);
73102
Har har2 = harReader.readFromFile(harFile);
74103
assertEquals(har1.hashCode(), har2.hashCode());

src/test/java/de/sstoehr/harreader/HarWriterTests.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ class HarWriterTests {
1919
private static final File HAR_FILE = new File("src/test/resources/sstoehr.har");
2020

2121
@Test
22-
void testWriteValueAsBytes() throws HarReaderException, IOException, HarWriterException {
22+
void shouldWriteHarAsString() throws HarReaderException, IOException, HarWriterException {
23+
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
24+
25+
Har har = new HarReader().readFromFile(HAR_FILE);
26+
HarWriter writer = new HarWriter();
27+
assertEquals(MAPPER.writeValueAsString(expected), writer.writeAsString(har));
28+
}
29+
30+
@Test
31+
void shouldWriteHarAsBytes() throws HarReaderException, IOException, HarWriterException {
2332
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
2433

2534
Har har = new HarReader().readFromFile(HAR_FILE);
@@ -28,7 +37,7 @@ void testWriteValueAsBytes() throws HarReaderException, IOException, HarWriterEx
2837
}
2938

3039
@Test
31-
void testWriteToOutputStream() throws HarReaderException, IOException, HarWriterException {
40+
void testWriteToOutputStream() throws IOException, HarWriterException {
3241
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3342
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
3443

@@ -38,7 +47,7 @@ void testWriteToOutputStream() throws HarReaderException, IOException, HarWriter
3847
}
3948

4049
@Test
41-
void testWriteToFile() throws HarReaderException, IOException, HarWriterException {
50+
void testWriteToFile() throws IOException, HarWriterException {
4251
File file = File.createTempFile("pref", "suff");
4352
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
4453

@@ -49,7 +58,7 @@ void testWriteToFile() throws HarReaderException, IOException, HarWriterExceptio
4958
}
5059

5160
@Test
52-
void testWriteToWriter() throws HarReaderException, IOException, HarWriterException {
61+
void testWriteToWriter() throws IOException, HarWriterException {
5362
StringWriter sw = new StringWriter();
5463
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
5564

0 commit comments

Comments
 (0)