org.unidle.social.AnalyticTrackingConnectInterceptorTest.java Source code

Java tutorial

Introduction

Here is the source code for org.unidle.social.AnalyticTrackingConnectInterceptorTest.java

Source

/*
 * #%L
 * unidle
 * %%
 * Copyright (C) 2013 Martin Lau
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
package org.unidle.social;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.connect.ConnectionData;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.unidle.analytics.AnalyticsStubImpl;
import org.unidle.analytics.config.AnalyticsTestConfiguration;
import org.unidle.config.CacheConfiguration;
import org.unidle.config.DataConfiguration;
import org.unidle.config.I18NConfiguration;
import org.unidle.config.ServiceConfiguration;
import org.unidle.config.SocialConfiguration;
import org.unidle.domain.User;
import org.unidle.repository.UserRepository;
import org.unidle.social.test.ConnectionStub;

import static org.fest.assertions.Assertions.assertThat;
import static org.unidle.analytics.AnalyticsEvent.CONNECT;
import static org.unidle.analytics.AnalyticsEvent.PROPERTY_DISPLAY_NAME;
import static org.unidle.analytics.AnalyticsEvent.PROPERTY_PROVIDER_ID;
import static org.unidle.analytics.AnalyticsEvent.PROPERTY_PROVIDER_USER_ID;

@ContextHierarchy({ @ContextConfiguration(classes = CacheConfiguration.class),
        @ContextConfiguration(classes = DataConfiguration.class),
        @ContextConfiguration(classes = I18NConfiguration.class),
        @ContextConfiguration(classes = ServiceConfiguration.class),
        @ContextConfiguration(classes = AnalyticsTestConfiguration.class),
        @ContextConfiguration(classes = SocialConfiguration.class) })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class AnalyticTrackingConnectInterceptorTest {

    @Autowired
    private AnalyticsStubImpl analytics;

    @Autowired
    private AnalyticTrackingConnectInterceptor<Object> subject;

    private User user;

    @Autowired
    private UserRepository userRepository;

    @Before
    public void setUp() throws Exception {
        user = new User();

        user.setEmail("email@example.com");
        user.setFirstName("first name");
        user.setLastName("last name");

        user = userRepository.save(user);

        analytics.reset();
    }

    @After
    public void tearDown() throws Exception {
        analytics.reset();
    }

    @Test
    public void testPostConnect() throws Exception {
        SecurityContextHolder.getContext()
                .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.postConnect(new ConnectionStub<>(new ConnectionData("provider id", "provider user id",
                "display name", "profile url", "image url", "access token", "secret", "refresh token", 1234L)),
                null);

        assertThat(analytics.trackings.size()).isEqualTo(1);
        assertThat(analytics.trackings.containsKey(user.getUuid())).isTrue();
        assertThat(analytics.trackings.getFirst(user.getUuid()).getLeft()).isEqualTo(CONNECT);
        assertThat(analytics.trackings.getFirst(user.getUuid()).getRight()).contains(PROPERTY_DISPLAY_NAME,
                "display name", PROPERTY_PROVIDER_ID, "provider id", PROPERTY_PROVIDER_USER_ID, "provider user id");
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testPreConnect() throws Exception {
        subject.preConnect(null, null, null);

        // Nothing to assert
    }

}