Skip to content

Commit

Permalink
Cleaning Java compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Dec 1, 2013
1 parent 6401269 commit ceae23e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 77 deletions.
Expand Up @@ -29,7 +29,6 @@

import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthRuntimeException;
import org.apache.oltu.oauth2.rsfilter.OAuthDecision;
import org.fcrepo.http.commons.session.SessionFactory;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -39,28 +38,28 @@ public class DefaultOAuthResourceProviderTest {

private static final String DUMMY_RSID = "dummy-rsid";
private static final String INVALID_TOKEN = "invalid-token";

private static final String VALID_TOKEN = "valid-token";
@Mock
private SessionFactory mockSessions;

@Mock
private Session mockSession;

@Mock
private Node mockNode;

@Mock
private Property mockClientProperty;

@Mock
private Property mockPrincipalProperty;

@Mock
private HttpServletRequest mockRequest;

DefaultOAuthResourceProvider testObj;

@Before
public void setUp() throws RepositoryException, NoSuchFieldException {
initMocks(this);
Expand All @@ -79,22 +78,20 @@ public void setUp() throws RepositoryException, NoSuchFieldException {
testObj = new DefaultOAuthResourceProvider();
setField(testObj, "sessionFactory", mockSessions);
}

@Test
public void testAcceptsExistingTokenRequest()
throws PathNotFoundException, RepositoryException, OAuthProblemException {
when(mockSession.getNode("/tokens/" + VALID_TOKEN))
.thenReturn(mockNode);
OAuthDecision actual =
testObj.validateRequest(DUMMY_RSID, VALID_TOKEN, mockRequest);
testObj.validateRequest(DUMMY_RSID, VALID_TOKEN, mockRequest);
verify(mockSession).logout();
}

@Test(expected=OAuthRuntimeException.class)
public void testRejectsNonexistentTokenRequest()
throws PathNotFoundException, RepositoryException, OAuthProblemException {
OAuthDecision actual =
testObj.validateRequest(DUMMY_RSID, INVALID_TOKEN, mockRequest);
testObj.validateRequest(DUMMY_RSID, INVALID_TOKEN, mockRequest);
verify(mockSession).logout();
}

Expand Down
Expand Up @@ -16,10 +16,7 @@
package org.fcrepo.auth.oauth.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Matchers.startsWith;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -48,26 +45,24 @@


public class AuthzEndpointTest {

private static final String DUMMY_TOKEN = "dummyOauthToken";


@Mock
private HttpServletRequest mockRequest;

@Mock
private SessionFactory mockSessions;

@Mock
private Session mockSession;

@Mock
private Node mockRootNode;

@Mock
private Node mockCodeNode;

private AuthzEndpoint testObj;

@Before
public void setUp() throws RepositoryException, NoSuchFieldException {
initMocks(this);
Expand All @@ -85,69 +80,69 @@ public void setUp() throws RepositoryException, NoSuchFieldException {
@Test
public void testValidCodeRequest()
throws URISyntaxException, OAuthSystemException, RepositoryException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getParameter("client_id")).thenReturn("bond");
when(mockRequest.getMethod()).thenReturn("GET");
when(mockRequest.getParameter("redirect_uri"))
.thenReturn("http://fedora.info/redirect");
when(mockRequest.getParameter(OAuth.OAUTH_RESPONSE_TYPE))
.thenReturn("code");
Response actual = testObj.getAuthorization(mockRequest);
final Response actual = testObj.getAuthorization(mockRequest);
assertEquals(302, actual.getStatus());
URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
final URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
assertEquals(-1, redirect.getQuery().indexOf("error="));
verify(mockSession).save();
verify(mockSession).logout();
}

@Test
public void testMissingResponseType()
throws URISyntaxException, OAuthSystemException, RepositoryException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getMethod()).thenReturn("GET");
when(mockRequest.getParameter("redirect_uri"))
.thenReturn("http://fedora.info/redirect");
when(mockRequest.getParameter("client_id")).thenReturn("bond");
Response actual = testObj.getAuthorization(mockRequest);
final Response actual = testObj.getAuthorization(mockRequest);
assertEquals(302, actual.getStatus());
URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
final URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
assertEquals(0, redirect.getQuery().indexOf("error=invalid_request"));
verify(mockSession, times(0)).save();
}

@Test
public void testUnsupportedResponseType()
throws URISyntaxException, OAuthSystemException, RepositoryException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getMethod()).thenReturn("GET");
when(mockRequest.getParameter("redirect_uri"))
.thenReturn("http://fedora.info/redirect");
when(mockRequest.getParameter("client_id")).thenReturn("bond");
when(mockRequest.getParameter(OAuth.OAUTH_RESPONSE_TYPE))
.thenReturn("token");
Response actual = testObj.getAuthorization(mockRequest);
final Response actual = testObj.getAuthorization(mockRequest);
assertEquals(302, actual.getStatus());
URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
final URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
assertEquals(0, redirect.getQuery().indexOf("error=unsupported_response_type"));
verify(mockSession, times(0)).save();
}

