Search in sources :

Example 1 with ResourceLoader

use of org.jbehave.core.io.ResourceLoader in project jbehave-core by jbehave.

the class ExamplesTableFactoryBehaviour method shouldCreateExamplesTableFromResourceInput.

@Test
public void shouldCreateExamplesTableFromResourceInput() {
    // Given
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    ExamplesTableFactory factory = new ExamplesTableFactory(resourceLoader, new TableTransformers());
    // When
    String resourcePath = "/path/to/table";
    when(resourceLoader.loadResourceAsText(resourcePath)).thenReturn(TABLE_AS_STRING);
    ExamplesTable examplesTable = factory.createExamplesTable(resourcePath);
    // Then
    assertThat(examplesTable.asString(), equalTo(TABLE_AS_STRING));
}
Also used : ResourceLoader(org.jbehave.core.io.ResourceLoader) Test(org.junit.Test)

Example 2 with ResourceLoader

use of org.jbehave.core.io.ResourceLoader in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldCreateJsonFromResourceInput.

@Test
public void shouldCreateJsonFromResourceInput() {
    // Given
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    ParameterConverters.JsonFactory factory = new ParameterConverters.JsonFactory(resourceLoader);
    // When
    String resourcePath = "/path/to/json";
    when(resourceLoader.loadResourceAsText(resourcePath)).thenReturn(JSON_AS_STRING);
    MyJsonDto json = (MyJsonDto) factory.createJson(resourcePath, MyJsonDto.class);
    // Then
    assertThat(new Gson().toJson(json), equalTo(JSON_AS_STRING));
}
Also used : ResourceLoader(org.jbehave.core.io.ResourceLoader) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 3 with ResourceLoader

use of org.jbehave.core.io.ResourceLoader in project jbehave-core by jbehave.

the class ImportToFilesystemBehaviour method canImportToFilesystem.

@Test
public void canImportToFilesystem() throws IOException {
    // Given
    ResourceIndexer indexer = mock(ResourceIndexer.class);
    ResourceLoader loader = mock(ResourceLoader.class);
    String rootURI = "http://wiki";
    Map<String, Resource> index = new HashMap<>();
    index.put("one", new Resource(rootURI + "/one"));
    index.put("two", new Resource(rootURI + "/two"));
    when(indexer.indexResources(rootURI)).thenReturn(index);
    String text1 = "story text 1";
    when(loader.loadResourceAsText(index.get("one").getURI())).thenReturn(text1);
    String text2 = "story text 2";
    when(loader.loadResourceAsText(index.get("two").getURI())).thenReturn(text2);
    // When
    String targetPath = "target/stories";
    String targetExt = ".story";
    ResourceImporter importer = new ImportToFilesystem(indexer, loader, targetPath, targetExt);
    importer.importResources(rootURI);
    // Then
    File file1 = new File(targetPath + "/one" + targetExt);
    assertThat(file1.exists(), equalTo(true));
    assertThat(readFileToString(file1, StandardCharsets.UTF_8), equalTo(text1));
    File file2 = new File(targetPath + "/two" + targetExt);
    assertThat(file2.exists(), equalTo(true));
    assertThat(readFileToString(file2, StandardCharsets.UTF_8), equalTo(text2));
}
Also used : ResourceLoader(org.jbehave.core.io.ResourceLoader) HashMap(java.util.HashMap) ImportToFilesystem(org.jbehave.core.io.rest.filesystem.ImportToFilesystem) ResourceImporter(org.jbehave.core.io.rest.ResourceImporter) Resource(org.jbehave.core.io.rest.Resource) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) File(java.io.File) ResourceIndexer(org.jbehave.core.io.rest.ResourceIndexer) Test(org.junit.Test)

Example 4 with ResourceLoader

use of org.jbehave.core.io.ResourceLoader in project jbehave-core by jbehave.

the class ImportToFilesystemMojoBehaviour method canImportToFilesystem.

@Test
public void canImportToFilesystem() throws IOException, MojoExecutionException, MojoFailureException {
    // Given
    final ResourceIndexer indexer = mock(ResourceIndexer.class);
    final ResourceLoader loader = mock(ResourceLoader.class);
    String rootURI = "http://wiki";
    Map<String, Resource> index = new HashMap<>();
    index.put("one", new Resource(rootURI + "/one"));
    index.put("two", new Resource(rootURI + "/two"));
    when(indexer.indexResources(rootURI)).thenReturn(index);
    String text1 = "story text 1";
    when(loader.loadResourceAsText(index.get("one").getURI())).thenReturn(text1);
    String text2 = "story text 2";
    when(loader.loadResourceAsText(index.get("two").getURI())).thenReturn(text2);
    // When
    String targetPath = "target/stories";
    String targetExt = ".story";
    ImportToFilesystemMojo mojo = new ImportToFilesystemMojo() {

        @Override
        ResourceIndexer newResourceIndexer() {
            return indexer;
        }

        @Override
        ResourceLoader newResourceLoader() {
            return loader;
        }
    };
    mojo.restProvider = "wiki";
    mojo.restRootURI = rootURI;
    mojo.resourcesPath = targetPath;
    mojo.resourcesExt = targetExt;
    mojo.execute();
    // Then
    File file1 = new File(targetPath + "/one" + targetExt);
    assertThat(file1.exists(), equalTo(true));
    assertThat(readFileToString(file1, StandardCharsets.UTF_8), equalTo(text1));
    File file2 = new File(targetPath + "/two" + targetExt);
    assertThat(file2.exists(), equalTo(true));
    assertThat(readFileToString(file2, StandardCharsets.UTF_8), equalTo(text2));
}
Also used : ResourceLoader(org.jbehave.core.io.ResourceLoader) HashMap(java.util.HashMap) Resource(org.jbehave.core.io.rest.Resource) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) File(java.io.File) ResourceIndexer(org.jbehave.core.io.rest.ResourceIndexer) Test(org.junit.Test)

Example 5 with ResourceLoader

use of org.jbehave.core.io.ResourceLoader in project jbehave-core by jbehave.

the class RESTSteps method storyIsLoaded.

@When("story $name text contains '$text'")
public void storyIsLoaded(String name, String text) {
    ResourceLoader loader = resourceLoader();
    Resource resource = index.get(name);
    String asText = loader.loadResourceAsText(resource.getURI());
    assertThat(asText, containsString(text));
}
Also used : ResourceLoader(org.jbehave.core.io.ResourceLoader) Resource(org.jbehave.core.io.rest.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) When(org.jbehave.core.annotations.When)

Aggregations

ResourceLoader (org.jbehave.core.io.ResourceLoader)7 Test (org.junit.Test)5 Resource (org.jbehave.core.io.rest.Resource)3 ResourceIndexer (org.jbehave.core.io.rest.ResourceIndexer)3 File (java.io.File)2 HashMap (java.util.HashMap)2 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ImportToFilesystem (org.jbehave.core.io.rest.filesystem.ImportToFilesystem)2 Gson (com.google.gson.Gson)1 When (org.jbehave.core.annotations.When)1 ResourceImporter (org.jbehave.core.io.rest.ResourceImporter)1