From d68bb0011bd785b2e0a86796f56b1f9256cc9fff Mon Sep 17 00:00:00 2001 From: angelasd Date: Tue, 16 Jun 2020 11:38:53 -0400 Subject: [PATCH 1/7] move writer methods to new writer class (#33) --- src/main/java/com/imsweb/x12/Separators.java | 1 + .../java/com/imsweb/x12/reader/X12Reader.java | 57 ++-------- .../java/com/imsweb/x12/writer/X12Writer.java | 83 ++++++++++++++ .../com/imsweb/x12/reader/X12ReaderTest.java | 79 +------------- .../com/imsweb/x12/writer/X12WriterTest.java | 102 ++++++++++++++++++ src/test/java/lab/X12ToHtmlLab.java | 5 +- 6 files changed, 201 insertions(+), 126 deletions(-) create mode 100644 src/main/java/com/imsweb/x12/writer/X12Writer.java create mode 100644 src/test/java/com/imsweb/x12/writer/X12WriterTest.java diff --git a/src/main/java/com/imsweb/x12/Separators.java b/src/main/java/com/imsweb/x12/Separators.java index ff99b7e..c2da7e1 100644 --- a/src/main/java/com/imsweb/x12/Separators.java +++ b/src/main/java/com/imsweb/x12/Separators.java @@ -39,6 +39,7 @@ public Separators(Character segment, Character element, Character composite) { setSegment(segment); setElement(element); setCompositeElement(composite); + setLineBreak(LineBreak.NONE); } /** diff --git a/src/main/java/com/imsweb/x12/reader/X12Reader.java b/src/main/java/com/imsweb/x12/reader/X12Reader.java index 5a348e8..f231e1b 100644 --- a/src/main/java/com/imsweb/x12/reader/X12Reader.java +++ b/src/main/java/com/imsweb/x12/reader/X12Reader.java @@ -36,10 +36,10 @@ public class X12Reader { - private static int _ISA_LENGTH = 106; - private static int _ELEMENT_SEPARATOR_POS = 3; // array position - private static int _COMPOSITE_SEPARATOR_POS = 104; // array position - private static int _SEGMENT_SEPARATOR_POS = 105; // array position + private static final int _ISA_LENGTH = 106; + private static final int _ELEMENT_SEPARATOR_POS = 3; // array position + private static final int _COMPOSITE_SEPARATOR_POS = 104; // array position + private static final int _SEGMENT_SEPARATOR_POS = 105; // array position private static final String _X091_ANSI_VERSION = "004010X091A1"; private static final String _X221_ANSI_VERSION = "005010X221A1"; @@ -59,6 +59,7 @@ public class X12Reader { private Map>> _childLoopTracker = new HashMap<>(); private Separators _separators; TransactionDefinition _definition; + private FileType _type; /** * All supported X12 file definitions @@ -86,7 +87,7 @@ public enum FileType { * Load definition from file * @return a TransactionDefinition */ - protected synchronized TransactionDefinition getDefinition() { + public synchronized TransactionDefinition getDefinition() { if (!_DEFINITIONS.containsKey(_mapping)) { XStream xstream = new XStream(new StaxDriver()); xstream.autodetectAnnotations(true); @@ -104,8 +105,6 @@ protected synchronized TransactionDefinition getDefinition() { } } - private FileType _type; - static { _TYPES.put(FileType.ANSI835_4010_X091, _X091_ANSI_VERSION); _TYPES.put(FileType.ANSI837_4010_X096, _X096_ANSI_VERSION); @@ -178,40 +177,9 @@ public X12Reader(FileType type, Reader reader) throws IOException { else parse(reader); } - - /** - * Gets an X12 formatted string representing this X12 reader. Will use no line - * breaks after separators. - * - * @return X12 formatted string representing this X12 reader. - */ - public String toX12String() { - _separators.setLineBreak(LineBreak.NONE); - return toX12StringImpl(); - } - /** - * Gets an X12 formatted string representing this X12 reader. - * - * @param lineBreak Line break to use for separators. - * @return X12 formatted string representing this X12 reader. - */ - public String toX12String(LineBreak lineBreak) { - _separators.setLineBreak(lineBreak); - return toX12StringImpl(); - } - - /** - * To HTML string will create an HTML segment from this X12 file. - * - * @return Human readable html segment representation of the X12 file. - */ - public String toHtml() { - StringBuilder builder = new StringBuilder(); - for (Loop loop : _dataLoops) { - builder.append(loop.toHtml(_definition.getLoop(), new ArrayList<>())); - } - return builder.toString(); + public TransactionDefinition getDefinition() { + return _definition; } /** @@ -1072,13 +1040,4 @@ private List getRequiredCompositePositions(SegmentDefinition seg) { return requiredPositions; } - - private String toX12StringImpl() { - StringBuilder builder = new StringBuilder(); - for (Loop loop : _dataLoops) { - builder.append(loop.toX12String(_definition.getLoop())); - builder.append(_separators.getLineBreak().getLineBreakString()); - } - return builder.toString(); - } } diff --git a/src/main/java/com/imsweb/x12/writer/X12Writer.java b/src/main/java/com/imsweb/x12/writer/X12Writer.java new file mode 100644 index 0000000..e66edcf --- /dev/null +++ b/src/main/java/com/imsweb/x12/writer/X12Writer.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2020 Information Management Services, Inc. + */ +package com.imsweb.x12.writer; + +import java.util.ArrayList; +import java.util.List; + +import com.imsweb.x12.LineBreak; +import com.imsweb.x12.Loop; +import com.imsweb.x12.Separators; +import com.imsweb.x12.mapping.TransactionDefinition; +import com.imsweb.x12.reader.X12Reader; +import com.imsweb.x12.reader.X12Reader.FileType; + +public class X12Writer { + + private static List _dataLoops; + private static Separators _separators; + private static TransactionDefinition _definition; + + public X12Writer(FileType fileType, List loops, Separators separators) { + _dataLoops = loops; + _definition = fileType.getDefinition(); + _separators = separators; + } + + public X12Writer(FileType fileType, List loops) { + _dataLoops = loops; + _definition = fileType.getDefinition(); + _separators = new Separators(); + } + + public X12Writer(X12Reader reader) { + _dataLoops = reader.getLoops(); + _definition = reader.getDefinition(); + _separators = reader.getSeparators(); + } + + + /** + * Gets an X12 formatted string representing this X12 reader. Will use no line + * breaks after separators. + * + * @return X12 formatted string representing this X12 reader. + */ + public String toX12String() { + return toX12String(LineBreak.NONE); + } + + /** + * Gets an X12 formatted string representing this X12 reader. + * + * @param lineBreak Line break to use for separators. + * @return X12 formatted string representing this X12 reader. + */ + public String toX12String(LineBreak lineBreak) { + _separators.setLineBreak(lineBreak); + return toX12StringImpl(); + } + + private String toX12StringImpl() { + StringBuilder builder = new StringBuilder(); + for (Loop loop : _dataLoops) { + builder.append(loop.toX12String(_definition.getLoop())); + builder.append(_separators.getLineBreak().getLineBreakString()); + } + return builder.toString(); + } + + /** + * To HTML string will create an HTML segment from this X12 file. + * + * @return Human readable html segment representation of the X12 file. + */ + public String toHtml() { + StringBuilder builder = new StringBuilder(); + for (Loop loop : _dataLoops) { + builder.append(loop.toHtml(_definition.getLoop(), new ArrayList<>())); + } + return builder.toString(); + } +} diff --git a/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java b/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java index ccd60ac..bb58109 100644 --- a/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java +++ b/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java @@ -25,8 +25,10 @@ import com.imsweb.x12.Element; import com.imsweb.x12.LineBreak; import com.imsweb.x12.Loop; +import com.imsweb.x12.X12; import com.imsweb.x12.mapping.TransactionDefinition; import com.imsweb.x12.reader.X12Reader.FileType; +import com.imsweb.x12.writer.X12Writer; public class X12ReaderTest { @@ -51,82 +53,7 @@ public void testConstructors() throws IOException { assertEquals(fromFileUtf8.getLoops().get(0).toString(), fromInputStreamUtf8.getLoops().get(0).toString()); assertEquals(fromFileUtf8.getLoops().get(0).toString(), fromReaderUtf8.getLoops().get(0).toString()); } - - /** - * Here we will test that you can go from x12, make changes, then serialize the - * x12 once again. - */ - @Test - public void testSerializeBasic() throws IOException { - URL url = this.getClass().getResource("/837_5010/x12_valid.txt"); - - X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), - StandardCharsets.UTF_8); - - String expected = IOUtils - .toString(this.getClass().getResourceAsStream("/837_5010/x12_valid.txt"), StandardCharsets.UTF_8) - .trim(); - LineBreak lineBreak; - if (expected.contains(LineBreak.CRLF.getLineBreakString())) { - lineBreak = LineBreak.CRLF; - } - else { - lineBreak = LineBreak.LF; - } - Assert.assertEquals(expected, fromFileUtf8.toX12String(lineBreak).trim()); - } - - /** - * Tests the toHtml method that - */ - @Test - public void testToHtmlBasic() throws IOException { - URL url = this.getClass().getResource("/837_5010/x12_valid.txt"); - - X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), - StandardCharsets.UTF_8); - - String x12Template = IOUtils.toString(getClass().getResourceAsStream("/html/x12-template.html"), StandardCharsets.UTF_8); - - String x12HtmlSegment = fromFileUtf8.toHtml(); - - String fullX12Html = String.format(x12Template, x12HtmlSegment); - - Document doc = Jsoup.parse(fullX12Html); - Elements loops = doc.select(".x12-loop"); - Assert.assertEquals(20, loops.size()); - - Elements segments = doc.select(".x12-segment"); - Assert.assertEquals(38, segments.size()); - - Elements elements = doc.select(".x12-element"); - Assert.assertEquals(216, elements.size()); - } - - /** - * Test a more complex x12 doc and see if we can serialize it. - */ - @Test - public void testSerializeComplex() throws IOException { - URL url = this.getClass().getResource("/837_5010/x12_complex.txt"); - - X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), - StandardCharsets.UTF_8); - - String expected = IOUtils - .toString(this.getClass().getResourceAsStream("/837_5010/x12_complex.txt"), StandardCharsets.UTF_8) - .trim(); - - LineBreak lineBreak; - if (expected.contains(LineBreak.CRLF.getLineBreakString())) { - lineBreak = LineBreak.CRLF; - } - else { - lineBreak = LineBreak.LF; - } - Assert.assertEquals(expected, fromFileUtf8.toX12String(lineBreak).trim()); - } - + @Test public void testMultipleGSLoops() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_multiple_gs.txt"); diff --git a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java new file mode 100644 index 0000000..7fc0eaa --- /dev/null +++ b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2020 Information Management Services, Inc. + */ +package com.imsweb.x12.writer; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; +import org.junit.Assert; +import org.junit.Test; + +import com.imsweb.x12.LineBreak; +import com.imsweb.x12.reader.X12Reader; +import com.imsweb.x12.reader.X12Reader.FileType; + +public class X12WriterTest { + /** + * Here we will test that you can go from x12, make changes, then serialize the + * x12 once again. + */ + @Test + public void testSerializeBasic() throws IOException { + URL url = this.getClass().getResource("/837_5010/x12_valid.txt"); + + X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), + StandardCharsets.UTF_8); + + String expected = IOUtils + .toString(this.getClass().getResourceAsStream("/837_5010/x12_valid.txt"), StandardCharsets.UTF_8) + .trim(); + LineBreak lineBreak; + if (expected.contains(LineBreak.CRLF.getLineBreakString())) { + lineBreak = LineBreak.CRLF; + } + else { + lineBreak = LineBreak.LF; + } + X12Writer writer = new X12Writer(fromFileUtf8); + Assert.assertEquals(expected, writer.toX12String(lineBreak).trim()); + } + + /** + * Tests the toHtml method that + */ + @Test + public void testToHtmlBasic() throws IOException { + URL url = this.getClass().getResource("/837_5010/x12_valid.txt"); + + X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), + StandardCharsets.UTF_8); + + String x12Template = IOUtils.toString(getClass().getResourceAsStream("/html/x12-template.html"), StandardCharsets.UTF_8); + + X12Writer writer = new X12Writer(fromFileUtf8); + String x12HtmlSegment = writer.toHtml(); + + String fullX12Html = String.format(x12Template, x12HtmlSegment); + + Document doc = Jsoup.parse(fullX12Html); + Elements loops = doc.select(".x12-loop"); + Assert.assertEquals(20, loops.size()); + + Elements segments = doc.select(".x12-segment"); + Assert.assertEquals(38, segments.size()); + + Elements elements = doc.select(".x12-element"); + Assert.assertEquals(216, elements.size()); + } + + /** + * Test a more complex x12 doc and see if we can serialize it. + */ + @Test + public void testSerializeComplex() throws IOException { + URL url = this.getClass().getResource("/837_5010/x12_complex.txt"); + + X12Reader fromFileUtf8 = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()), + StandardCharsets.UTF_8); + + String expected = IOUtils + .toString(this.getClass().getResourceAsStream("/837_5010/x12_complex.txt"), StandardCharsets.UTF_8) + .trim(); + + LineBreak lineBreak; + if (expected.contains(LineBreak.CRLF.getLineBreakString())) { + lineBreak = LineBreak.CRLF; + } + else { + lineBreak = LineBreak.LF; + } + X12Writer writer = new X12Writer(fromFileUtf8); + Assert.assertEquals(expected, writer.toX12String(lineBreak).trim()); + } + + +} diff --git a/src/test/java/lab/X12ToHtmlLab.java b/src/test/java/lab/X12ToHtmlLab.java index 6379400..2a938cd 100644 --- a/src/test/java/lab/X12ToHtmlLab.java +++ b/src/test/java/lab/X12ToHtmlLab.java @@ -1,6 +1,8 @@ package lab; import com.imsweb.x12.reader.X12Reader; +import com.imsweb.x12.writer.X12Writer; + import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -37,7 +39,8 @@ public void generateHtmlFromX12() throws Exception { String x12Template = IOUtils.toString(getClass().getResourceAsStream("/html/x12-template.html"), StandardCharsets.UTF_8); - String x12HtmlSegment = fromFileUtf8.toHtml(); + X12Writer writer = new X12Writer(fromFileUtf8); + String x12HtmlSegment = writer.toHtml(); String fullX12Html = String.format(x12Template, x12HtmlSegment); From 46f5ee566bdd2cb920f05bdaf282fa720670878e Mon Sep 17 00:00:00 2001 From: angelasd Date: Tue, 16 Jun 2020 11:46:19 -0400 Subject: [PATCH 2/7] fix some inspections (#33) --- .../java/com/imsweb/x12/reader/X12Reader.java | 1 - .../java/com/imsweb/x12/writer/X12Writer.java | 36 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/imsweb/x12/reader/X12Reader.java b/src/main/java/com/imsweb/x12/reader/X12Reader.java index f231e1b..4947120 100644 --- a/src/main/java/com/imsweb/x12/reader/X12Reader.java +++ b/src/main/java/com/imsweb/x12/reader/X12Reader.java @@ -19,7 +19,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import com.imsweb.x12.LineBreak; import com.imsweb.x12.Loop; import com.imsweb.x12.Segment; import com.imsweb.x12.Separators; diff --git a/src/main/java/com/imsweb/x12/writer/X12Writer.java b/src/main/java/com/imsweb/x12/writer/X12Writer.java index e66edcf..49bfc26 100644 --- a/src/main/java/com/imsweb/x12/writer/X12Writer.java +++ b/src/main/java/com/imsweb/x12/writer/X12Writer.java @@ -15,26 +15,26 @@ public class X12Writer { - private static List _dataLoops; - private static Separators _separators; - private static TransactionDefinition _definition; + private static List _DATA_LOOPS; + private static Separators _SEPARATORS; + private static TransactionDefinition _DEFINITION; public X12Writer(FileType fileType, List loops, Separators separators) { - _dataLoops = loops; - _definition = fileType.getDefinition(); - _separators = separators; + _DATA_LOOPS = loops; + _DEFINITION = fileType.getDefinition(); + _SEPARATORS = separators; } public X12Writer(FileType fileType, List loops) { - _dataLoops = loops; - _definition = fileType.getDefinition(); - _separators = new Separators(); + _DATA_LOOPS = loops; + _DEFINITION = fileType.getDefinition(); + _SEPARATORS = new Separators(); } public X12Writer(X12Reader reader) { - _dataLoops = reader.getLoops(); - _definition = reader.getDefinition(); - _separators = reader.getSeparators(); + _DATA_LOOPS = reader.getLoops(); + _DEFINITION = reader.getDefinition(); + _SEPARATORS = reader.getSeparators(); } @@ -55,15 +55,15 @@ public String toX12String() { * @return X12 formatted string representing this X12 reader. */ public String toX12String(LineBreak lineBreak) { - _separators.setLineBreak(lineBreak); + _SEPARATORS.setLineBreak(lineBreak); return toX12StringImpl(); } private String toX12StringImpl() { StringBuilder builder = new StringBuilder(); - for (Loop loop : _dataLoops) { - builder.append(loop.toX12String(_definition.getLoop())); - builder.append(_separators.getLineBreak().getLineBreakString()); + for (Loop loop : _DATA_LOOPS) { + builder.append(loop.toX12String(_DEFINITION.getLoop())); + builder.append(_SEPARATORS.getLineBreak().getLineBreakString()); } return builder.toString(); } @@ -75,8 +75,8 @@ private String toX12StringImpl() { */ public String toHtml() { StringBuilder builder = new StringBuilder(); - for (Loop loop : _dataLoops) { - builder.append(loop.toHtml(_definition.getLoop(), new ArrayList<>())); + for (Loop loop : _DATA_LOOPS) { + builder.append(loop.toHtml(_DEFINITION.getLoop(), new ArrayList<>())); } return builder.toString(); } From 9c4712c5e6bd1c780947b579211e3be99bc247ec Mon Sep 17 00:00:00 2001 From: angelasd Date: Tue, 16 Jun 2020 11:50:32 -0400 Subject: [PATCH 3/7] fix some inspections (#33) --- .../com/imsweb/x12/reader/X12ReaderTest.java | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java b/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java index bb58109..acd33d5 100644 --- a/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java +++ b/src/test/java/com/imsweb/x12/reader/X12ReaderTest.java @@ -15,20 +15,13 @@ import java.nio.charset.StandardCharsets; import java.util.List; -import org.apache.commons.io.IOUtils; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.select.Elements; import org.junit.Assert; import org.junit.Test; import com.imsweb.x12.Element; -import com.imsweb.x12.LineBreak; import com.imsweb.x12.Loop; -import com.imsweb.x12.X12; import com.imsweb.x12.mapping.TransactionDefinition; import com.imsweb.x12.reader.X12Reader.FileType; -import com.imsweb.x12.writer.X12Writer; public class X12ReaderTest { @@ -53,7 +46,7 @@ public void testConstructors() throws IOException { assertEquals(fromFileUtf8.getLoops().get(0).toString(), fromInputStreamUtf8.getLoops().get(0).toString()); assertEquals(fromFileUtf8.getLoops().get(0).toString(), fromReaderUtf8.getLoops().get(0).toString()); } - + @Test public void testMultipleGSLoops() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_multiple_gs.txt"); @@ -146,7 +139,7 @@ public void testWithInputStreamConstructor() throws Exception { validate837Valid(reader.getLoops().get(0)); } - + @Test public void testBadValidCode() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_bad_valid_code.txt"); @@ -944,14 +937,14 @@ public void testComplex() throws Exception { assertNotNull(loop.getLoop("ST_LOOP", 0).getSegment("ST")); assertEquals("837", loop.getLoop("ST_LOOP", 0).getSegment("ST").getElementValue("ST01")); assertEquals("987654", loop.getLoop("ST_LOOP", 0).getSegment("ST").getElementValue("ST02")); - assertNotNull(loop.getLoop("ST_LOOP",0).getSegment("SE")); + assertNotNull(loop.getLoop("ST_LOOP", 0).getSegment("SE")); assertEquals("25", loop.getLoop("ST_LOOP", 0).getSegment("SE").getElementValue("SE01")); assertEquals("987654", loop.getLoop("ST_LOOP", 0).getSegment("SE").getElementValue("SE02")); assertNotNull(loop.getLoop("ST_LOOP", 1).getSegment("ST")); assertEquals("837", loop.getLoop("ST_LOOP", 1).getSegment("ST").getElementValue("ST01")); assertEquals("987655", loop.getLoop("ST_LOOP", 1).getSegment("ST").getElementValue("ST02")); - assertNotNull(loop.getLoop("ST_LOOP",1).getSegment("SE")); + assertNotNull(loop.getLoop("ST_LOOP", 1).getSegment("SE")); assertEquals("26", loop.getLoop("ST_LOOP", 1).getSegment("SE").getElementValue("SE01")); assertEquals("987655", loop.getLoop("ST_LOOP", 1).getSegment("SE").getElementValue("SE02")); @@ -1295,14 +1288,14 @@ public void testX223Repeated2320() throws Exception { Assert.assertEquals(1, loop.getLoop("2000B").getLoop("2320", 0).findLoop("2330B").size()); Assert.assertEquals(1, loop.getLoop("2000B").getLoop("2320", 1).findLoop("2330A").size()); Assert.assertEquals(1, loop.getLoop("2000B").getLoop("2320", 1).findLoop("2330B").size()); - Assert.assertEquals("S", loop.getLoop("2320",0).getSegment(0).getElement("SBR01").getValue()); - Assert.assertEquals("T", loop.getLoop("2320",1).getSegment(0).getElement("SBR01").getValue()); - Assert.assertEquals("JOHN", loop.getLoop("2320",0).getLoop("2330A").getSegment(0).getElement("NM104").getValue()); - Assert.assertEquals("JANE", loop.getLoop("2320",1).getLoop("2330A").getSegment(0).getElement("NM104").getValue()); - Assert.assertEquals("AETNA", loop.getLoop("2320",0).getLoop("2330B").getSegment(0).getElement("NM103").getValue()); - Assert.assertEquals("ANOTHER NAME", loop.getLoop("2320",1).getLoop("2330B").getSegment(0).getElement("NM103").getValue()); + Assert.assertEquals("S", loop.getLoop("2320", 0).getSegment(0).getElement("SBR01").getValue()); + Assert.assertEquals("T", loop.getLoop("2320", 1).getSegment(0).getElement("SBR01").getValue()); + Assert.assertEquals("JOHN", loop.getLoop("2320", 0).getLoop("2330A").getSegment(0).getElement("NM104").getValue()); + Assert.assertEquals("JANE", loop.getLoop("2320", 1).getLoop("2330A").getSegment(0).getElement("NM104").getValue()); + Assert.assertEquals("AETNA", loop.getLoop("2320", 0).getLoop("2330B").getSegment(0).getElement("NM103").getValue()); + Assert.assertEquals("ANOTHER NAME", loop.getLoop("2320", 1).getLoop("2330B").getSegment(0).getElement("NM103").getValue()); } - + @Test public void test277CAAccepted() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_277CA_accepted.txt"); @@ -1331,7 +1324,7 @@ public void test277CAAccepted() throws Exception { Assert.assertEquals("Should be able to see a approval status code - CSCC", "A2", statusCodeElement.getSubValues().get(0)); Assert.assertEquals("Should be able to see a approval status code - CSC", "20", statusCodeElement.getSubValues().get(1)); } - + @Test public void test277CARejected() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_277CA_rejected.txt"); @@ -1353,7 +1346,7 @@ public void test277CARejected() throws Exception { Assert.assertEquals("Should be able to see a approval status code - CSC", "562", statusCodeElement.getSubValues().get(1)); Assert.assertEquals("Should be able to see a approval status code - EIC", "85", statusCodeElement.getSubValues().get(2)); } - + @Test public void test999Accepted() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_999_accepted.txt"); @@ -1364,9 +1357,9 @@ public void test999Accepted() throws Exception { Loop loop = reader.getLoops().get(0); assertEquals(1, loop.getLoops().size()); Assert.assertEquals("A", loop.getLoop("GS_LOOP").getLoop("ST_LOOP").getLoop("HEADER").getLoop("2000").getSegment("IK5").getElement("IK501").getValue()); - + } - + @Test public void test999Rejected() throws Exception { URL url = this.getClass().getResource("/837_5010/x12_999_rejected.txt"); @@ -1377,6 +1370,6 @@ public void test999Rejected() throws Exception { Loop loop = reader.getLoops().get(0); assertEquals(1, loop.getLoops().size()); Assert.assertEquals("R", loop.getLoop("GS_LOOP").getLoop("ST_LOOP").getLoop("HEADER").getLoop("2000").getSegment("IK5").getElement("IK501").getValue()); - + } } From 1e686d529ec98a7229520cef0bb9b0f86e07a8be Mon Sep 17 00:00:00 2001 From: angelasd Date: Tue, 16 Jun 2020 11:56:03 -0400 Subject: [PATCH 4/7] fix some inspections (#33) --- .../java/com/imsweb/x12/writer/X12Writer.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/imsweb/x12/writer/X12Writer.java b/src/main/java/com/imsweb/x12/writer/X12Writer.java index 49bfc26..1964aea 100644 --- a/src/main/java/com/imsweb/x12/writer/X12Writer.java +++ b/src/main/java/com/imsweb/x12/writer/X12Writer.java @@ -15,26 +15,26 @@ public class X12Writer { - private static List _DATA_LOOPS; - private static Separators _SEPARATORS; - private static TransactionDefinition _DEFINITION; + private final List _dataLoops; + private final Separators _separators; + private final TransactionDefinition _definition; public X12Writer(FileType fileType, List loops, Separators separators) { - _DATA_LOOPS = loops; - _DEFINITION = fileType.getDefinition(); - _SEPARATORS = separators; + _dataLoops = loops; + _definition = fileType.getDefinition(); + _separators = separators; } public X12Writer(FileType fileType, List loops) { - _DATA_LOOPS = loops; - _DEFINITION = fileType.getDefinition(); - _SEPARATORS = new Separators(); + _dataLoops = loops; + _definition = fileType.getDefinition(); + _separators = new Separators(); } public X12Writer(X12Reader reader) { - _DATA_LOOPS = reader.getLoops(); - _DEFINITION = reader.getDefinition(); - _SEPARATORS = reader.getSeparators(); + _dataLoops = reader.getLoops(); + _definition = reader.getDefinition(); + _separators = reader.getSeparators(); } @@ -55,15 +55,15 @@ public String toX12String() { * @return X12 formatted string representing this X12 reader. */ public String toX12String(LineBreak lineBreak) { - _SEPARATORS.setLineBreak(lineBreak); + _separators.setLineBreak(lineBreak); return toX12StringImpl(); } private String toX12StringImpl() { StringBuilder builder = new StringBuilder(); - for (Loop loop : _DATA_LOOPS) { - builder.append(loop.toX12String(_DEFINITION.getLoop())); - builder.append(_SEPARATORS.getLineBreak().getLineBreakString()); + for (Loop loop : _dataLoops) { + builder.append(loop.toX12String(_definition.getLoop())); + builder.append(_separators.getLineBreak().getLineBreakString()); } return builder.toString(); } @@ -75,8 +75,8 @@ private String toX12StringImpl() { */ public String toHtml() { StringBuilder builder = new StringBuilder(); - for (Loop loop : _DATA_LOOPS) { - builder.append(loop.toHtml(_DEFINITION.getLoop(), new ArrayList<>())); + for (Loop loop : _dataLoops) { + builder.append(loop.toHtml(_definition.getLoop(), new ArrayList<>())); } return builder.toString(); } From 0825fca8b8a67fd9ef004a71577d363354641dab Mon Sep 17 00:00:00 2001 From: angelasd Date: Tue, 16 Jun 2020 17:00:10 -0400 Subject: [PATCH 5/7] starting unit test for printing string from loop (#33) --- .../com/imsweb/x12/writer/X12WriterTest.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java index 7fc0eaa..a24d882 100644 --- a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java +++ b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Collections; import org.apache.commons.io.IOUtils; import org.jsoup.Jsoup; @@ -15,7 +16,11 @@ import org.junit.Assert; import org.junit.Test; +import com.imsweb.x12.Element; import com.imsweb.x12.LineBreak; +import com.imsweb.x12.Loop; +import com.imsweb.x12.Segment; +import com.imsweb.x12.Separators; import com.imsweb.x12.reader.X12Reader; import com.imsweb.x12.reader.X12Reader.FileType; @@ -98,5 +103,58 @@ public void testSerializeComplex() throws IOException { Assert.assertEquals(expected, writer.toX12String(lineBreak).trim()); } - + @Test + public void testPrintFromLoop() { + Separators separators = new Separators(); + separators.setLineBreak(LineBreak.CRLF); + Loop isaLoop = new Loop(separators, "ISA_LOOP"); + Segment segment = new Segment("ISA", separators); + addElement(segment, "01", "00", separators); + addElement(segment, "02", " ", separators); + addElement(segment, "03", "01", separators); + addElement(segment, "04", "SECRET ", separators); + addElement(segment, "05", "ZZ", separators); + addElement(segment, "06", "SUBMITTERS.ID ", separators); + addElement(segment, "07", "ZZ", separators); + addElement(segment, "08", "RECEIVERS.ID ", separators); + addElement(segment, "09", "030101", separators); + addElement(segment, "10", "1253", separators); + addElement(segment, "11", "U", separators); + addElement(segment, "12", "00501", separators); + addElement(segment, "13", "000000905", separators); + addElement(segment, "14", "1", separators); + addElement(segment, "15", "T", separators); + addElement(segment, "16", ":", separators); + isaLoop.addSegment(segment); + segment = new Segment("IEA", separators); + addElement(segment, "01", "1", separators); + addElement(segment, "02", "000000905", separators); + isaLoop.addSegment(segment); + + Loop gsLoop = new Loop(separators, "GS_LOOP"); + segment = new Segment("GS", separators); + addElement(segment, "01", "HC", separators); + addElement(segment, "02", "SENDER CODE", separators); + addElement(segment, "03", "RECEIVER CODE", separators); + addElement(segment, "04", "19991231", separators); + addElement(segment, "05", "0802", separators); + addElement(segment, "06", "1", separators); + addElement(segment, "07", "X", separators); + addElement(segment, "08", "005010X222A1", separators); + gsLoop.addSegment(segment); + segment = new Segment("GE", separators); + addElement(segment, "01", "1", separators); + addElement(segment, "02", "1", separators); + gsLoop.addSegment(segment); + isaLoop.getLoops().add(gsLoop); + + + X12Writer writer = new X12Writer(FileType.ANSI837_5010_X222, Collections.singletonList(isaLoop), separators); + //System.out.println(writer.toX12String(LineBreak.CRLF)); + } + + + private void addElement(Segment segment, String elementNum, String data, Separators separators) { + segment.addElement(new Element(segment.getId() + elementNum, data, separators)); + } } From 3a8777c1bbb333aa8cb3bdbd7f6c89532c48b9b7 Mon Sep 17 00:00:00 2001 From: angelasd Date: Wed, 17 Jun 2020 13:40:15 -0400 Subject: [PATCH 6/7] test for creating x12 string from loop (#33) --- .../com/imsweb/x12/writer/X12WriterTest.java | 322 +++++++++++++++--- .../resources/837_5010/x12_writer_test.txt | 35 ++ 2 files changed, 316 insertions(+), 41 deletions(-) create mode 100644 src/test/resources/837_5010/x12_writer_test.txt diff --git a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java index a24d882..8e8a79d 100644 --- a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java +++ b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java @@ -25,6 +25,7 @@ import com.imsweb.x12.reader.X12Reader.FileType; public class X12WriterTest { + /** * Here we will test that you can go from x12, make changes, then serialize the * x12 once again. @@ -104,57 +105,296 @@ public void testSerializeComplex() throws IOException { } @Test - public void testPrintFromLoop() { + public void testPrintFromLoop() throws Exception { Separators separators = new Separators(); separators.setLineBreak(LineBreak.CRLF); Loop isaLoop = new Loop(separators, "ISA_LOOP"); - Segment segment = new Segment("ISA", separators); - addElement(segment, "01", "00", separators); - addElement(segment, "02", " ", separators); - addElement(segment, "03", "01", separators); - addElement(segment, "04", "SECRET ", separators); - addElement(segment, "05", "ZZ", separators); - addElement(segment, "06", "SUBMITTERS.ID ", separators); - addElement(segment, "07", "ZZ", separators); - addElement(segment, "08", "RECEIVERS.ID ", separators); - addElement(segment, "09", "030101", separators); - addElement(segment, "10", "1253", separators); - addElement(segment, "11", "U", separators); - addElement(segment, "12", "00501", separators); - addElement(segment, "13", "000000905", separators); - addElement(segment, "14", "1", separators); - addElement(segment, "15", "T", separators); - addElement(segment, "16", ":", separators); + Segment segment = new Segment("ISA"); + addElement(segment, "01", "00"); + addElement(segment, "02", " "); + addElement(segment, "03", "01"); + addElement(segment, "04", "SECRET "); + addElement(segment, "05", "ZZ"); + addElement(segment, "06", "SUBMITTERS.ID "); + addElement(segment, "07", "ZZ"); + addElement(segment, "08", "RECEIVERS.ID "); + addElement(segment, "09", "030101"); + addElement(segment, "10", "1253"); + addElement(segment, "11", "U"); + addElement(segment, "12", "00501"); + addElement(segment, "13", "000000905"); + addElement(segment, "14", "1"); + addElement(segment, "15", "T"); + addElement(segment, "16", ":"); isaLoop.addSegment(segment); - segment = new Segment("IEA", separators); - addElement(segment, "01", "1", separators); - addElement(segment, "02", "000000905", separators); + segment = new Segment("IEA"); + addElement(segment, "01", "1"); + addElement(segment, "02", "000000905"); isaLoop.addSegment(segment); - + Loop gsLoop = new Loop(separators, "GS_LOOP"); - segment = new Segment("GS", separators); - addElement(segment, "01", "HC", separators); - addElement(segment, "02", "SENDER CODE", separators); - addElement(segment, "03", "RECEIVER CODE", separators); - addElement(segment, "04", "19991231", separators); - addElement(segment, "05", "0802", separators); - addElement(segment, "06", "1", separators); - addElement(segment, "07", "X", separators); - addElement(segment, "08", "005010X222A1", separators); + segment = new Segment("GS"); + addElement(segment, "01", "HC"); + addElement(segment, "02", "SENDER CODE"); + addElement(segment, "03", "RECEIVER CODE"); + addElement(segment, "04", "19991231"); + addElement(segment, "05", "0802"); + addElement(segment, "06", "1"); + addElement(segment, "07", "X"); + addElement(segment, "08", "005010X222A1"); gsLoop.addSegment(segment); - segment = new Segment("GE", separators); - addElement(segment, "01", "1", separators); - addElement(segment, "02", "1", separators); + segment = new Segment("GE"); + addElement(segment, "01", "1"); + addElement(segment, "02", "1"); gsLoop.addSegment(segment); isaLoop.getLoops().add(gsLoop); - - + + Loop stLoop = new Loop(separators, "ST_LOOP"); + segment = new Segment("ST"); + addElement(segment, "01", "837"); + addElement(segment, "02", "987654"); + addElement(segment, "03", "005010X222A1"); + stLoop.addSegment(segment); + segment = new Segment("SE"); + addElement(segment, "01", "25"); + addElement(segment, "02", "987654"); + stLoop.addSegment(segment); + gsLoop.getLoops().add(stLoop); + + Loop header = new Loop(separators, "HEADER"); + segment = new Segment("BHT"); + addElement(segment, "01", "0019"); + addElement(segment, "02", "00"); + addElement(segment, "03", "0123"); + addElement(segment, "04", "20200617"); + addElement(segment, "05", "0932"); + addElement(segment, "06", "CH"); + header.addSegment(segment); + Loop a1000 = new Loop(separators, "1000A"); + segment = new Segment("NM1"); + addElement(segment, "01", "41"); + addElement(segment, "02", "2"); + addElement(segment, "03", "MEDICAL FACILITY"); + addElement(segment, "04", ""); + addElement(segment, "05", ""); + addElement(segment, "06", ""); + addElement(segment, "07", ""); + addElement(segment, "08", "46"); + addElement(segment, "09", "999999999"); + a1000.addSegment(segment); + segment = new Segment("PER"); + addElement(segment, "01", "IC"); + addElement(segment, "02", "MEDICAL DOCTOR"); + addElement(segment, "03", "TE"); + addElement(segment, "04", "3016809770"); + addElement(segment, "05", "EX"); + addElement(segment, "06", "123"); + a1000.addSegment(segment); + Loop b1000 = new Loop(separators, "1000B"); + segment = new Segment("NM1"); + addElement(segment, "01", "40"); + addElement(segment, "02", "2"); + addElement(segment, "03", "HEALTH RECEIVER"); + addElement(segment, "04", ""); + addElement(segment, "05", ""); + addElement(segment, "06", ""); + addElement(segment, "07", ""); + addElement(segment, "08", "46"); + addElement(segment, "09", "111222333"); + b1000.addSegment(segment); + header.getLoops().add(a1000); + header.getLoops().add(b1000); + stLoop.getLoops().add(header); + + Loop detail = new Loop(separators, "DETAIL"); + Loop a2000 = new Loop(separators, "2000A"); + segment = new Segment("HL"); + addElement(segment, "01", "1"); + addElement(segment, "02", ""); + addElement(segment, "03", "20"); + addElement(segment, "04", "1"); + a2000.addSegment(segment); + Loop aa2010 = new Loop(separators, "2010AA"); + segment = new Segment("NM1"); + addElement(segment, "01", "85"); + addElement(segment, "02", "2"); + addElement(segment, "03", "MEDICAL GROUP"); + addElement(segment, "04", ""); + addElement(segment, "05", ""); + addElement(segment, "06", ""); + addElement(segment, "07", ""); + addElement(segment, "08", "XX"); + addElement(segment, "09", "1234567890"); + aa2010.addSegment(segment); + segment = new Segment("N3"); + addElement(segment, "01", "3901 CALVERTON BLVD"); + aa2010.addSegment(segment); + segment = new Segment("N4"); + addElement(segment, "01", "CALVERTON"); + addElement(segment, "02", "MD"); + addElement(segment, "03", "20705"); + aa2010.addSegment(segment); + segment = new Segment("REF"); + addElement(segment, "01", "EI"); + addElement(segment, "02", "123456789"); + aa2010.addSegment(segment); + segment = new Segment("PER"); + addElement(segment, "01", "IC"); + addElement(segment, "02", "JANE JONES"); + addElement(segment, "03", "TE"); + addElement(segment, "04", "3022893453"); + aa2010.addSegment(segment); + segment = new Segment("PER"); + addElement(segment, "01", "IC"); + addElement(segment, "02", "JANE JONES"); + addElement(segment, "03", "TE"); + addElement(segment, "04", "3012833053"); + addElement(segment, "05", "EX"); + addElement(segment, "06", "201"); + aa2010.addSegment(segment); + Loop ab2010 = new Loop(separators, "2010AB"); + segment = new Segment("NM1"); + addElement(segment, "01", "87"); + addElement(segment, "02", "2"); + ab2010.addSegment(segment); + segment = new Segment("N3"); + addElement(segment, "01", "227 LASTNER LANE"); + ab2010.addSegment(segment); + segment = new Segment("N4"); + addElement(segment, "01", "GREENBELT"); + addElement(segment, "02", "MD"); + addElement(segment, "03", "20770"); + ab2010.addSegment(segment); + Loop b2000 = new Loop(separators, "2000B"); + segment = new Segment("HL"); + addElement(segment, "01", "2"); + addElement(segment, "02", "1"); + addElement(segment, "03", "22"); + addElement(segment, "04", "1"); + b2000.addSegment(segment); + segment = new Segment("SBR"); + addElement(segment, "01", "P"); + addElement(segment, "02", ""); + addElement(segment, "03", "SUBSCRIBER GROUP"); + addElement(segment, "04", ""); + addElement(segment, "05", ""); + addElement(segment, "06", ""); + addElement(segment, "07", ""); + addElement(segment, "08", ""); + addElement(segment, "09", "CI"); + b2000.addSegment(segment); + Loop ba2010 = new Loop(separators, "2010BA"); + segment = new Segment("NM1"); + addElement(segment, "01", "IL"); + addElement(segment, "02", "1"); + addElement(segment, "03", "DOE"); + addElement(segment, "04", "JOHN"); + addElement(segment, "05", "T"); + addElement(segment, "06", ""); + addElement(segment, "07", "JR"); + addElement(segment, "08", "MI"); + addElement(segment, "09", "123456"); + ba2010.addSegment(segment); + segment = new Segment("N3"); + addElement(segment, "01", "123 MAIN STREET"); + addElement(segment, "02", "APARTMENT 9"); + ba2010.addSegment(segment); + segment = new Segment("N4"); + addElement(segment, "01", "RIVERDALE"); + addElement(segment, "02", "MD"); + addElement(segment, "03", "20737"); + ba2010.addSegment(segment); + segment = new Segment("DMG"); + addElement(segment, "01", "D8"); + addElement(segment, "02", "19611124"); + addElement(segment, "03", "M"); + ba2010.addSegment(segment); + Loop bb2010 = new Loop(separators, "2010BB"); + segment = new Segment("NM1"); + addElement(segment, "01", "PR"); + addElement(segment, "02", "2"); + addElement(segment, "03", "HEALTH INSURANCE COMPANY"); + addElement(segment, "04", ""); + addElement(segment, "05", ""); + addElement(segment, "06", ""); + addElement(segment, "07", ""); + addElement(segment, "08", "PI"); + addElement(segment, "09", "11122333"); + bb2010.addSegment(segment); + Loop l2300 = new Loop(separators, "2300"); + segment = new Segment("CLM"); + addElement(segment, "01", "A37YH556"); + addElement(segment, "02", "500"); + addElement(segment, "03", ""); + addElement(segment, "04", ""); + addElement(segment, "05", "11:B:1"); + addElement(segment, "06", "Y"); + addElement(segment, "07", "A"); + addElement(segment, "08", "Y"); + addElement(segment, "09", "I"); + addElement(segment, "10", "P"); + l2300.addSegment(segment); + segment = new Segment("DTP"); + addElement(segment, "01", "435"); + addElement(segment, "02", "D8"); + addElement(segment, "03", "20200515"); + l2300.addSegment(segment); + segment = new Segment("DTP"); + addElement(segment, "01", "096"); + addElement(segment, "02", "D8"); + addElement(segment, "03", "20200523"); + l2300.addSegment(segment); + segment = new Segment("HI"); + addElement(segment, "01", "BK:1739"); + addElement(segment, "02", ""); + addElement(segment, "03", ""); + l2300.addSegment(segment); + Loop l2400 = new Loop(separators, "2400"); + segment = new Segment("LX"); + addElement(segment, "01", "1"); + l2400.addSegment(segment); + segment = new Segment("SV1"); + addElement(segment, "01", "HC:99211:25"); + addElement(segment, "02", "12.25"); + addElement(segment, "03", "UN"); + addElement(segment, "04", "5"); + addElement(segment, "05", "11"); + addElement(segment, "06", ""); + addElement(segment, "07", "1:2:3"); + addElement(segment, "08", ""); + addElement(segment, "09", "Y"); + l2400.addSegment(segment); + segment = new Segment("DTP"); + addElement(segment, "01", "472"); + addElement(segment, "02", "RD8"); + addElement(segment, "03", "20200524-20200528"); + l2400.addSegment(segment); + segment = new Segment("DTP"); + addElement(segment, "01", "304"); + addElement(segment, "02", "D8"); + addElement(segment, "03", "20200530"); + l2400.addSegment(segment); + l2300.getLoops().add(l2400); + b2000.getLoops().add(l2300); + b2000.getLoops().add(bb2010); + b2000.getLoops().add(ba2010); + a2000.getLoops().add(b2000); + a2000.getLoops().add(ab2010); + a2000.getLoops().add(aa2010); + detail.getLoops().add(a2000); + stLoop.getLoops().add(detail); + X12Writer writer = new X12Writer(FileType.ANSI837_5010_X222, Collections.singletonList(isaLoop), separators); - //System.out.println(writer.toX12String(LineBreak.CRLF)); + String writerResult = writer.toX12String(LineBreak.CRLF).trim(); + + String expected = IOUtils + .toString(this.getClass().getResourceAsStream("/837_5010/x12_writer_test.txt"), StandardCharsets.UTF_8) + .trim(); + + Assert.assertEquals(expected, writerResult); } - - - private void addElement(Segment segment, String elementNum, String data, Separators separators) { - segment.addElement(new Element(segment.getId() + elementNum, data, separators)); + + private void addElement(Segment segment, String elementNum, String data) { + segment.addElement(new Element(segment.getId() + elementNum, data)); } } diff --git a/src/test/resources/837_5010/x12_writer_test.txt b/src/test/resources/837_5010/x12_writer_test.txt new file mode 100644 index 0000000..d466641 --- /dev/null +++ b/src/test/resources/837_5010/x12_writer_test.txt @@ -0,0 +1,35 @@ +ISA*00* *01*SECRET *ZZ*SUBMITTERS.ID *ZZ*RECEIVERS.ID *030101*1253*U*00501*000000905*1*T*:~ +GS*HC*SENDER CODE*RECEIVER CODE*19991231*0802*1*X*005010X222A1~ +ST*837*987654*005010X222A1~ +BHT*0019*00*0123*20200617*0932*CH~ +NM1*41*2*MEDICAL FACILITY*****46*999999999~ +PER*IC*MEDICAL DOCTOR*TE*3016809770*EX*123~ +NM1*40*2*HEALTH RECEIVER*****46*111222333~ +HL*1**20*1~ +NM1*85*2*MEDICAL GROUP*****XX*1234567890~ +N3*3901 CALVERTON BLVD~ +N4*CALVERTON*MD*20705~ +REF*EI*123456789~ +PER*IC*JANE JONES*TE*3022893453~ +PER*IC*JANE JONES*TE*3012833053*EX*201~ +NM1*87*2~ +N3*227 LASTNER LANE~ +N4*GREENBELT*MD*20770~ +HL*2*1*22*1~ +SBR*P**SUBSCRIBER GROUP******CI~ +NM1*IL*1*DOE*JOHN*T**JR*MI*123456~ +N3*123 MAIN STREET*APARTMENT 9~ +N4*RIVERDALE*MD*20737~ +DMG*D8*19611124*M~ +NM1*PR*2*HEALTH INSURANCE COMPANY*****PI*11122333~ +CLM*A37YH556*500***11:B:1*Y*A*Y*I*P~ +DTP*435*D8*20200515~ +DTP*096*D8*20200523~ +HI*BK:1739**~ +LX*1~ +SV1*HC:99211:25*12.25*UN*5*11**1:2:3**Y~ +DTP*472*RD8*20200524-20200528~ +DTP*304*D8*20200530~ +SE*25*987654~ +GE*1*1~ +IEA*1*000000905~ \ No newline at end of file From b5016d3bfec739271bacdab4ada1ba85b7c12932 Mon Sep 17 00:00:00 2001 From: angelasd Date: Wed, 17 Jun 2020 13:55:06 -0400 Subject: [PATCH 7/7] fix test (#33) --- .../com/imsweb/x12/writer/X12WriterTest.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java index 8e8a79d..5998770 100644 --- a/src/test/java/com/imsweb/x12/writer/X12WriterTest.java +++ b/src/test/java/com/imsweb/x12/writer/X12WriterTest.java @@ -106,8 +106,20 @@ public void testSerializeComplex() throws IOException { @Test public void testPrintFromLoop() throws Exception { + String expected = IOUtils + .toString(this.getClass().getResourceAsStream("/837_5010/x12_writer_test.txt"), StandardCharsets.UTF_8) + .trim(); + + LineBreak lineBreak; + if (expected.contains(LineBreak.CRLF.getLineBreakString())) { + lineBreak = LineBreak.CRLF; + } + else { + lineBreak = LineBreak.LF; + } + Separators separators = new Separators(); - separators.setLineBreak(LineBreak.CRLF); + separators.setLineBreak(lineBreak); Loop isaLoop = new Loop(separators, "ISA_LOOP"); Segment segment = new Segment("ISA"); addElement(segment, "01", "00"); @@ -385,11 +397,7 @@ public void testPrintFromLoop() throws Exception { stLoop.getLoops().add(detail); X12Writer writer = new X12Writer(FileType.ANSI837_5010_X222, Collections.singletonList(isaLoop), separators); - String writerResult = writer.toX12String(LineBreak.CRLF).trim(); - - String expected = IOUtils - .toString(this.getClass().getResourceAsStream("/837_5010/x12_writer_test.txt"), StandardCharsets.UTF_8) - .trim(); + String writerResult = writer.toX12String(lineBreak).trim(); Assert.assertEquals(expected, writerResult); }