Skip to content

Commit

Permalink
feat: add readAll
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jul 26, 2016
1 parent b92095a commit 220ad40
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.qoomon.banking.swift.message.exception.SwiftMessageParseException;

import java.io.Reader;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -28,7 +30,16 @@ public SwiftMessageReader(Reader textReader) {
this.blockReader = new SwiftBlockReader(textReader);
}

public SwiftMessage readMessage() throws SwiftMessageParseException {
public List<SwiftMessage> readAll() throws SwiftMessageParseException {
List<SwiftMessage> result = new LinkedList<>();
SwiftMessage message;
while ((message = read()) != null) {
result.add(message);
}
return result;
}

public SwiftMessage read() throws SwiftMessageParseException {
try {
if (currentBlock == null) {
nextBlock = blockReader.readBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public GeneralBlock readBlock() throws BlockParseException {
}

if (openingBrackets != closingBrackets) {
throw new BlockParseException("Parse error: unclosed '{'", lineNumber);
throw new BlockParseException("Unclosed '{'", lineNumber);
}

return block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.qoomon.banking.swift.message.exception.SwiftMessageParseException;
import com.qoomon.banking.swift.submessage.exception.SubMessageParserException;
import com.qoomon.banking.swift.submessage.field.*;
import com.qoomon.banking.swift.submessage.mt942.MT942Page;

import java.io.Reader;
import java.util.LinkedList;
Expand Down Expand Up @@ -34,7 +35,16 @@ public MT940PageReader(Reader textReader) {
this.fieldReader = new SwiftFieldReader(textReader);
}

public MT940Page readPage() throws SwiftMessageParseException {
public List<MT940Page> readAll() throws SwiftMessageParseException {
List<MT940Page> result = new LinkedList<>();
MT940Page page;
while ((page = read()) != null) {
result.add(page);
}
return result;
}

public MT940Page read() throws SwiftMessageParseException {
try {
if (currentField == null) {
nextField = fieldReader.readField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ public MT942PageReader(Reader textReader) {
this.fieldReader = new SwiftFieldReader(textReader);
}

public MT942Page readPage() throws SwiftMessageParseException {
public List<MT942Page> readAll() throws SwiftMessageParseException {
List<MT942Page> result = new LinkedList<>();
MT942Page page;
while ((page = read()) != null) {
result.add(page);
}
return result;
}

public MT942Page read() throws SwiftMessageParseException {
try {
if (currentField == null) {
nextField = fieldReader.readField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void parse_WHEN_detecting_whitespaces_between_blocks_THEN_throw_exception
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(() -> classUnderTest.readMessage());
Throwable exception = catchThrowable(() -> classUnderTest.read());
// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);

Expand All @@ -61,7 +61,7 @@ public void parse_SHOULD_read_multiple_messages() throws Exception {
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
List<SwiftMessage> messageList = TestUtils.collectUntilNull(() -> classUnderTest.readMessage());
List<SwiftMessage> messageList = TestUtils.collectUntilNull(() -> classUnderTest.read());
// Then
assertThat(messageList).hasSize(2);
}
Expand All @@ -77,7 +77,7 @@ public void parse_WHEN_first_bracket_is_missing_THEN_throw_exception() throws Ex
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readMessage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -97,7 +97,7 @@ public void parse_WHEN_block_structure_is_wrong_THEN_throw_exception() throws Ex
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readMessage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -116,7 +116,7 @@ public void parse_WHEN_message_is_not_finished_THEN_throw_exception() throws Exc
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readMessage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -137,7 +137,7 @@ public void parse_WHEN_unknown_block_appears_THEN_throw_exception() throws Excep
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readMessage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -157,7 +157,7 @@ public void parse_WHEN_brackets_are_unbalanced_THEN_throw_exception() throws Exc
SwiftMessageReader classUnderTest = new SwiftMessageReader(new StringReader(swiftMessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readMessage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).as("Exception").isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -180,7 +180,7 @@ public void parse_SHOULD_parse_valid_files() throws Exception {
try {
System.out.println(filePath);
SwiftMessageReader classUnderTest = new SwiftMessageReader(new FileReader(filePath.toFile()));
classUnderTest.readMessage();
classUnderTest.read();
} catch (Exception e) {
System.out.println(Throwables.getStackTraceAsString(e));
errors[0]++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.qoomon.banking.swift.message.block;

import com.qoomon.banking.TestUtils;
import com.qoomon.banking.swift.message.block.exception.BlockParseException;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.io.StringReader;
import java.util.List;

import static org.assertj.core.api.Assertions.*;

/**
* Created by qoomon on 26/07/16.
*/
public class SwiftBlockReaderTest {

@Test
public void readBlock_SHOULD_read_valid_blocks() throws Exception {
// Given

String blockText = "{1:a}{2:b}{3:c}";

SwiftBlockReader subjectUnderTest = new SwiftBlockReader(new StringReader(blockText));

// When
List<GeneralBlock> blockList = TestUtils.collectUntilNull(subjectUnderTest::readBlock);

// Then

assertThat(blockList).hasSize(3);
assertThat(blockList.get(0).getId()).isEqualTo("1");
assertThat(blockList.get(0).getContent()).isEqualTo("a");
assertThat(blockList.get(1).getId()).isEqualTo("2");
assertThat(blockList.get(1).getContent()).isEqualTo("b");
assertThat(blockList.get(2).getId()).isEqualTo("3");
assertThat(blockList.get(2).getContent()).isEqualTo("c");

}

@Test
public void readBlock_WHEN_unfinished_block_detected_THROW_exception() throws Exception {
// Given

String blockText = "{1:a}{2:b}{3:c";

SwiftBlockReader subjectUnderTest = new SwiftBlockReader(new StringReader(blockText));

// When
Throwable exception = catchThrowable(() -> TestUtils.collectUntilNull(subjectUnderTest::readBlock));

// Then
assertThat(exception).isInstanceOf(BlockParseException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void parse_WHEN_parse_valid_file_RETURN_message() throws Exception {
MT940PageReader classUnderTest = new MT940PageReader(new StringReader(mt940MessageText));

// When
List<MT940Page> pageList = TestUtils.collectUntilNull(classUnderTest::readPage);
List<MT940Page> pageList = TestUtils.collectUntilNull(classUnderTest::read);

// Then
assertThat(pageList).hasSize(1);
Expand All @@ -67,7 +67,7 @@ public void parse_WHEN_unfinished_page_detected_THROW_exception() throws Excepti
MT940PageReader classUnderTest = new MT940PageReader(new StringReader(mt940MessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readPage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -86,7 +86,7 @@ public void parse_WHEN_parse_many_valid_file_RETURN_message() throws Exception {
files.forEach(filePath -> {
try {
MT940PageReader classUnderTest = new MT940PageReader(new FileReader(filePath.toFile()));
List<MT940Page> messageList = TestUtils.collectUntilNull(classUnderTest::readPage);
List<MT940Page> messageList = TestUtils.collectUntilNull(classUnderTest::read);
assertThat(messageList).isNotEmpty();
} catch (Exception e) {
System.out.println(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void parse_WHEN_parse_valid_file_RETURN_message() throws Exception {
MT942PageReader classUnderTest = new MT942PageReader(new StringReader(mt920MessageText));

// When
List<MT942Page> pageList = TestUtils.collectUntilNull(classUnderTest::readPage);
List<MT942Page> pageList = TestUtils.collectUntilNull(classUnderTest::read);

// Then
assertThat(pageList).hasSize(1);
Expand All @@ -69,7 +69,7 @@ public void parse_WHEN_unfinished_page_detected_THROW_exception() throws Excepti
MT940PageReader classUnderTest = new MT940PageReader(new StringReader(mt940MessageText));

// When
Throwable exception = catchThrowable(classUnderTest::readPage);
Throwable exception = catchThrowable(classUnderTest::read);

// Then
assertThat(exception).isInstanceOf(SwiftMessageParseException.class);
Expand All @@ -89,7 +89,7 @@ public void parse_WHEN_parse_many_valid_file_RETURN_message() throws Exception {
files.forEach(filePath -> {
try {
MT942PageReader classUnderTest = new MT942PageReader(new FileReader(filePath.toFile()));
List<MT942Page> pageList = TestUtils.collectUntilNull(classUnderTest::readPage);
List<MT942Page> pageList = TestUtils.collectUntilNull(classUnderTest::read);
assertThat(pageList).isNotEmpty();
} catch (Exception e) {
System.out.println(filePath);
Expand Down

0 comments on commit 220ad40

Please sign in to comment.