view common/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java @ 25:dd0992bd51aa

Add Maven Central upload, refactor lang-schema and json libs to thermostat-common library - merge common-json and common-lang-schema packages into thermostat- common. - add targets to generate Javadoc and source jars, and modify sources to remove some Javadoc warnings. - add targets to sign and deploy thermostat-common to Maven Central. Reviewed-by: sgehwolf, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025214.html
author Simon Tooke <stooke@redhat.com>
date Fri, 29 Sep 2017 14:48:00 -0400
parents common/type-system/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java@5100a9b678f4
children d0304caad13a
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.lang.schema.internal;

import com.redhat.thermostat.lang.schema.annotations.Schema;
import com.redhat.thermostat.lang.schema.annotations.Type;
import com.redhat.thermostat.lang.schema.internal.JSONServiceImpl;
import com.redhat.thermostat.lang.schema.models.Timestamp;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 */
public class JSONServiceTest {

    @Type(description = "Some Object for testing out")
    public class SomeMoreComplexType {
        @Schema(description = "Some Long", required = true)
        private long aLong;

        @Schema(description = "Some String", required = true)
        private String aString;

        public SomeMoreComplexType(long aLong, String aString) {
            this.aLong = aLong;
            this.aString = aString;

        }

        public long getALong() {
            return aLong;
        }

        public String getAString() {
            return aString;
        }
    }

    @Type(description = "Some Object for testing out")
    public class SomeMoreComplexType2 {
        @Schema(description = "Some Long", required = true)
        private long aLong;

        // not part of the schema, won't be serialised!
        private String aString;

        public SomeMoreComplexType2(long aLong, String aString) {
            this.aLong = aLong;
            this.aString = aString;

        }

        public long getALong() {
            return aLong;
        }

        public String getAString() {
            return aString;
        }
    }

    @Type(description = "Something more complex perhaps?")
    public class CompoundType {
        @Schema(description = "Some Long", required = true)
        private long aLong;

        @Schema
        private SomeMoreComplexType complexType;

        public CompoundType(long aLong, SomeMoreComplexType complexType) {
            this.aLong = aLong;
            this.complexType = complexType;

        }

        public long getALong() {
            return aLong;
        }

        public SomeMoreComplexType getComplexType() {
            return complexType;
        }
    }

    @Type(description = "Something more complex perhaps?")
    public class CompoundTypeMissingSchema {
        @Schema(description = "Some Long", required = true)
        private long aLong;

        // won't be serialised
        private SomeMoreComplexType complexType;

        public CompoundTypeMissingSchema(long aLong, SomeMoreComplexType complexType) {
            this.aLong = aLong;
            this.complexType = complexType;

        }

        public long getALong() {
            return aLong;
        }

        public SomeMoreComplexType getComplexType() {
            return complexType;
        }
    }

    @Type(description = "An extended type that should inherit its schema")
    public class ExtendedType extends CompoundType {

        @Schema(description = "Some other long from derived class", required = true)
        private long anotherLong;

        public ExtendedType(long aLong, long anotherLong, SomeMoreComplexType complexType) {
            super(aLong, complexType);
            this.anotherLong = anotherLong;
        }
    }

    private JSONServiceImpl service;

    @Before
    public void setUp() {
        service = new JSONServiceImpl(false);
    }

    @Test
    public void testSimpleType() {
        Timestamp timestamp = new Timestamp(123456L);
        assertEquals("{\"timestamp\":\"123456\"}", service.serialiase(timestamp));
    }

    @Test
    public void testComplexType() {
        SomeMoreComplexType type = new SomeMoreComplexType(42, "test");
        assertEquals("{\"aLong\":\"42\",\"aString\":\"test\"}",
                service.serialiase(type));
    }

    @Test
    public void testComplexType2() {
        SomeMoreComplexType2 type = new SomeMoreComplexType2(42, "test");
        assertEquals("{\"aLong\":\"42\"}",
                service.serialiase(type));
    }

    @Test
    public void testCompoundType() {
        CompoundType type = new CompoundType(42, new SomeMoreComplexType(10, "test"));
        assertEquals("{\"aLong\":\"42\",\"complexType\":{\"aLong\":\"10\",\"aString\":\"test\"}}",
                service.serialiase(type));
    }

    @Test
    public void testCompoundTypeMissingSchema() {
        CompoundTypeMissingSchema type = new CompoundTypeMissingSchema(42, new SomeMoreComplexType(10, "test"));
        assertEquals("{\"aLong\":\"42\"}",
                service.serialiase(type));
    }

    @Test
    public void testExtendedType() {
        ExtendedType type = new ExtendedType(50, 42, new SomeMoreComplexType(10, "test"));
        assertEquals("{\"anotherLong\":\"42\",\"aLong\":\"50\",\"complexType\":{\"aLong\":\"10\",\"aString\":\"test\"}}",
                service.serialiase(type));
    }
}