Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Apr 17, 2018
1 parent 185502b commit b75e813
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class EventSourcesFileParser {

private static final int FIRST_ELEMENT = 0;

private final YamlParser yamlParser;
private final YamlFileValidator yamlFileValidator;

Expand All @@ -22,7 +24,7 @@ public EventSourcesFileParser(final YamlParser yamlParser, final YamlFileValidat

public EventSources getEventSources(final Path baseDir, final Collection<Path> paths) {
final List<EventSources> eventSources = paths.stream()
.filter(path -> isEventSource(path))
.filter(this::isEventSource)
.map(path -> parseEventSourcesFromYaml(baseDir, path))
.collect(toList());

Expand All @@ -32,10 +34,12 @@ public EventSources getEventSources(final Path baseDir, final Collection<Path> p
if (moreThenOneEventSourcesPresent) {
throw new SubscriptionFileParserException("More then one event-sources.yaml files found!");
}

if (noEventSources) {
throw new SubscriptionFileParserException("No event-sources.yaml files found!");
}
return eventSources.get(0);

return eventSources.get(FIRST_ELEMENT);
}

private EventSources parseEventSourcesFromYaml(final Path baseDir,final Path path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package uk.gov.justice.subscription.jms.parser;

public class SubscriptionFileParserException extends RuntimeException{
public class SubscriptionFileParserException extends RuntimeException {

public SubscriptionFileParserException(final String message, final Throwable cause) {
super(message, cause);
}
public SubscriptionFileParserException(final String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package uk.gov.justice.subscription.jms.parser;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import uk.gov.justice.subscription.domain.eventsource.EventSources;
import uk.gov.justice.subscription.yaml.parser.YamlFileValidator;
import uk.gov.justice.subscription.yaml.parser.YamlParser;

import java.nio.file.Path;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class EventSourcesFileParserTest {

@Mock
private YamlParser yamlParser;

@Mock
private YamlFileValidator yamlFileValidator;

@InjectMocks
private EventSourcesFileParser eventSourcesFileParser;

@Test
public void shouldReturnSingleEventSources() {
final Path baseDir = mock(Path.class);
final Path resolvedPath = mock(Path.class);

final Path eventSourcePath = mock(Path.class);
final Path subscriptionDescriptorPath = mock(Path.class);
final List<Path> pathList = asList(eventSourcePath, subscriptionDescriptorPath);

final EventSources eventSources = mock(EventSources.class);

when(subscriptionDescriptorPath.endsWith("event-sources.yaml")).thenReturn(false);
when(eventSourcePath.endsWith("event-sources.yaml")).thenReturn(true);

when(baseDir.resolve(eventSourcePath)).thenReturn(resolvedPath);
when(yamlParser.parseYamlFrom(resolvedPath, EventSources.class)).thenReturn(eventSources);


final EventSources expectedEventSources = eventSourcesFileParser.getEventSources(baseDir, pathList);

assertThat(expectedEventSources, is(eventSources));
}

@Test
public void shouldThrowExceptionIfMoreThanOneEventSourcesPresent() {
final Path baseDir = mock(Path.class);
final Path resolvedPath = mock(Path.class);

final Path eventSourcePath = mock(Path.class);
final Path subscriptionDescriptorPath = mock(Path.class);
final List<Path> pathList = asList(eventSourcePath, eventSourcePath, subscriptionDescriptorPath);

final EventSources eventSources = mock(EventSources.class);

when(subscriptionDescriptorPath.endsWith("event-sources.yaml")).thenReturn(false);
when(eventSourcePath.endsWith("event-sources.yaml")).thenReturn(true);

when(baseDir.resolve(eventSourcePath)).thenReturn(resolvedPath);
when(yamlParser.parseYamlFrom(resolvedPath, EventSources.class)).thenReturn(eventSources);

try {
eventSourcesFileParser.getEventSources(baseDir, pathList);
fail();
} catch (final SubscriptionFileParserException e) {
assertThat(e.getMessage(), is("More then one event-sources.yaml files found!"));
}
}

@Test
public void shouldThrowExceptionIfNoEventSourcesPresent() {
final Path baseDir = mock(Path.class);

try {
eventSourcesFileParser.getEventSources(baseDir, emptyList());
fail();
} catch (final SubscriptionFileParserException e) {
assertThat(e.getMessage(), is("No event-sources.yaml files found!"));
}
}
}

0 comments on commit b75e813

Please sign in to comment.