com.raskasa.solrfit.SolrResponseTest.java Source code

Java tutorial

Introduction

Here is the source code for com.raskasa.solrfit.SolrResponseTest.java

Source

/*
 * Copyright (C) 2015 Ras Kasa Williams
 *
 * 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.raskasa.solrfit;

import com.raskasa.solrfit.internal.ExampleSolrApi;
import com.raskasa.solrfit.internal.SimpleApi;
import com.raskasa.solrfit.internal.State;
import com.raskasa.solrfit.internal.User;
import com.raskasa.solrfit.model.SolrResponse;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import okio.Okio;
import org.junit.Test;
import retrofit.RestAdapter;
import retrofit.client.OkClient;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

public final class SolrResponseTest {
    @Test
    public void testSimpleResponse() throws Exception {
        // Create a MockWebServer. These are lean enough that you can create a new
        // instance for every unit test.
        MockWebServer server = new MockWebServer();

        // Schedule some responses.
        server.enqueue(new MockResponse().setBody(loadJsonFile("simple.json")));

        // Start the server.
        server.start();

        // Ask the server for its URL. You'll need this to make HTTP requests.
        URL baseUrl = server.getUrl("/");

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(10, SECONDS);
        client.setReadTimeout(10, SECONDS);
        client.setWriteTimeout(10, SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client))
                .setEndpoint(baseUrl.toString()).build();

        SimpleApi api = restAdapter.create(SimpleApi.class);
        User user = api.getUser();

        assertThat(user.firstName).isEqualTo("Ras Kasa");
        assertThat(user.lastName).isEqualTo("Williams");

        // Shut down the server. Instances cannot be reused.
        server.shutdown();
    }

    @Test
    public void testSolrResponse() throws Exception {
        MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody(loadJsonFile("solr.json")));
        server.start();
        URL baseUrl = server.getUrl("/");

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(10, SECONDS);
        client.setReadTimeout(10, SECONDS);
        client.setWriteTimeout(10, SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client))
                .setEndpoint(baseUrl.toString()).build();

        ExampleSolrApi solr = restAdapter.create(ExampleSolrApi.class);
        SolrQuery query = new SolrQuery.Builder().basic("", "").build();
        SolrResponse<State> response = solr.states(query);
        assertThat(response.internal.results.get(0).name).isEqualTo("New York");
        assertThat(response.internal.results.get(0).code).isEqualTo("NY");
        assertThat(response.internal.results.get(0).country).isEqualTo("United States");
        assertThat(response.internal.results.get(0).countryCode).isEqualTo("US");
        assertThat(response.internal.results.get(1).name).isEqualTo("California");
        assertThat(response.internal.results.get(1).code).isEqualTo("CA");
        assertThat(response.internal.results.get(1).country).isEqualTo("United States");
        assertThat(response.internal.results.get(1).countryCode).isEqualTo("US");
    }

    private String loadJsonFile(String fileName) throws IOException {
        ClassLoader classLoader = getClass().getClassLoader();
        URL url = classLoader.getResource(fileName);
        if (url == null) {
            throw new NullPointerException(fileName + " can't be found.");
        }
        File file = new File(url.getFile());
        return Okio.buffer(Okio.source(file)).readUtf8();
    }
}