view plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapterTest.java @ 2733:4a55900090e4

Compare JSON strings as JSON, not strings This patch adds a assertJsonEquals() method to common-test (bringing in Gson at test time), and modifies many of the JUnit tests to use it instead of string compare. Doing a proper JSON compare means whitespace and member order no longer needs to be exact. Member order doesn't matter in JSON objects; only arrays. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024424.html
author Simon Tooke <stooke@redhat.com>
date Wed, 09 Aug 2017 13:26:10 -0400
parents cc297a1bf67f
children 2397ba60a7d6
line wrap: on
line source

/*
 * Copyright 2012-2017 Red Hat, Inc.
 *
 * This file is part of Thermostat.
 *
 * Thermostat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * Thermostat is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Thermostat; see the file COPYING.  If not see
 * <http://www.gnu.org/licenses/>.
 *
 * Linking this code with other modules is making a combined work
 * based on this code.  Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this code give
 * you permission to link this code with independent modules to
 * produce an executable, regardless of the license terms of these
 * independent modules, and to copy and distribute the resulting
 * executable under terms of your choice, provided that you also
 * meet, for each linked independent module, the terms and conditions
 * of the license of that module.  An independent module is a module
 * which is not derived from or based on this code.  If you modify
 * this code, you may extend this exception to your version of the
 * library, but you are not obligated to do so.  If you do not wish
 * to do so, delete this exception statement from your version.
 */

package com.redhat.thermostat.jvm.overview.agent.internal.model;

import static com.redhat.thermostat.testutils.JsonUtils.assertJsonEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.VmInfoUpdate;
import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoTypeAdapter.VmInfoUpdateTypeAdapter;
import com.redhat.thermostat.jvm.overview.agent.model.VmInfo;
import org.junit.Test;

public class VmInfoTypeAdapterTest {
    
    @Test
    public void testWrite() throws Exception {
        VmInfoTypeAdapter adapter = new VmInfoTypeAdapter();
        final String expected = "[{\"agentId\":\"agent1\",\"jvmId\":\"vm1\",\"jvmPid\":8000," +
                "\"startTime\":{\"$numberLong\":\"50000\"},\"stopTime\":{\"$numberLong\":\"-9223372036854775808\"}," +
                "\"javaVersion\":\"1.8.0\",\"javaHome\":\"/path/to/java\",\"mainClass\":\"myClass\"," +
                "\"javaCommandLine\":\"java myClass\",\"jvmName\":\"myJVM\",\"jvmArguments\":\"-Dhello\",\"" +
                "jvmInfo\":\"interesting\",\"jvmVersion\":\"1800\"," +
                "\"properties\":[{\"key\":\"A\",\"value\":\"B\"},{\"key\":\"C\",\"value\":\"D\"}]," +
                "\"environment\":[{\"key\":\"E\",\"value\":\"F\"},{\"key\":\"G\",\"value\":\"H\"}]," +
                "\"loadedNativeLibraries\":[],\"uid\":{\"$numberLong\":\"1234\"},\"username\":\"test\"}," +
                "{\"agentId\":\"agent2\",\"jvmId\":\"vm2\",\"jvmPid\":9000,\"startTime\":{\"$numberLong\":\"100000\"}," +
                "\"stopTime\":{\"$numberLong\":\"200000\"},\"javaVersion\":\"1.7.0\",\"javaHome\":\"/path/to/jre\"," +
                "\"mainClass\":\"myOtherClass\",\"javaCommandLine\":\"otherClass.sh\",\"jvmName\":\"myOtherJVM\"," +
                "\"jvmArguments\":\"-Dworld\",\"jvmInfo\":\"info\",\"jvmVersion\":\"1700\",\"properties\":[]," +
                "\"environment\":[{\"key\":\"A\",\"value\":\"B\"},{\"key\":\"C\",\"value\":\"D\"}]," +
                "\"loadedNativeLibraries\":[\"libhello\",\"libworld\"],\"uid\":{\"$numberLong\":\"5678\"}," +
                "\"username\":\"user\"}]";
        
        final Map<String, String> props = new HashMap<>();
        props.put("A", "B");
        props.put("C", "D");
        final Map<String, String> env = new HashMap<>();
        env.put("E", "F");
        env.put("G", "H");
        VmInfo first = new VmInfo("agent1", "vm1", 8000, 50000L, Long.MIN_VALUE, "1.8.0", "/path/to/java", "myClass", 
                "java myClass", "myJVM", "interesting", "1800", "-Dhello", props, env, new String[0], 1234L, "test");
        
        final Map<String, String> props2 = new HashMap<>();
        final Map<String, String> env2 = new HashMap<>();
        env2.put("A", "B");
        env2.put("C", "D");
        final String[] libs = { "libhello", "libworld" };
        VmInfo second = new VmInfo("agent2", "vm2", 9000, 100000L, 200000L, "1.7.0", "/path/to/jre", "myOtherClass",
                                   "otherClass.sh", "myOtherJVM", "info", "1700", "-Dworld", props2, env2, libs, 5678L, "user");
        List<VmInfo> infos = Arrays.asList(first, second);
        
        String json = adapter.toJson(infos);
        assertJsonEquals(expected, json);
    }
    
    @Test
    public void testUpdate() throws Exception {
        VmInfoUpdateTypeAdapter adapter = new VmInfoUpdateTypeAdapter();
        final String expected = "{\"set\":{\"stopTime\":{\"$numberLong\":\"5000\"}}}";
        
        VmInfoUpdate update = new VmInfoUpdate(5000L);
        String json = adapter.toJson(update);
        assertJsonEquals(expected, json);
    }
    
}