no.dusken.common.plugin.control.admin.PluginStoreControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.common.plugin.control.admin.PluginStoreControllerTest.java

Source

/*
 Copyright 2006 - 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.common.plugin.control.admin;

import no.dusken.common.plugin.DuskenPlugin;
import no.dusken.common.plugin.PluginContentMapping;
import org.junit.Before;
import org.junit.Test;
import org.kantega.jexmec.PluginManager;
import org.kantega.jexmec.store.PluginStore;
import org.kantega.jexmec.store.PluginStoreProvider;
import org.kantega.jexmec.store.xml.XmlPluginStoreProvider;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
public class PluginStoreControllerTest {

    private PluginStoreController pluginStoreController;

    private PluginStoreProvider pluginStoreProvider;

    private List<PluginManager<DuskenPlugin>> pluginManagers = new LinkedList<PluginManager<DuskenPlugin>>();

    @Before
    public void setUp() throws Exception {
        File file = getNonExistingTempFile();
        pluginStoreProvider = new XmlPluginStoreProvider(file);
        pluginStoreController = new PluginStoreController();
        pluginStoreController.setPluginManagers(pluginManagers);
        pluginStoreController.setPluginStoreProvider(pluginStoreProvider);

        pluginManagers.add(mock(PluginManager.class));
        pluginManagers.add(mock(PluginManager.class));

        List<DuskenPlugin> plugins = createPlugins();
        when(pluginManagers.get(0).getPlugins()).thenReturn(plugins);

        PluginStore store1 = pluginStoreProvider.getStore(plugins.get(0));
        store1.setString("setting1", "value1");
        store1.setString("setting2", "value2");
        PluginStore store2 = pluginStoreProvider.getStore(plugins.get(1));
        store2.setString("setting1", "value1");
        store2.setString("setting2", "value2");

        List<DuskenPlugin> plugins2 = createPlugins();
        when(pluginManagers.get(0).getPlugins()).thenReturn(plugins);

        PluginStore store21 = pluginStoreProvider.getStore(plugins2.get(0));
        store21.setString("setting1", "value1");
        store21.setString("setting2", "value2");
        PluginStore store22 = pluginStoreProvider.getStore(plugins2.get(1));
        store22.setString("setting1", "value1");
        store22.setString("setting2", "value2");
    }

    @Test
    public void testRequest() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/pluginstore");
        MockHttpServletResponse response = new MockHttpServletResponse();

        ModelAndView mav = pluginStoreController.handleRequestInternal(request, response);
        Map<DuskenPlugin, Map<String, String>> plugins = (Map<DuskenPlugin, Map<String, String>>) mav.getModel()
                .get("plugins");
        assertNotNull("Pluginsmap was null", plugins);
        assertEquals("Pluginsmap contained wrong number of plugins", 2, plugins.size());
        for (DuskenPlugin p : plugins.keySet()) {
            Map<String, String> settings = plugins.get(p);
            assertEquals("Wrong number of settings", 2, settings.size());
        }
        assertNotNull("Map did not contain locale", mav.getModel().get("locale"));
    }

    @Test
    public void testSubmit() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/pluginstore");
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.setParameter("no.dusken.common.plugin.p1-setting1", "newValue1p1");
        request.setParameter("no.dusken.common.plugin.p1-setting2", "newValue2p1");
        request.setParameter("no.dusken.common.plugin.p2-setting1", "newValue1p2");
        request.setParameter("no.dusken.common.plugin.p2-setting2", "newValue2p2");
        ModelAndView mav = pluginStoreController.handleRequestInternal(request, response);

        Map<DuskenPlugin, Map<String, String>> plugins = (Map<DuskenPlugin, Map<String, String>>) mav.getModel()
                .get("plugins");
        assertNotNull("Pluginsmap was null", plugins);
        assertEquals("Pluginsmap contained wrong number of plugins", 2, plugins.size());
        for (DuskenPlugin p : plugins.keySet()) {
            PluginStore store = pluginStoreProvider.getStore(p);
            String setting1 = store.getString("setting1", "default");
            String setting2 = store.getString("setting2", "default");

            assertEquals("Setting1 has wrong value", "newValue1".concat(p.getPluginName(null)), setting1);
            assertEquals("Setting2 has wrong value", "newValue2".concat(p.getPluginName(null)), setting2);
        }

        Map updatemap = (Map) mav.getModel().get("updatemap");
        assertNotNull("Map did not contain updatemap", updatemap);
        assertEquals("Wrong number of updatestrings", 4, updatemap.size());
    }

    private List<DuskenPlugin> createPlugins() {
        DuskenPlugin plugin1 = new DuskenPlugin() {

            public List<HandlerMapping> getHandlerMappings() {
                return null;
            }

            public AuthenticationProvider getAuthenticationProvider() {
                return null;
            }

            @Override
            public AccessDecisionVoter getAccessDecisionVoter() {
                return null;
            }

            @Override
            public PluginContentMapping getContentMapping(String mappingKey) {
                return null;
            }

            public String getPluginUid() {
                return "no.dusken.common.plugin.p1";
            }

            @Override
            public String getPluginName(Locale locale) {
                return "p1";
            }

            @Override
            public String getPluginDescription(Locale locale) {
                return null;
            }

            public String getPluginDescription() {
                return "Plugin1";
            }
        };
        DuskenPlugin plugin2 = new DuskenPlugin() {

            public List<HandlerMapping> getHandlerMappings() {
                return null;
            }

            public AuthenticationProvider getAuthenticationProvider() {
                return null;
            }

            @Override
            public AccessDecisionVoter getAccessDecisionVoter() {
                return null;
            }

            @Override
            public PluginContentMapping getContentMapping(String mappingKey) {
                return null;
            }

            public String getPluginUid() {
                return "no.dusken.common.plugin.p2";
            }

            @Override
            public String getPluginName(Locale locale) {
                return "p2";
            }

            @Override
            public String getPluginDescription(Locale locale) {
                return "Plugin2";
            }

        };
        List<DuskenPlugin> plugins = new LinkedList<DuskenPlugin>();
        plugins.add(plugin1);
        plugins.add(plugin2);
        return plugins;
    }

    private File getNonExistingTempFile() throws IOException {
        File file = File.createTempFile("pluginstore", ".xml");
        file.delete();
        file = new File(file.getParentFile(), file.getName());
        file.deleteOnExit();
        return file;
    }
}