/*
 * 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.json;

import com.google.common.collect.Lists;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.StringValue;
import com.google.protobuf.TypeRegistry;
import io.spine.json.given.Node;
import io.spine.json.given.WrappedString;
import io.spine.testing.UtilityClassTest;
import io.spine.type.KnownTypes;
import io.spine.type.TypeUrl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static io.spine.base.Identifier.newUuid;
import static io.spine.json.Json.fromJson;
import static io.spine.json.Json.toCompactJson;
import static io.spine.json.Json.toJson;
import static io.spine.testing.Assertions.assertNpe;
import static io.spine.testing.TestValues.nullRef;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@DisplayName("Json utility class should")
class JsonTest extends UtilityClassTest<Json> {

    JsonTest() {
        super(Json.class);
    }

    @Test
    @DisplayName("build JsonFormat registry for known types")
    void knownTypes() {
        TypeRegistry typeRegistry = Json.typeRegistry();

        List<Descriptor> found = Lists.newLinkedList();
        for (TypeUrl typeUrl : KnownTypes.instance()
                                         .allUrls()) {
            Descriptor descriptor = typeRegistry.find(typeUrl.toTypeName()
                                                             .value());
            if (descriptor != null) {
                found.add(descriptor);
            }
        }

        assertFalse(found.isEmpty());
    }

    @Test
    @DisplayName("not allow null message")
    void rejectNulls() {
        assertNpe(() -> toJson(nullRef()));
    }

    @Test
    @DisplayName("print to JSON")
    void print() {
        StringValue value = StringValue.of("print_to_json");
        assertFalse(toJson(value).isEmpty());
    }

    @Test
    @DisplayName("print to compact JSON")
    void printCompact() {
        String idValue = newUuid();
        Node node = Node.newBuilder()
                        .setName(idValue)
                        .setRight(Node.getDefaultInstance())
                        .build();
        String result = toCompactJson(node);
        assertFalse(result.isEmpty());
        assertFalse(result.contains(System.lineSeparator()));
    }

    @Test
    @DisplayName("parse from JSON")
    void parse() {
        String idValue = newUuid();
        String jsonMessage = format("{\"value\": \"%s\"}", idValue);
        WrappedString parsedValue = fromJson(jsonMessage, WrappedString.class);
        assertNotNull(parsedValue);
        assertEquals(idValue, parsedValue.getValue());
    }

    @Test
    @DisplayName("parse from JSON with unknown values")
    void parseUnknown() {
        String idValue = newUuid();
        String jsonMessage = format("{\"value\": \"%s\", \"newField\": \"newValue\"}", idValue);
        WrappedString parsedValue = fromJson(jsonMessage, WrappedString.class);
        assertNotNull(parsedValue);
        assertEquals(idValue, parsedValue.getValue());
    }
}

@@ -1395,49 +1395,8 @@
 ue;%0A
-import com.google.protobuf.TypeRegistry;%0A
 impo
@@ -1545,38 +1545,8 @@
 es;%0A
-import io.spine.type.TypeUrl;%0A
 impo
@@ -2164,20 +2164,22 @@
 ayName(%22
+%60
 Json
+%60
  utility
@@ -2402,20 +2402,11 @@
     
-TypeRegistry
+var
  typ
@@ -2512,15 +2512,11 @@
 or (
-TypeUrl
+var
  typ
@@ -2547,50 +2547,8 @@
 ce()
-%0A                                         
 .all
@@ -2569,25 +2569,18 @@
         
-Descripto
+va
 r descri
@@ -2625,23 +2625,16 @@
 eName()%0A
-       
         
@@ -3037,27 +3037,19 @@
         
-StringValue
+var
  value =
@@ -3213,38 +3213,35 @@
 act() %7B%0A        
-String
+var
  idValue = newUu
@@ -3254,20 +3254,19 @@
         
-Node
+var
  node = 
@@ -3295,32 +3295,24 @@
             
-    
-    
 .setName(idV
@@ -3317,24 +3317,16 @@
 dValue)%0A
-        
         
@@ -3386,24 +3386,16 @@
         
-      
-  
 .build()
@@ -3404,22 +3404,19 @@
         
-String
+var
  result 
@@ -3611,38 +3611,35 @@
 rse() %7B%0A        
-String
+var
  idValue = newUu
@@ -3644,38 +3644,35 @@
 Uuid();%0A        
-String
+var
  jsonMessage = f
@@ -3714,37 +3714,27 @@
 e);%0A        
-WrappedString
+var
  parsedValue
@@ -3978,22 +3978,19 @@
         
-String
+var
  idValue
@@ -4011,22 +4011,19 @@
         
-String
+var
  jsonMes
@@ -4105,29 +4105,19 @@
         
-WrappedString
+var
  parsedV

Use `var`s in `io.spine.json` test package.