package org.apereo.cas.services.web;

import org.apereo.cas.services.DefaultServicesManager;
import org.apereo.cas.services.InMemoryServiceRegistry;
import org.apereo.cas.services.RegexRegisteredService;
import org.apereo.cas.services.RegisteredServiceTestUtils;
import org.apereo.cas.web.support.WebUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.RequestContextHolder;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
        
/**
 * @author Scott Battaglia
 * @since 3.1
 */
public class ServiceThemeResolverTests {

    private static final String MOZILLA = "Mozilla";
    private static final String DEFAULT_THEME_NAME = "test";
    private ServiceThemeResolver serviceThemeResolver;
    private DefaultServicesManager servicesManager;
    private Map<String, String> mobileBrowsers;

    @Before
    public void setUp() throws Exception {
        this.servicesManager = new DefaultServicesManager(new InMemoryServiceRegistry());

        mobileBrowsers = new HashMap<>();
        mobileBrowsers.put(MOZILLA, "theme");
        this.serviceThemeResolver = new ServiceThemeResolver(DEFAULT_THEME_NAME, servicesManager, mobileBrowsers);
    }

    @Test
    public void verifyGetServiceThemeDoesNotExist() {
        final RegexRegisteredService r = new RegexRegisteredService();
        r.setTheme("myTheme");
        r.setId(1000);
        r.setName("Test Service");
        r.setServiceId("myServiceId");

        this.servicesManager.save(r);

        final MockHttpServletRequest request = new MockHttpServletRequest();
        final RequestContext ctx = mock(RequestContext.class);
        final MutableAttributeMap scope = new LocalAttributeMap();
        scope.put("service", RegisteredServiceTestUtils.getService(r.getServiceId()));
        when(ctx.getFlowScope()).thenReturn(scope);
        RequestContextHolder.setRequestContext(ctx);
        request.addHeader(WebUtils.USER_AGENT_HEADER, MOZILLA);
        assertEquals(DEFAULT_THEME_NAME, this.serviceThemeResolver.resolveThemeName(request));
    }

    @Test
    public void verifyGetDefaultService() {
        final MockHttpServletRequest request = new MockHttpServletRequest();
        request.setParameter("service", "myServiceId");
        request.addHeader(WebUtils.USER_AGENT_HEADER, MOZILLA);
        assertEquals(DEFAULT_THEME_NAME, this.serviceThemeResolver.resolveThemeName(request));
    }

    @Test
    public void verifyGetDefaultServiceWithNoServicesManager() {
        this.serviceThemeResolver = new ServiceThemeResolver(DEFAULT_THEME_NAME, null, mobileBrowsers);
        final MockHttpServletRequest request = new MockHttpServletRequest();
        request.setParameter("service", "myServiceId");
        request.addHeader(WebUtils.USER_AGENT_HEADER, MOZILLA);
        assertEquals(DEFAULT_THEME_NAME, this.serviceThemeResolver.resolveThemeName(request));
    }
}

@@ -31,16 +31,60 @@
 s.web;%0A%0A
+import org.apereo.cas.CasProtocolConstants;%0A
 import o
@@ -130,16 +130,16 @@
 anager;%0A
-
 import o
@@ -1508,36 +1508,16 @@
 esolver(
-DEFAULT_THEME_NAME, 
 services
@@ -1538,24 +1538,99 @@
 eBrowsers);%0A
+        this.serviceThemeResolver.setDefaultThemeName(DEFAULT_THEME_NAME);%0A
     %7D%0A%0A    @
@@ -2152,25 +2152,54 @@
 ope.put(
-%22service%22
+CasProtocolConstants.PARAMETER_SERVICE
 , Regist
@@ -2677,33 +2677,62 @@
 etParameter(
-%22service%22
+CasProtocolConstants.PARAMETER_SERVICE
 , %22myService
@@ -3039,36 +3039,16 @@
 esolver(
-DEFAULT_THEME_NAME, 
 null, mo
@@ -3054,32 +3054,107 @@
 obileBrowsers);%0A
+        this.serviceThemeResolver.setDefaultThemeName(DEFAULT_THEME_NAME);%0A
         final Mo
@@ -3247,17 +3247,46 @@
 ter(
-%22service%22
+CasProtocolConstants.PARAMETER_SERVICE
 , %22m

fix tests