/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.net.string;

import com.google.common.truth.Truth8;
import io.spine.net.EmailAddress;
import io.spine.net.InternetDomain;
import io.spine.net.Url;
import io.spine.string.Stringifier;
import io.spine.string.StringifierRegistry;
import io.spine.testing.UtilityClassTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static com.google.common.truth.Truth.assertThat;
import static io.spine.net.string.NetStringifiers.forEmailAddress;
import static io.spine.net.string.NetStringifiers.forInternetDomain;
import static io.spine.net.string.NetStringifiers.forUrl;

class NetStringifiersTest extends UtilityClassTest<NetStringifiers> {

    NetStringifiersTest() {
        super(NetStringifiers.class);
    }

    @Test
    @DisplayName("register stringifiers")
    void registration() {
        assertRegistered(forUrl(), Url.class);
        assertRegistered(forEmailAddress(), EmailAddress.class);
        assertRegistered(forInternetDomain(), InternetDomain.class);
    }

    private static <T> void assertRegistered(Stringifier<T> stringifier, Class<T> cls) {
        Optional<Stringifier<T>> optional = StringifierRegistry.instance()
                                                               .find(cls);
        Truth8.assertThat(optional).hasValue(stringifier);
    }

    @Nested
    @DisplayName("convert to string and back")
    class Convert {

        @Test
        @DisplayName("Url")
        void url() {
            assertStringifier(Url.class, "https://spine.io/about");
        }

        @Test
        @DisplayName("InternetDomain")
        void internetDomain() {
            assertStringifier(InternetDomain.class, "spine.io");
        }

        @Test
        @DisplayName("EmailAddress")
        void emailAddress() {
            assertStringifier(EmailAddress.class, "info@spine.io");
        }

        <T> void assertStringifier(Class<T> cls, String value) {
            Optional<Stringifier<T>> optional = StringifierRegistry.instance()
                                                                   .find(cls);
            Truth8.assertThat(optional).isPresent();

            @SuppressWarnings("OptionalGetWithoutIsPresent") // checked above.
                    Stringifier<T> stringifier = optional.get();

            T object = stringifier.reverse()
                                  .convert(value);
            assertThat(stringifier.convert(object))
                    .isEqualTo(value);
        }
    }
}

@@ -1295,20 +1295,22 @@
 mon.
-truth.Truth8
+base.Converter
 ;%0Aim
@@ -1723,16 +1723,73 @@
 rtThat;%0A
+import static com.google.common.truth.Truth8.assertThat;%0A
 import s
@@ -2522,102 +2522,18 @@
 l = 
-StringifierRegistry.instance()%0A                                                               
+registry()
 .fin
@@ -2540,39 +2540,32 @@
 d(cls);%0A        
-Truth8.
 assertThat(optio
@@ -3202,163 +3202,151 @@
 ing 
-value) %7B%0A            Optional%3CStringifier%3CT%3E%3E optional = StringifierRegistry.instance()%0A                                                                   
+stringForm) %7B%0A            // Check that there is a stringifier for the passed class.%0A            Optional%3CStringifier%3CT%3E%3E optional = registry()
 .fin
@@ -3365,23 +3365,16 @@
         
-Truth8.
 assertTh
@@ -3416,141 +3416,195 @@
     
-@SuppressWarnings(%22OptionalGetWithoutIsPresent%22) // checked above.%0A                    Stringifier%3CT%3E stringifier = optional.get
+Stringifier%3CT%3E stringifier = optional.get();%0A%0A            // Parse the object back from the passed string representation.%0A            Converter%3CString, T%3E parser = stringifier.reverse
 ();%0A
-%0A
     
@@ -3626,64 +3626,157 @@
 t = 
-stringifier.reverse()%0A                                  
+parser.convert(stringForm);%0A%0A            // Now check that object is stringified to the passed form.%0A            String stringifierForm = stringifier
 .con
@@ -3780,24 +3780,26 @@
 convert(
-value
+object
 );%0A
+%0A
         
@@ -3828,25 +3828,36 @@
 fier
-.convert(object))
+Form).isEqualTo(stringForm);
 %0A   
@@ -3865,48 +3865,115 @@
     
- 
+%7D%0A
     
+%7D%0A%0A
     
-   .isEqualTo(value);%0A        %7D
+private static StringifierRegistry registry() %7B%0A        return StringifierRegistry.instance();
 %0A   

Document test flow