/*
* Copyright (c) 2010 Lockheed Martin Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eurekastreams.server.action.execution.opensocial;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.apache.shindig.social.opensocial.oauth.OAuthEntry;
import org.eurekastreams.commons.actions.context.PrincipalActionContext;
import org.eurekastreams.server.action.request.opensocial.CreateOAuthRequestTokenRequest;
import org.eurekastreams.server.domain.OAuthDomainEntry;
import org.eurekastreams.server.persistence.mappers.InsertMapper;
import org.eurekastreams.server.persistence.mappers.requests.PersistenceRequest;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
/**
* Test suite for the {@link CreateOAuthRequestTokenExecution} class.
*
*/
@SuppressWarnings("unchecked")
public class CreateOAuthRequestTokenExecutionTest
{
/**
* System under test.
*/
private CreateOAuthRequestTokenExecution sut;
/**
* Context for building mock objects.
*/
private final Mockery context = new JUnit4Mockery()
{
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};
/**
* Instance of OAuth entry mapper injected by spring.
*/
private InsertMapper<OAuthDomainEntry> insertMapper = context.mock(InsertMapper.class);
/**
* Strategy for converting {@link OAuthEntry} objects to {@link OAuthDomainEntry} objects.
*/
private OAuthEntryConversionStrategy oauthConversionStrat = context.mock(OAuthEntryConversionStrategy.class);
/**
* Mocked instance of the action context.
*/
private PrincipalActionContext actionContext = context.mock(PrincipalActionContext.class);
/**
* Instance of the OAuthDomain for this execution example: samplecontainer.com.
*/
private static final String TEST_OAUTH_DOMAIN = "testdomain.com";
/**
* Instance of the OAuthContainer for this execution example: default.
*/
private static final String TEST_OAUTH_CONTAINER = "testcontainer";
/**
* Test callback url.
*/
private static final String TEST_CALLBACK_URL = "http://example.org/callbackurl";
/**
* Test oauth version.
*/
private static final String TEST_OAUTH_VERSION = "1.0";
/**
* Test Consumer key.
*/
private static final String TEST_CONSUMER_KEY = "testconsumerkey";
/**
* Prepare the system under test.
*/
@Before
public void setup()
{
sut = new CreateOAuthRequestTokenExecution(TEST_OAUTH_DOMAIN, TEST_OAUTH_CONTAINER, insertMapper,
oauthConversionStrat);
}
/**
* Test successful token creation.
*/
@Test
public void testSuccessfulTokenCreation()
{
final CreateOAuthRequestTokenRequest request = new CreateOAuthRequestTokenRequest(TEST_CONSUMER_KEY,
TEST_OAUTH_VERSION, TEST_CALLBACK_URL);
context.checking(new Expectations()
{
{
oneOf(actionContext).getParams();
will(returnValue(request));
oneOf(oauthConversionStrat).convertToEntryDTO(with(any(OAuthEntry.class)));
oneOf(insertMapper).execute(with(any(PersistenceRequest.class)));
}
});
OAuthEntry results = sut.execute(actionContext);
assertNotNull(results);
assertEquals(results.getCallbackUrl(), TEST_CALLBACK_URL);
assertEquals(results.getConsumerKey(), TEST_CONSUMER_KEY);
assertEquals(results.getOauthVersion(), TEST_OAUTH_VERSION);
assertEquals(results.getType(), OAuthEntry.Type.REQUEST);
assertNotNull(results.getToken());
assertNotNull(results.getTokenSecret());
assertEquals(results.getContainer(), TEST_OAUTH_CONTAINER);
assertEquals(results.getDomain(), TEST_OAUTH_DOMAIN);
assertEquals(results.getCallbackUrl(), TEST_CALLBACK_URL);
assertEquals(results.isCallbackUrlSigned(), true);
context.assertIsSatisfied();
}
/**
* Test successful token creation with null callback.
*/
@Test
public void testSuccessfulTokenCreationWithNullCallback()
{
final CreateOAuthRequestTokenRequest request = new CreateOAuthRequestTokenRequest(TEST_CONSUMER_KEY,
TEST_OAUTH_VERSION, null);
context.checking(new Expectations()
{
{
oneOf(actionContext).getParams();
will(returnValue(request));
oneOf(oauthConversionStrat).convertToEntryDTO(with(any(OAuthEntry.class)));
oneOf(insertMapper).execute(with(any(PersistenceRequest.class)));
}
});
OAuthEntry results = sut.execute(actionContext);
assertNotNull(results);
assertEquals(results.getCallbackUrl(), null);
assertEquals(results.getConsumerKey(), TEST_CONSUMER_KEY);
assertEquals(results.getOauthVersion(), TEST_OAUTH_VERSION);
assertEquals(results.getType(), OAuthEntry.Type.REQUEST);
assertNotNull(results.getToken());
assertNotNull(results.getTokenSecret());
assertEquals(results.getContainer(), TEST_OAUTH_CONTAINER);
assertEquals(results.getDomain(), TEST_OAUTH_DOMAIN);
assertEquals(results.getCallbackUrl(), null);
assertEquals(results.isCallbackUrlSigned(), false);
context.assertIsSatisfied();
}
}