no.dusken.barweb.plugin.liquidityplugin.control.admin.TestAdminController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.barweb.plugin.liquidityplugin.control.admin.TestAdminController.java

Source

/*
 Copyright 2009 - 2010 Under Dusken
    
 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 no.dusken.barweb.plugin.liquidityplugin.control.admin;

import no.dusken.barweb.model.Gjeng;
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.kantega.jexmec.store.PluginStore;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.*;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
@RunWith(MockitoJUnitRunner.class)
public class TestAdminController {

    @Mock
    private PluginStore pluginStoreProvider;

    @Mock
    private BarPersonService barPersonService;

    @Mock
    private GjengService gjengService;

    private StatusController controller;

    @Before
    public void onsetup() {
        controller = new StatusController();
        controller.setBarPersonService(barPersonService);
        controller.setPluginStore(pluginStoreProvider);
        controller.setGjengService(gjengService);
        when(gjengService.getByDefaultGjengTrue()).thenReturn(new Gjeng());
    }

    @Test
    public void testStatus() {
        when(pluginStoreProvider.getString("valuesinbar", "0")).thenReturn("100");
        when(pluginStoreProvider.getString("valuesinbank", "0")).thenReturn("100");
        when(barPersonService.getSumPersonBalances((Gjeng) anyObject())).thenReturn(100l);

        Model model = new ExtendedModelMap();
        String view = controller.status(model);
        assertEquals("Wrong view", "no/dusken/barweb/plugin/liquidityplugin/admin/status", view);

        // bankbalance - summed balance
        assertEquals("wrong value", model.asMap().get("sum"), 100l);
        assertEquals("wrong value", model.asMap().get("valuesinbar"), 100);
        assertEquals("wrong value", model.asMap().get("valuesinbank"), 100);
        assertEquals("wrong value", model.asMap().get("sumPersonBalance"), 100l);
    }

    @Test
    public void voidTestUpdate() {
        when(pluginStoreProvider.getString("valuesinbar", "0")).thenReturn("50");
        when(pluginStoreProvider.getString("valuesinbank", "0")).thenReturn("50");
        when(barPersonService.getSumPersonBalances((Gjeng) anyObject())).thenReturn(100l);

        Model model = new ExtendedModelMap();
        String view = controller.update(50 + "", 50 + "", model);
        assertEquals("Wrong view", "no/dusken/barweb/plugin/liquidityplugin/admin/status", view);

        verify(pluginStoreProvider, times(1)).setString("valuesinbar", "50");
        verify(pluginStoreProvider, times(1)).setString("valuesinbank", "50");

        // bankbalance - summed balance
        assertEquals("wrong value", model.asMap().get("sum"), 0l);
        assertEquals("wrong value", model.asMap().get("valuesinbar"), 50);
        assertEquals("wrong value", model.asMap().get("valuesinbank"), 50);
        assertEquals("wrong value", model.asMap().get("sumPersonBalance"), 100l);
    }
}