@Test
public void testMissingClientId()
throws URISyntaxException, OAuthSystemException, RepositoryException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getMethod()).thenReturn("GET");
when(mockRequest.getParameter("redirect_uri"))
.thenReturn("http://fedora.info/redirect");
when(mockRequest.getParameter(OAuth.OAUTH_RESPONSE_TYPE))
.thenReturn("token");
Response actual = testObj.getAuthorization(mockRequest);
final Response actual = testObj.getAuthorization(mockRequest);
assertEquals(302, actual.getStatus());
URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
final URI redirect = URI.create(actual.getMetadata().getFirst("Location").toString());
assertEquals(0, redirect.getQuery().indexOf("error=invalid_request"));
verify(mockSession, times(0)).save();
}

/**
* Redirect uri parms are optional, but the error handling
* for a bad request is different when it is absent
Expand All @@ -157,7 +152,7 @@ public void testMissingClientId()
*/
@Test(expected=WebApplicationException.class)
public void testMissingRedirectUriAndClientId() throws URISyntaxException, OAuthSystemException, RepositoryException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getMethod()).thenReturn("GET");
when(mockRequest.getParameter(OAuth.OAUTH_RESPONSE_TYPE))
.thenReturn("code");
Expand Down
Expand Up @@ -37,12 +37,12 @@
import com.google.common.collect.Sets;

public class DefaultTokenRequestValidationsTest {

private static String INVALID_CODE = "invalid-code";

private static String VALID_CODE = "valid-code";

private static String INVALID_CLIENT = "invalid-client";
// private static String INVALID_CLIENT = "invalid-client";

private static String VALID_CLIENT = "valid-client";

Expand All @@ -51,24 +51,24 @@ public class DefaultTokenRequestValidationsTest {
private static String VALID_SCOPE = "valid-scope";

@Mock OAuthTokenRequest mockRequest;

@Mock
private SessionFactory mockSessions;

@Mock
private Session mockSession;

@Mock
private Node mockNode;

@Mock
Property mockProperty;

@Mock
Property mockScopes;

private DefaultTokenRequestValidations testObj;

@Before
public void setUp() throws RepositoryException {
initMocks(this);
Expand All @@ -86,20 +86,20 @@ public void setUp() throws RepositoryException {

testObj = new DefaultTokenRequestValidations(mockSessions);
}

@Test
public void testValidAuthCodeUnscoped() throws RepositoryException {
when(mockRequest.getClientId()).thenReturn(VALID_CLIENT);
when(mockRequest.getCode()).thenReturn(VALID_CODE);

assertTrue(testObj.isValidAuthCode(mockRequest));
}

@Test
public void testValidAuthCodeInScope() throws RepositoryException {
when(mockRequest.getClientId()).thenReturn(VALID_CLIENT);
when(mockRequest.getCode()).thenReturn(VALID_CODE);

when(mockRequest.getScopes()).thenReturn(Sets.newHashSet(VALID_SCOPE));
when(mockNode.getProperty("oauth-scopes"))
.thenReturn(mockScopes);
Expand All @@ -113,7 +113,7 @@ public void testValidAuthCodeOutOfScope() throws RepositoryException {
when(mockRequest.getScopes()).thenReturn(Sets.newHashSet(INVALID_SCOPE));
when(mockNode.getProperty("oauth-scopes"))
.thenReturn(mockScopes);

assertFalse(testObj.isValidAuthCode(mockRequest));
}
@Test
Expand All @@ -126,23 +126,23 @@ public void testInvalidAuthCode() throws RepositoryException {
public void testValidClient() {
assertTrue(testObj.isValidClient(mockRequest));
}

@Test
public void testInvalidClient() {
assertFalse(testObj.isValidClient(null));
}

@Test
public void testMissingClient() throws RepositoryException {
when(mockRequest.getClientId()).thenReturn(null);
assertFalse(testObj.isValidAuthCode(mockRequest));
}

@Test
public void testValidSecret() {
assertTrue(testObj.isValidSecret(mockRequest));
}

@Test
public void testInvalidSecret() {
assertFalse(testObj.isValidSecret(null));
Expand Down
Expand Up @@ -18,7 +18,6 @@
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand All @@ -30,8 +29,7 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -66,16 +64,13 @@ public void setLogger() {
protected static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/rest/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

public AbstractOAuthResourceIT() {
connectionManager.setMaxTotal(MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
connectionManager.closeIdleConnections(3, SECONDS);
client = new DefaultHttpClient(connectionManager);
final HttpClientBuilder b =
HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE)
.setMaxConnTotal(MAX_VALUE);
client = b.build();
}

protected static HttpPost postObjMethod(final String pid) {
Expand Down

0 comments on commit ceae23e

Please sign in to comment.