org.springside.examples.bootservice.functional.TaskRestServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.springside.examples.bootservice.functional.TaskRestServiceTest.java

Source

/*
 * Copyright 2012-2014 the original author or authors.
 *
 * 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 org.springside.examples.bootservice.functional;

import static org.assertj.core.api.Assertions.*;

import java.net.URI;
import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import org.springside.examples.bootservice.BootServiceApplication;
import org.springside.examples.bootservice.domain.Task;
import org.springside.examples.bootservice.domain.User;
import org.springside.modules.test.data.RandomData;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BootServiceApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port=0")
@DirtiesContext
public class TaskRestServiceTest {

    @Value("${local.server.port}")
    private int port;

    private RestTemplate restTemplate;
    private String resourceUrl;

    @Before
    public void setup() {
        restTemplate = new TestRestTemplate();
        resourceUrl = "http://localhost:" + port + "/task";
    }

    @Test
    public void listTask() {
        TaskList tasks = restTemplate.getForObject(resourceUrl, TaskList.class);
        assertThat(tasks).hasSize(5);
        Task firstTask = tasks.get(0);

        assertThat(firstTask.title).isEqualTo("Spring Boot");
        assertThat(firstTask.user.name).isEqualTo("Calvin");
    }

    @Test
    public void getTask() {
        Task task = restTemplate.getForObject(resourceUrl + "/{id}", Task.class, 1L);
        assertThat(task.title).isEqualTo("Spring Boot");
        assertThat(task.user.name).isEqualTo("Calvin");
    }

    @Test
    public void createUpdateAndDeleteTask() {

        // create
        Task task = randomTask();

        URI createdTaskUri = restTemplate.postForLocation(resourceUrl, task);
        System.out.println(createdTaskUri.toString());
        Task createdTask = restTemplate.getForObject(createdTaskUri, Task.class);
        assertThat(createdTask.title).isEqualTo(task.title);

        // update
        String id = StringUtils.substringAfterLast(createdTaskUri.toString(), "/");
        task.id = new Long(id);
        task.title = RandomData.randomName("Task");

        restTemplate.put(createdTaskUri, task);

        Task updatedTask = restTemplate.getForObject(createdTaskUri, Task.class);
        assertThat(updatedTask.title).isEqualTo(task.title);

        // delete
        restTemplate.delete(createdTaskUri);
        Task deletedTask = restTemplate.getForObject(createdTaskUri, Task.class);
        assertThat(deletedTask).isNull();
    }

    public static Task randomTask() {
        Task task = new Task();
        task.title = RandomData.randomName("Task");
        User user = new User(1L);
        task.user = user;
        return task;
    }

    // ArrayList<Task>RestTemplate????
    private static class TaskList extends ArrayList<Task> {
    }

}