/**
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabric8.it.certmanager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.dapr.v1alpha1.Component;
import io.dapr.v1alpha1.ComponentSpec;
import io.dapr.v1alpha1.componentspec.Metadata;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;
import io.fabric8.java.generator.testing.KubernetesResourceDiff;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestPreserveUnknownUsability {

  @Test
  void testDeserialization() {
    // Arrange
    Component sample =
      Serialization.unmarshal(getClass().getResourceAsStream("/sample.yaml"), Component.class);

    // Act
    List<Metadata> metadataList = sample.getSpec().getMetadata();

    // Assert
    assertEquals(5, metadataList.size());
  }

  private Component createSampleComponent() throws Exception {
    Component component = new Component();
    ComponentSpec spec = new ComponentSpec();
    spec.setType("pubsub.mqtt");
    spec.setVersion("v1");

    Metadata meta1 = new Metadata();
    meta1.setName("consumerID");
    meta1.setValue(new TextNode("{uuid}"));

    Metadata meta2 = new Metadata();
    meta2.setName("url");
    meta2.setValue(new TextNode("tcp://admin:public@localhost:1883"));

    Metadata meta3 = new Metadata();
    meta3.setName("qos");
    meta3.setValue(new IntNode(1));

    Metadata meta4 = new Metadata();
    meta4.setName("anArray");
    meta4.setValue(Serialization.jsonMapper().readTree("[1,2,3]"));

    Metadata meta5 = new Metadata();
    meta5.setName("anObject");
    meta5.setValue(Serialization.jsonMapper().readTree("{ \"ONE\": 1, \"TWO\": 2 }"));

    List<Metadata> lst = new ArrayList();
    lst.add(meta1);
    lst.add(meta2);
    lst.add(meta3);
    lst.add(meta4);
    lst.add(meta5);
    spec.setMetadata(lst);

    component.setSpec(spec);
    ObjectMeta om = new ObjectMeta();
    om.setName("messagebus");
    component.setMetadata(om);

    return component;
  }

  @Test
  void testAgainstSample() throws Exception {
    // Arrange
    Path resPath = Paths.get(getClass().getResource("/sample.yaml").toURI());
    String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
    Component sample = createSampleComponent();
    KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));

    // Act
    List<JsonNode> aggregatedDiffs = diff.getListOfDiffs();

    // Assert
    assertEquals(0, aggregatedDiffs.size());
  }
}

@@ -687,113 +687,8 @@
 de;%0A
-import com.fasterxml.jackson.databind.node.IntNode;%0Aimport com.fasterxml.jackson.databind.node.TextNode;%0A
 impo
@@ -718,16 +718,16 @@
 ponent;%0A
+
 import i
@@ -801,24 +801,72 @@
 c.Metadata;%0A
+import io.fabric8.kubernetes.api.model.AnyType;%0A
 import io.fa
@@ -1169,24 +1169,72 @@
 .ArrayList;%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import java.
@@ -1964,23 +1964,22 @@
 lue(new 
-TextNod
+AnyTyp
 e(%22%7Buuid
@@ -2075,15 +2075,14 @@
 new 
-TextNod
+AnyTyp
 e(%22t
@@ -2209,14 +2209,14 @@
 new 
-IntNod
+AnyTyp
 e(1)
@@ -2309,53 +2309,38 @@
 lue(
-Serialization.jsonMapper().readTree(%22%5B
+new AnyType(new int%5B%5D%7B
 1,
+ 
 2,
-3%5D%22
+ 3%7D
 ));%0A
@@ -2416,87 +2416,129 @@
     
-meta5.setValue(Serialization.jsonMapper().readTree(%22%7B %5C%22ONE%5C%22: 1, %5C%22TWO%5C%22: 2 %7D%22
+Map%3CString, Object%3E obj = new HashMap();%0A    obj.put(%22ONE%22, 1);%0A    obj.put(%22TWO%22, 2);%0A    meta5.setValue(new AnyType(obj
 ));%0A

fix the integration test