cz.muni.fi.dndtroops.facade.TroopFacadeImplTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.dndtroops.facade.TroopFacadeImplTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.dndtroops.facade;

import cz.muni.fi.dndtroops.enums.RoleName;
import cz.muni.fi.dndtroops.service.ServiceConfiguration;
import cz.muni.fi.dndtroopsapi.dto.HeroCreateDTO;
import cz.muni.fi.dndtroopsapi.dto.TroopCreateDTO;
import cz.muni.fi.dndtroopsapi.dto.TroopDTO;
import cz.muni.fi.dndtroopsapi.facade.HeroFacade;
import cz.muni.fi.dndtroopsapi.facade.TroopFacade;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import javax.inject.Inject;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 *
 * @author Karel Auf
 */
@ContextConfiguration(classes = ServiceConfiguration.class)
public class TroopFacadeImplTest extends AbstractTestNGSpringContextTests {

    @Inject
    HeroFacade heroFacade;

    @Inject
    TroopFacade troopFacade;

    private TroopCreateDTO troop1;
    private HeroCreateDTO heroA;

    @BeforeMethod
    public void prepareHeroes() {
        troop1 = new TroopCreateDTO();
        troop1.setName("Troop A");
        troop1.setMission("mission");
        troop1.setMoney(new BigDecimal("11"));

        List<TroopDTO> list = troopFacade.getAllTroops();
        for (TroopDTO t : list) {
            troopFacade.deleteTroop(t.getId());
        }

        heroA = new HeroCreateDTO();
        heroA.setName("Hero A");
        heroA.setLevel(1);
        heroA.setRole(RoleName.BARD);

    }
    /* //uncoment here to be able to run tests
           
     @Test
     @Rollback(true)
     public void createTest(){
        
     troopFacade.createTroop(troop1);
     Assert.assertEquals(troopFacade.getAllTroops().size(), 1);
     troopFacade.deleteTroop(troopFacade.getTroopWithName("Troop A").getId());
     }
        
     @Test
     @Rollback(true)
     public void changeTest(){
        
     troopFacade.createTroop(troop1);
     Assert.assertEquals(troopFacade.getAllTroops().size(), 1);
     troopFacade.changeMission(troopFacade.getTroopWithName("Troop A").getId(), "new mission");
     Assert.assertEquals(troopFacade.getTroopWithName("Troop A").getMission(), "new mission");
        
     troopFacade.changeMoney(troopFacade.getTroopWithName("Troop A").getId(), new BigDecimal("1"));
     Assert.assertEquals(troopFacade.getTroopWithName("Troop A").getMoney().compareTo(new BigDecimal("1")), 0);
     troopFacade.deleteTroop(troopFacade.getTroopWithName("Troop A").getId());
        
     }
        
     @Test
     @Rollback(true)
     public void heroTest(){
        
     troopFacade.createTroop(troop1);
     heroFacade.createHero(heroA);
        
     troopFacade.addHero(troopFacade.getTroopWithName("Troop A").getId(), heroFacade.getHeroWithName("Hero A").getId());
     Assert.assertEquals(heroFacade.getHeroWithName("Hero A").getTroop(), troopFacade.getTroopWithName("Troop A"));
     Assert.assertEquals(troopFacade.getTroopWithName("Troop A").getMembers().size(), 1);
     Assert.assertTrue(troopFacade.getTroopWithName("Troop A").getMembers().contains(heroFacade.getHeroWithName("Hero A")));
        
     troopFacade.removeHero(troopFacade.getTroopWithName("Troop A").getId(), heroFacade.getHeroWithName("Hero A").getId());
     Assert.assertEquals(heroFacade.getHeroWithName("Hero A").getTroop(), null);
     Assert.assertEquals(troopFacade.getTroopWithName("Troop A").getMembers().size(), 0);
        
     troopFacade.deleteTroop(troopFacade.getTroopWithName("Troop A").getId());
     heroFacade.deleteHero(heroFacade.getHeroWithName("Hero A").getId());
     }
        
     @Test
     @Rollback(true)
     public void deleteTest(){
     troopFacade.createTroop(troop1);
     Assert.assertEquals(troopFacade.getAllTroops().size(), 1);
     troopFacade.deleteTroop(troopFacade.getTroopWithName("Troop A").getId());
     Assert.assertEquals(troopFacade.getAllTroops().size(), 0);
     }
     /*
     */
}