/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.io;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.UUID;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junitpioneer.jupiter.TempDirectory.TempDir;

@ExtendWith(TempDirectory.class)
@DisplayName("Resource should")
class ResourceTest {

    @Test
    @DisplayName("throw ISE if queried for a non-existing file")
    void throwOnNonExisting(@TempDir Path path) {
        Path nonExistentFilePath = path.resolve(UUID.randomUUID()
                                                    .toString());
        File nonExistingFile = nonExistentFilePath.toFile();
        String name = nonExistingFile.getName();
        Resource file = Resource.file(name);
        assertThat(file.exists()).isFalse();
        assertThrows(IllegalStateException.class, file::locate);
    }

    @Test
    @DisplayName("correctly identify a file that is contained under the resources directory")
    void correctlyPickUrlsUp() throws IOException {
        Resource resource = Resource.file("test_resource.txt");
        assertThat(resource).isNotNull();
        assertThat(resource.exists()).isTrue();
        assertThat(resource.locate()).isNotNull();
        assertThat(resource.locateAll()).hasSize(1);
        try (InputStream stream = resource.open()) {
            assertThat(stream.available()).isGreaterThan(0);
        }
    }
}

@@ -1028,16 +1028,57 @@
 ne.io;%0A%0A
+import com.google.common.io.CharStreams;%0A
 import o
@@ -1111,16 +1111,16 @@
 ayName;%0A
-
 import o
@@ -1322,16 +1322,39 @@
 Stream;%0A
+import java.io.Reader;%0A
 import j
@@ -1396,16 +1396,125 @@
 .UUID;%0A%0A
+import static com.google.common.base.Charsets.US_ASCII;%0Aimport static com.google.common.base.Charsets.UTF_8;%0A
 import s
@@ -1772,16 +1772,90 @@
 Test %7B%0A%0A
+    private static final String EXISTING_RESOURCE = %22test_resource.txt%22;%0A%0A
     @Tes
@@ -2573,35 +2573,33 @@
 ce.file(
-%22test_resource.txt%22
+EXISTING_RESOURCE
 );%0A     
@@ -2901,16 +2901,16 @@
 han(0);%0A
-
         
@@ -2916,11 +2916,1037 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    @DisplayName(%22open as a byte stream%22)%0A    void openAsBytes() throws IOException %7B%0A        Resource resource = Resource.file(EXISTING_RESOURCE);%0A        try (InputStream stream = resource.open()) %7B%0A            assertThat(stream.available()).isGreaterThan(0);%0A        %7D%0A    %7D%0A%0A    @Test%0A    @DisplayName(%22open as a char stream%22)%0A    void openAsChars() throws IOException %7B%0A        Resource resource = Resource.file(EXISTING_RESOURCE);%0A        try (Reader reader = resource.openAsText(US_ASCII)) %7B%0A            String content = CharStreams.toString(reader);%0A            assertThat(content).isNotEmpty();%0A        %7D%0A    %7D%0A%0A    @Test%0A    @DisplayName(%22open as UTF-8%22)%0A    void openAsUtf8() throws IOException %7B%0A        Resource resource = Resource.file(EXISTING_RESOURCE);%0A        String defaultEncodedString = CharStreams.toString(resource.openAsText());%0A        String utf8EncodedString = CharStreams.toString(resource.openAsText(UTF_8));%0A        assertThat(defaultEncodedString).isEqualTo(utf8EncodedString);%0A    %7D
 %0A%7D%0A

Add a test for the new API in `Resource`