view tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/schema/SchemaIntegrationTest.java @ 292:1738c834713c

Add schema service to serve combined API swagger file This patch serves an uber-api Swagger file from URL https://127.0.0.1:30000/schema/fullapi-swagger.yaml (or ... .json) Rviewed-by: sgehwolf Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025369.html
author Simon Tooke <stooke@redhat.com>
date Tue, 24 Oct 2017 14:46:18 -0400
parents
children 8ecf97835acd
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.gateway.service.schema;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.redhat.thermostat.common.json.JsonUtil;
import com.redhat.thermostat.common.yaml.YamlToJson;
import com.redhat.thermostat.gateway.tests.integration.ServiceIntegrationTest;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.client.api.ContentResponse;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

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

public class SchemaIntegrationTest extends ServiceIntegrationTest {

    private static final String serviceName = "schema";
    private static final String versionString = "";
    private static final int HTTP_200_OK = 200;
    private static final int HTTP_404_NOTFOUND = 404;

    public SchemaIntegrationTest() {
        super(serviceName);
    }

    @Test
    public void testGoodYaml() throws InterruptedException, ExecutionException, TimeoutException, IOException {
        final ContentResponse resp = get("/fullapi-swagger.yaml");
        final YamlToJson y2j = new YamlToJson();
        final JsonObject json = y2j.yamlToJsonObject(resp.getContentAsString());
        verifyApi(json);
    }

    @Test
    public void testGoodJson() throws InterruptedException, ExecutionException, TimeoutException {
        final ContentResponse resp = get("/fullapi-swagger.json");
        final JsonObject json = new JsonParser().parse(resp.getContentAsString()).getAsJsonObject();
        verifyApi(json);
    }

    private void verifyApi(JsonObject obj) {
        final String title = JsonUtil.fetch(obj, "info.title").getAsString();
        assertNotNull(title);
        assertTrue(title.contains("Thermostat" ));
        final JsonObject paths = obj.get("paths").getAsJsonObject();
        assertNotNull(paths);
        assertFalse(paths.entrySet().isEmpty());
    }

    @Test
    public void testBad() throws InterruptedException, ExecutionException, TimeoutException {
        final ContentResponse resp = getBad("/fullapi-swagge.yaml");
    }

    protected ContentResponse get(final String path) throws InterruptedException, ExecutionException, TimeoutException {
        ContentResponse response = client.newRequest(resourceUrl + path).method(HttpMethod.GET).send();
        assertEquals(HTTP_200_OK, response.getStatus());
        return response;
    }

    protected ContentResponse getBad(final String path) throws InterruptedException, ExecutionException, TimeoutException {
        ContentResponse response = client.newRequest(resourceUrl + path).method(HttpMethod.GET).send();
        assertEquals(HTTP_404_NOTFOUND, response.getStatus());
        return response;
    }

    @Override
    public String getServiceVersion() {
        return versionString;
    }

    @Override
    public String getServiceName() {
        return serviceName;
    }

    @Override
    protected String getDocUrl() {
        return baseUrl + "/" + getServiceName() + "/doc/" + getServiceName() + "-swagger.yaml";
    }
}