/*
 * Copyright 2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.find.idol.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.autonomy.frontend.configuration.authentication.CommunityPrincipal;
import com.hp.autonomy.frontend.find.core.savedsearches.EmbeddableIndex;
import com.hp.autonomy.frontend.find.core.test.MvcIntegrationTestUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import java.util.Collection;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Component
@ConditionalOnProperty(value = "mock.configuration", matchIfMissing = true)
public class IdolMvcIntegrationTestUtils extends MvcIntegrationTestUtils {
    @Override
    public String[] getDatabases() {
        return new String[]{"Wookiepedia"};
    }

    @Override
    public String getDatabasesAsJson() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(getDatabases());
    }

    @Override
    public EmbeddableIndex getEmbeddableIndex() {
        return new EmbeddableIndex("Wookiepedia", null);
    }

    @Override
    protected Authentication createAuthentication(final Collection<GrantedAuthority> authorities) {
        final CommunityPrincipal communityPrincipal = new CommunityPrincipal(1L, "user", null);

        final UsernamePasswordAuthenticationToken authentication = mock(UsernamePasswordAuthenticationToken.class);
        when(authentication.isAuthenticated()).thenReturn(true);
        when(authentication.getPrincipal()).thenReturn(communityPrincipal);
        when(authentication.getAuthorities()).thenReturn(authorities);
        when(authentication.getName()).thenReturn("user");

        return authentication;
    }

    @Override
    protected void addFieldRequestParams(final MockHttpServletRequestBuilder requestBuilder) {
    }
}

@@ -564,16 +564,144 @@
 tUtils;%0A
+import com.hp.autonomy.searchcomponents.idol.test.IdolTestUtils;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -767,24 +767,73 @@
 OnProperty;%0A
+import org.springframework.core.env.Environment;%0A
 import org.s
@@ -1438,54 +1438,368 @@
     
-@Override%0A    public String%5B%5D getDatabases() %7B
+private final Environment environment;%0A%0A    @Autowired%0A    public IdolMvcIntegrationTestUtils(final Environment environment) %7B%0A        this.environment = environment;%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getDatabases() %7B%0A        final String testDatabase = environment.getProperty(IdolTestUtils.TEST_DATABASE_PROPERTY, IdolTestUtils.DEFAULT_TEST_DATABASE);
 %0A   
@@ -1823,29 +1823,28 @@
 tring%5B%5D%7B
-%22Wookiepedia%22
+testDatabase
 %7D;%0A    %7D

Allow overriding of IT database via system property.