package de.zalando.toga.provider;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static java.util.regex.Pattern.compile;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ProviderTest {
/**
* TESTMAP:
*
* - latest version
*/
@Test(expected = IllegalArgumentException.class)
public void exampleFolderDoesNotExist() {
new ExampleProvider(new File("this/does/not/exist"));
}
@Test(expected = IllegalArgumentException.class)
public void sourceDirIsActuallyAFile() {
new ExampleProvider(new File("src/test/resources/provider/E1-ServiceB-1.json"));
}
@Test(expected = IllegalArgumentException.class)
public void exampleFolderMustNotBeNull() {
new ExampleProvider(null);
}
@Test
public void nonExistingPatternShouldLeadToZeroResults() {
ExampleProvider provider = new ExampleProvider(new File("src/test/resources/provider"));
List<String> examples = provider.getExamples("not-matching-pattern");
assertThat(examples, is(not(nullValue())));
assertThat(examples.size(), is(0));
}
@Test
public void nonExistingPatternShouldLeadToZeroResultsWithPattern() {
ExampleProvider provider = new ExampleProvider(new File("src/test/resources/provider"));
List<String> examples = provider.getExamples(compile("not-matching-pattern"));
assertThat(examples, is(not(nullValue())));
assertThat(examples.size(), is(0));
}
@Test
public void simplePatternShouldGetAllExamples() {
ExampleProvider provider = new ExampleProvider(new File("src/test/resources/provider"));
List<String> examples = provider.getExamples("E1-ServiceB-1");
assertThat(examples, is(not(nullValue())));
assertThat(examples.size(), is(1));
}
@Test
public void versionPatternShouldGetAllExamples() {
ExampleProvider provider = new ExampleProvider(new File("src/test/resources/provider"));
List<String> examples = provider.getExamples("E1-ServiceB");
assertThat(examples, is(not(nullValue())));
assertThat(examples.size(), is(4));
}
@Test(expected = RuntimeException.class)
public void mapperThrowsException() throws IOException {
ExampleProvider provider = new ExampleProvider(new File("src/test/resources/provider"));
provider.mapper = mock(ObjectMapper.class);
when(provider.mapper.readValue(any(File.class), any(TypeReference.class))).thenThrow(new IOException());
provider.getExamples("E1-ServiceB");
}
}