package com.admicro.vertx.utils;

import io.vertx.core.json.JsonObject;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class XmlConverterTest {

    @Test
    public void test_toJson() {
        StringBuilder builder = new StringBuilder();
        builder.append("<server>\n");
        builder.append("<server_options>\n" +
                "<address>0.0.0.0</address>\n" +
                "<port>8888</port>\n" +
                "</server_options>");
        builder.append("<database>\n" +
                "<driver_class>com.mysql.jdbc.Driver</driver_class>\n" +
                "<url>jdbc:mysql://localhost/server_load</url>\n" +
                "<user>root</user>\n" +
                "<password>root</password>\n" +
                "<max_pool_size>30</max_pool_size>\n" +
                "</database>\n");
        builder.append("<deployment_options></deployment_options>\n");
        builder.append("<vertx_options></vertx_options>\n");
        builder.append("</server>");

        JsonObject server = null;
        try {
            server = XmlConverter.toJson(builder.toString(), "server").getJsonObject("server");
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }

        JsonObject serverOptions = server.getJsonObject("server_options");
        JsonObject database = server.getJsonObject("database");
        JsonObject deploymentOptions = server.getJsonObject("deployment_options");
        JsonObject vertxOptions = server.getJsonObject("vertx_options");

        assertTrue(serverOptions != null);
        assertTrue(database != null);
        assertTrue(deploymentOptions != null);
        assertTrue(vertxOptions != null);

        assertEquals(serverOptions.getString("address"), "0.0.0.0");
        assertEquals(serverOptions.getInteger("port"), (Integer) 8888);

        assertEquals(database.getString("driver_class"), "com.mysql.jdbc.Driver");
        assertEquals(database.getString("url"), "jdbc:mysql://localhost/server_load");
        assertEquals(database.getString("user"), "root");
        assertEquals(database.getString("password"), "root");
        assertEquals(database.getInteger("max_pool_size"), (Integer) 30);
    }
}

@@ -1075,42 +1075,8 @@
 );%0A%0A
-        JsonObject server = null;%0A
     
@@ -1097,16 +1097,27 @@
         
+JsonObject 
 server =
@@ -1196,92 +1196,13 @@
 %22);%0A
+%0A
     
-    %7D catch (Exception e) %7B%0A            Assert.fail(e.getMessage());%0A        %7D%0A%0A
     
@@ -1272,32 +1272,36 @@
 ions%22);%0A        
+    
 JsonObject datab
@@ -1336,24 +1336,28 @@
 database%22);%0A
+    
         Json
@@ -1427,32 +1427,36 @@
 ions%22);%0A        
+    
 JsonObject vertx
@@ -1505,32 +1505,36 @@
 ons%22);%0A%0A        
+    
 assertTrue(serve
@@ -1552,32 +1552,36 @@
  null);%0A        
+ 
+   
 assertTrue(datab
@@ -1586,32 +1586,36 @@
 abase != null);%0A
+    
         assertTr
@@ -1645,32 +1645,36 @@
  null);%0A        
+ 
+   
 assertTrue(vertx
@@ -1688,24 +1688,28 @@
  != null);%0A%0A
+    
         asse
@@ -1765,32 +1765,36 @@
 .0.0%22);%0A        
+ 
+   
 assertEquals(ser
@@ -1841,13 +1841,16 @@
  888
-8
 );%0A%0A
+    
     
@@ -1928,32 +1928,36 @@
 iver%22);%0A        
+    
 assertEquals(dat
@@ -2019,32 +2019,36 @@
 load%22);%0A        
+    
 assertEquals(dat
@@ -2081,32 +2081,36 @@
 root%22);%0A        
+ 
+   
 assertEquals(dat
@@ -2139,32 +2139,36 @@
 ord%22), %22root%22);%0A
+    
         assertEq
@@ -2225,16 +2225,99 @@
 r) 30);%0A
+        %7D catch (Exception e) %7B%0A            Assert.fail(e.getMessage());%0A        %7D%0A
     %7D%0A%7D%0A

Update unit test