org.bozzo.ipplan.web.assembler.AddressPlanResourceAssemblerTest.java Source code

Java tutorial

Introduction

Here is the source code for org.bozzo.ipplan.web.assembler.AddressPlanResourceAssemblerTest.java

Source

/*
 * Copyright (C) 2015
 *     Boris Barnier <b.barnier@gmail.com>
 *   
 * This file is part of ipplan-api.
 * 
 * ipplan-api 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 3 of the License, or
 * (at your option) any later version.
 * 
 * ipplan-api 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 ipplan-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.bozzo.ipplan.web.assembler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.bozzo.ipplan.IpplanApiApplication;
import org.bozzo.ipplan.domain.model.AddressPlan;
import org.bozzo.ipplan.domain.model.ui.AddressPlanResource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * @author boris
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = IpplanApiApplication.class)
@WebAppConfiguration
public class AddressPlanResourceAssemblerTest {

    @Autowired
    private AddressPlanResourceAssembler assembler;

    @Test
    public void toResources_with_empty_array_should_return_an_empty_array() {
        List<AddressPlanResource> resources = this.assembler.toResources(new ArrayList<>());
        Assert.assertNotNull(resources);
        Assert.assertEquals(0, resources.size());
    }

    @Test
    public void toResources_with_singleton_array_should_return_a_singleton_array() {
        AddressPlan plan = new AddressPlan();
        plan.setId(1L);
        plan.setDescription("Test description");
        plan.setInfraId(1);
        plan.setLastModifed(new Date());
        plan.setName("test");
        List<AddressPlanResource> resources = this.assembler.toResources(Collections.singletonList(plan));
        Assert.assertNotNull(resources);
        Assert.assertEquals(1, resources.size());
        Assert.assertTrue(ResourceSupport.class.isAssignableFrom(resources.get(0).getClass()));
    }

    @Test
    public void toResources_with_array_should_return_a_array() {
        List<AddressPlan> list = new ArrayList<>();
        AddressPlan plan = new AddressPlan();
        plan.setId(1L);
        plan.setDescription("Test description");
        plan.setInfraId(1);
        plan.setLastModifed(new Date());
        plan.setName("test");
        list.add(plan);
        list.add(plan);
        list.add(plan);
        List<AddressPlanResource> resources = this.assembler.toResources(list);
        Assert.assertNotNull(resources);
        Assert.assertEquals(list.size(), resources.size());
        Assert.assertTrue(ResourceSupport.class.isAssignableFrom(resources.get(0).getClass()));
    }
}