|
| 1 | +package de.sstoehr.harreader; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.StringWriter; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import de.sstoehr.harreader.model.Har; |
| 16 | + |
| 17 | +class HarWriterTests { |
| 18 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 19 | + private static final File HAR_FILE = new File("src/test/resources/sstoehr.har"); |
| 20 | + |
| 21 | + @Test |
| 22 | + void testWriteValueAsBytes() 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), new String(writer.writeAsBytes(har), StandardCharsets.UTF_8)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testWriteToOutputStream() throws HarReaderException, IOException, HarWriterException { |
| 32 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 33 | + Har expected = MAPPER.readValue(HAR_FILE, Har.class); |
| 34 | + |
| 35 | + HarWriter writer = new HarWriter(); |
| 36 | + writer.writeTo(baos, expected); |
| 37 | + assertEquals(MAPPER.writeValueAsString(expected), new String(baos.toByteArray(), StandardCharsets.UTF_8)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testWriteToFile() throws HarReaderException, IOException, HarWriterException { |
| 42 | + File file = File.createTempFile("pref", "suff"); |
| 43 | + Har expected = MAPPER.readValue(HAR_FILE, Har.class); |
| 44 | + |
| 45 | + HarWriter writer = new HarWriter(); |
| 46 | + writer.writeTo(file, expected); |
| 47 | + Har actual = MAPPER.readValue(file, Har.class); |
| 48 | + assertEquals(MAPPER.writeValueAsString(expected), MAPPER.writeValueAsString(actual)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testWriteToWriter() throws HarReaderException, IOException, HarWriterException { |
| 53 | + StringWriter sw = new StringWriter(); |
| 54 | + Har expected = MAPPER.readValue(HAR_FILE, Har.class); |
| 55 | + |
| 56 | + HarWriter writer = new HarWriter(); |
| 57 | + writer.writeTo(sw, expected); |
| 58 | + assertEquals(MAPPER.writeValueAsString(expected), sw.toString()); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments