Skip to content

Commit

Permalink
adds writers mediatype coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jam01 committed Dec 4, 2020
1 parent 58ae85b commit afca0e5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/test/java/com/datasonnet/CSVWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ void testCSVWriter() throws URISyntaxException, IOException {
Mapper mapper = new Mapper("payload");


String mapped = mapper.transform(data, Collections.emptyMap(), MediaTypes.APPLICATION_CSV).getContent();
Document<String> mapped = mapper.transform(data, Collections.emptyMap(), MediaTypes.APPLICATION_CSV);
assertEquals(MediaTypes.APPLICATION_CSV, mapped.getMediaType());

String expected = TestResourceReader.readFileAsString("writeCSVTest.csv");
assertEquals(expected.trim(), mapped.trim());
assertEquals(expected.trim(), mapped.getContent().trim());
}

@Test
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/com/datasonnet/JSONWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.datasonnet;

/*-
* Copyright 2019-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.datasonnet.document.DefaultDocument;
import com.datasonnet.document.Document;
import com.datasonnet.document.MediaTypes;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONWriterTest {

@Test
public void testJSONWriter() {

Mapper mapper = new Mapper("{ \n" +
" str: 'value', \n" +
" arr: [1, 2, 3], \n" +
" obj: {}, \n" +
" num: 9, \n" +
" 'null': null \n" +
"}");

Document<String> mapped = mapper.transform(DefaultDocument.NULL_INSTANCE, Collections.emptyMap(), MediaTypes.APPLICATION_JSON);
assertEquals(MediaTypes.APPLICATION_JSON, mapped.getMediaType());

String expected = "{\"str\":\"value\",\"arr\":[1,2,3],\"obj\":{},\"num\":9,\"null\":null}";
assertEquals(expected.trim(), mapped.getContent().trim());
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/datasonnet/JavaWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void testJavaWriter() throws Exception {

Document<Gizmo> mapped = mapper.transform(data, new HashMap<>(), MediaTypes.APPLICATION_JAVA, Gizmo.class);

assertTrue(MediaTypes.APPLICATION_JAVA.equalsTypeAndSubtype(mapped.getMediaType()));

Object result = mapped.getContent();
assertTrue(result instanceof Gizmo);

Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/datasonnet/XMLWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import com.datasonnet.document.DefaultDocument;
import com.datasonnet.document.Document;
import com.datasonnet.document.MediaType;
import com.datasonnet.document.MediaTypes;
import com.datasonnet.util.TestResourceReader;
Expand Down Expand Up @@ -87,9 +88,9 @@ void testXMLWriterExt() throws Exception {

Mapper mapper = new Mapper(datasonnet);

String mappedXml = mapper.transform(new DefaultDocument<>(jsonData, MediaTypes.APPLICATION_JSON), Collections.emptyMap(), MediaTypes.APPLICATION_XML).getContent();

assertThat(mappedXml, CompareMatcher.isSimilarTo(expectedXml).ignoreWhitespace());
Document<String> mappedXml = mapper.transform(new DefaultDocument<>(jsonData, MediaTypes.APPLICATION_JSON), Collections.emptyMap(), MediaTypes.APPLICATION_XML);
assertEquals(MediaTypes.APPLICATION_XML, mappedXml.getMediaType());
assertThat(mappedXml.getContent(), CompareMatcher.isSimilarTo(expectedXml).ignoreWhitespace());
}

@Test
Expand Down

0 comments on commit afca0e5

Please sign in to comment.