com.stackoverflow.so32806530.App.java Source code

Java tutorial

Introduction

Here is the source code for com.stackoverflow.so32806530.App.java

Source

/*
 * Copyright 2015 Rgis Caspar <regis.caspar@gmail.com>
 *
 * 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 com.stackoverflow.so32806530;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.slf4j.Logger;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
 * @author regis
 */
public class App {

    private static final Logger log = org.slf4j.LoggerFactory.getLogger(App.class);

    /**
     * Program EP.
     * @param args CLI args
     *
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void main(final String[] args) throws URISyntaxException, IOException {
        // ---------- Read response.xml -----
        final String xml = new String(
                Files.readAllBytes(Paths.get(App.class.getClassLoader().getResource("response.xml").toURI())),
                Charset.forName("UTF-8"));

        // ---------- starts fake API server -----
        final WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(8089));
        wireMockServer.stubFor(get(urlMatching("/v2/discovery/events.*")).willReturn(
                aResponse().withHeader("Content-type", "application/xml").withStatus(200).withBody(xml)));
        wireMockServer.start();
        // ---------------------------------------

        try {
            final String name = "foo";
            final String APIKEY = "MYAPI";
            final String URL = "http://localhost:8089/v2/discovery/events?apikey=" + APIKEY;
            final String readyUrl = URL + "&what=" + name;
            final RestTemplate restTemplate = new RestTemplate();
            final EventsResponse eventResponse = restTemplate.getForObject(readyUrl, EventsResponse.class);

            log.info("Seatwave: {}", eventResponse.getEvents().size());

            for (final Event event : eventResponse.getEvents()) {
                log.info("EventID: {}", event.getId());
            }
        } catch (final Exception ex) {
            log.error("Something went wrong", ex);
        }

        // ---------- stops fake API server ------
        wireMockServer.stop();
        // ---------------------------------------
    }
}