Search in sources :

Example 1 with SiteRequestScheme

use of org.ambraproject.wombat.config.site.url.SiteRequestScheme in project wombat by PLOS.

the class TestTheme method testResolveForeignJournalKey.

@Test(dataProvider = "booleans")
public void testResolveForeignJournalKey(final boolean useJournalKeyMap) throws Exception {
    SiteRequestScheme dummyScheme = SiteRequestScheme.builder().build();
    Theme homeTheme = new StubTheme("homeTheme", "homeJournal") {

        @Override
        protected Map<String, Object> getJournalConfigMap() {
            Map<String, Object> map = super.getJournalConfigMap();
            if (useJournalKeyMap) {
                ImmutableMap<String, String> otherJournals = ImmutableMap.of("targetJournal", "targetSite");
                map.put("otherJournals", otherJournals);
            }
            return map;
        }
    };
    Site homeSite = new Site("homeSite", homeTheme, dummyScheme, "");
    Theme targetTheme = new StubTheme("targetTheme", "targetJournal");
    Site targetSite = new Site("targetSite", targetTheme, dummyScheme, "");
    SiteSet siteSet = new SiteSet(ImmutableList.of(homeSite, targetSite));
    Site resolved = homeTheme.resolveForeignJournalKey(siteSet, "targetJournal");
    assertEquals(resolved, targetSite);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) SiteSet(org.ambraproject.wombat.config.site.SiteSet) Test(org.testng.annotations.Test)

Example 2 with SiteRequestScheme

use of org.ambraproject.wombat.config.site.url.SiteRequestScheme in project wombat by PLOS.

the class ControllerTestConfiguration method activeSite.

/**
 *  Unit test can only work with a single site.
 */
@Bean
protected Site activeSite(Theme theme) {
    final SiteRequestScheme mockRequestScheme = mock(SiteRequestScheme.class);
    doAnswer(invocation -> {
        final Object[] args = invocation.getArguments();
        final HttpServletRequest request = (HttpServletRequest) args[0];
        return true;
    }).when(mockRequestScheme).isForSite(any(HttpServletRequest.class));
    final Site mockSite = mock(Site.class);
    when(mockSite.getRequestScheme()).thenReturn(mockRequestScheme);
    when(mockSite.getTheme()).thenReturn(theme);
    when(mockSite.getKey()).thenReturn(DESKTOP_PLOS_ONE);
    when(mockSite.toString()).thenReturn(DESKTOP_PLOS_ONE);
    when(mockSite.getJournalKey()).thenReturn(DESKTOP_PLOS_ONE);
    return mockSite;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Site(org.ambraproject.wombat.config.site.Site) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) Bean(org.springframework.context.annotation.Bean)

Example 3 with SiteRequestScheme

use of org.ambraproject.wombat.config.site.url.SiteRequestScheme in project wombat by PLOS.

the class SiteSet method validateSchemes.

/**
 * @throws RuntimeConfigurationException if there a detectable collision between site config values
 */
private static void validateSchemes(Collection<Site> sites) {
    Map<String, Site> keys = Maps.newHashMapWithExpectedSize(sites.size());
    Map<SiteRequestScheme, Site> requestSchemes = Maps.newHashMapWithExpectedSize(sites.size());
    for (Site site : sites) {
        Site previous;
        String key = site.getKey();
        previous = keys.put(key, site);
        if (previous != null) {
            throw new RuntimeConfigurationException("Multiple sites have the same key: " + key);
        }
        /*
       * Depends on the equals and hashCode implementations of the SiteRequestPredicate implementations contained in
       * the SiteRequestScheme. If they don't provide good overrides, this check is still safe against false positives
       * but may not catch all error conditions.
       */
        SiteRequestScheme requestScheme = site.getRequestScheme();
        previous = requestSchemes.put(requestScheme, site);
        if (previous != null) {
            String message = String.format("Multiple sites (%s, %s) have the same request scheme: %s", key, previous.getKey(), requestScheme);
            throw new RuntimeConfigurationException(message);
        }
    }
}
Also used : RuntimeConfigurationException(org.ambraproject.wombat.config.RuntimeConfigurationException) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme)

Example 4 with SiteRequestScheme

use of org.ambraproject.wombat.config.site.url.SiteRequestScheme in project wombat by PLOS.

the class SiteSet method create.

public static SiteSet create(List<? extends Map<String, ?>> siteSpecifications, ThemeGraph themeGraph) {
    List<Site> sites = Lists.newArrayListWithCapacity(siteSpecifications.size());
    for (Map<String, ?> siteSpec : siteSpecifications) {
        String siteKey = (String) siteSpec.get("key");
        String themeKey = (String) siteSpec.get("theme");
        String siteType = (String) siteSpec.get("type");
        Theme theme = themeGraph.getTheme(themeKey);
        if (theme == null) {
            throw new RuntimeException(String.format("No theme with key=\"%s\" found (for site: %s)", themeKey, siteKey));
        }
        Map<String, ?> resolveDefinition = (Map<String, ?>) siteSpec.get("resolve");
        SiteRequestScheme requestScheme = resolveDefinition != null ? parseRequestScheme(resolveDefinition) : SiteRequestScheme.builder().setPathToken(siteKey).build();
        sites.add(new Site(siteKey, theme, requestScheme, siteType));
    }
    validateSchemes(sites);
    return new SiteSet(sites);
}
Also used : SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) Theme(org.ambraproject.wombat.config.theme.Theme) BiMap(com.google.common.collect.BiMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) HashBiMap(com.google.common.collect.HashBiMap) Map(java.util.Map)

Example 5 with SiteRequestScheme

use of org.ambraproject.wombat.config.site.url.SiteRequestScheme in project wombat by PLOS.

the class WombatControllerTestConfig method siteSetDependency.

@Bean
public SiteSet siteSetDependency() {
    Site mockSite = mock(Site.class);
    ImmutableSet<Site> sitesSet = new ImmutableSet.Builder<Site>().add(mockSite).build();
    when(mockSite.getJournalKey()).thenReturn("daJournalKey");
    SiteRequestScheme mockRequestScheme = mock(SiteRequestScheme.class);
    when(mockRequestScheme.isForSite(any(HttpServletRequest.class))).thenReturn(true);
    when(mockSite.getRequestScheme()).thenReturn(mockRequestScheme);
    when(mockSite.toString()).thenReturn(MOCK_SITE_URL);
    SiteSet mockSet = mock(SiteSet.class);
    when(mockSet.getSites()).thenReturn(sitesSet);
    return mockSet;
}
Also used : Site(org.ambraproject.wombat.config.site.Site) HttpServletRequest(javax.servlet.http.HttpServletRequest) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) SiteSet(org.ambraproject.wombat.config.site.SiteSet) Bean(org.springframework.context.annotation.Bean)

Aggregations

SiteRequestScheme (org.ambraproject.wombat.config.site.url.SiteRequestScheme)6 Site (org.ambraproject.wombat.config.site.Site)4 SiteSet (org.ambraproject.wombat.config.site.SiteSet)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Bean (org.springframework.context.annotation.Bean)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 Map (java.util.Map)1 RuntimeConfigurationException (org.ambraproject.wombat.config.RuntimeConfigurationException)1 Theme (org.ambraproject.wombat.config.theme.Theme)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1