package no.dusken.barweb.plugin.statisticsplugin.control; import no.dusken.barweb.model.BarPerson; import no.dusken.barweb.model.Gjeng; import no.dusken.barweb.plugin.statisticsplugin.model.StatisticsElement; import no.dusken.barweb.plugin.statisticsplugin.service.StatisticsService; import no.dusken.barweb.service.BarPersonService; import no.dusken.barweb.service.GjengService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; import java.util.Calendar; import java.util.LinkedList; import java.util.List; import static junit.framework.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ @RunWith(MockitoJUnitRunner.class) public class TestStatisticsController { @Mock private StatisticsService statisticsService; @Mock private GjengService gjengService; @Mock private BarPersonService barPersonService; private StatisticsController controller; @Before public void onSetup(){ controller = new StatisticsController(); controller.setStatisticsService(statisticsService); controller.setGjengService(gjengService); controller.setBarPersonService(barPersonService); Gjeng g = new Gjeng(){ @Override public Long getId() { return 3L; } }; when(gjengService.getByDefaultGjengTrue()).thenReturn(g); when(gjengService.findOne(3L)).thenReturn(g); } @Test public void testShowPersonBalance(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); BarPerson barPerson = new BarPerson(); when(barPersonService.findOne(1L)).thenReturn(barPerson); when(statisticsService.getByTrackedObjectIdAndNameAndTimeCreatedBetween(eq(1L), eq(StatisticsConstants.PERSON_BALANCE), any(Calendar.class), any(Calendar.class))).thenReturn(elements); controller.showPersonBalance(1L, model); assertTrue("Should contain barPerson", model.asMap().containsKey("entity")); assertTrue("Should contain barPerson", model.asMap().get("entity").equals(barPerson)); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("saldo")); } @Test public void testShowPersonSpentMoney(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); BarPerson barPerson = new BarPerson(); when(barPersonService.findOne(1L)).thenReturn(barPerson); when(statisticsService.getByTrackedObjectIdAndName(eq(1L), eq(StatisticsConstants.PERSON_MONEY_SPENT_LAST_WEEK) )).thenReturn(elements); controller.showPersonSpentMoney(1L, model); assertTrue("Should contain barPerson", model.asMap().containsKey("entity")); assertTrue("Should contain barPerson", model.asMap().get("entity").equals(barPerson)); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("penger brukt")); } @Test public void testShowPersonBeers(){ List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); BarPerson barPerson = new BarPerson(); when(barPersonService.findOne(1L)).thenReturn(barPerson); when(statisticsService.getByTrackedObjectIdAndName(eq(1L), eq(StatisticsConstants.PERSON_BEER_LAST_WEEK))).thenReturn(elements); Model model = new ExtendedModelMap(); controller.showPersonBeers(1L, model); assertTrue("Should contain barPerson", model.asMap().containsKey("entity")); assertTrue("Should contain barPerson", model.asMap().get("entity").equals(barPerson)); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("øl")); } @Test public void testShowTotalBeers(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); when(statisticsService.getByTrackedObjectIdAndName(eq(3L), eq(StatisticsConstants.GJENG_BEER_LAST_WEEK))).thenReturn(elements); controller.showTotalBeers(model); assertTrue("Should contain gjeng", model.asMap().containsKey("entity")); assertTrue("Should contain gjeng", model.asMap().get("entity").equals(gjengService.getByDefaultGjengTrue())); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("øl, gjeng")); } @Test public void testShowTotalMoneySpent(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); when(statisticsService.getByTrackedObjectIdAndName(eq(3L), eq(StatisticsConstants.GJENG_MONEY_SPENT_LAST_WEEK))).thenReturn(elements); controller.showTotalMoneySpent(model); assertTrue("Should contain gjeng", model.asMap().containsKey("entity")); assertTrue("Should contain gjeng", model.asMap().get("entity").equals(gjengService.getByDefaultGjengTrue())); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("penger brukt, gjeng")); } @Test public void testShowSumPersonBalance(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); when(statisticsService.getByTrackedObjectIdAndName(eq(3L), eq(StatisticsConstants.SUM_PERSON_BALANCE))).thenReturn(elements); controller.showSumPersonBalance(model); assertTrue("Should contain gjeng", model.asMap().containsKey("entity")); assertTrue("Should contain gjeng", model.asMap().get("entity").equals(gjengService.getByDefaultGjengTrue())); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("sum person-saldoer")); } @Test public void testShowValueInBar(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); when(statisticsService.getByTrackedObjectIdAndName(eq(3L), eq(StatisticsConstants.GJENG_VALUE_BAR))).thenReturn(elements); controller.showValueInBar(model); assertTrue("Should contain gjeng", model.asMap().containsKey("entity")); assertTrue("Should contain gjeng", model.asMap().get("entity").equals(gjengService.getByDefaultGjengTrue())); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("verdi i baren")); } @Test public void testShowLiquidity(){ Model model = new ExtendedModelMap(); List<StatisticsElement> elements = new LinkedList<StatisticsElement>(); when(statisticsService.getByTrackedObjectIdAndName(eq(3L), eq(StatisticsConstants.GJENG_LIQUIDITY))).thenReturn(elements); controller.showLiquidity(model); assertTrue("Should contain gjeng", model.asMap().containsKey("entity")); assertTrue("Should contain gjeng", model.asMap().get("entity").equals(gjengService.getByDefaultGjengTrue())); assertTrue("Should contain elements", model.asMap().containsKey("elements")); assertTrue("Should contain elements", model.asMap().get("elements").equals(elements)); assertTrue("Should contain correct name", model.asMap().containsKey("statisticsName")); assertTrue("Should contain correct name", model.asMap().get("statisticsName").equals("barens likviditet")); } }