/*
 * 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) {
    }
}

/*
 * 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 com.hp.autonomy.searchcomponents.idol.test.IdolTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.env.Environment;
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 {
    private final Environment environment;

    @Autowired
    public IdolMvcIntegrationTestUtils(final Environment environment) {
        this.environment = environment;
    }

    @Override
    public String[] getDatabases() {
        final String testDatabase = environment.getProperty(IdolTestUtils.TEST_DATABASE_PROPERTY, IdolTestUtils.DEFAULT_TEST_DATABASE);
        return new String[]{testDatabase};
    }

    @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) {
    }
}

Allow overriding of IT database via system property.