Skip to content

Commit

Permalink
Issue #5 - improved after-refactor coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bor committed Mar 30, 2013
1 parent af0bc36 commit 800511f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 56 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/javaswift/joss/client/mock/ClientMock.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.javaswift.joss.client.mock;

import org.javaswift.joss.command.mock.factory.AuthenticationCommandFactoryMock;
import org.javaswift.joss.command.shared.factory.AuthenticationCommandFactory;
import org.javaswift.joss.model.Client;
import org.javaswift.joss.swift.scheduled.ObjectDeleter;
import org.javaswift.joss.swift.MockUserStore;
Expand Down Expand Up @@ -27,18 +29,21 @@ public AccountMock authenticate(String tenant, String username, String password,

public AccountMock authenticate(String tenant, String username, String password, String authUrl, String preferredRegion) {
LOG.info("JOSS / MOCK mode");
if (!allowEveryone) {
LOG.info("JOSS / Attempting authentication with tenant: "+tenant+", username: "+username+", Auth URL: "+authUrl);
users.authenticate(username, password);
}
LOG.info("JOSS / Creating mock account instance");
LOG.info("JOSS / * Check credentials: "+!allowEveryone);
LOG.info("JOSS / * Allow objectdeleter: "+allowObjectDeleter);
LOG.info("JOSS / * Use onFileObjectStore: "+onFileObjectStore);
LOG.info("JOSS / * Use public URL: "+publicUrl);
Swift swift = new Swift()
.setObjectDeleter(allowObjectDeleter ? new ObjectDeleter(10, 10) : null)
.setOnFileObjectStore(onFileObjectStore)
.setUserStore(users)
.setPublicUrl(publicUrl);
if (!allowEveryone) {
AuthenticationCommandFactory authenticationCommandFactory = new AuthenticationCommandFactoryMock(swift);
LOG.info("JOSS / Attempting authentication with tenant: "+tenant+", username: "+username+", Auth URL: "+authUrl);
authenticationCommandFactory.createAuthenticationCommand(null, null, tenant, username, password).call();
}
return new AccountMock(swift);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public CommandMock(Swift swift, Account account, Container container) {
this(swift, account, container, null);
}

public CommandMock(Swift swift, Account account, StoredObject object) {
this(swift, account, null, object);
}

public CommandMock(Swift swift, Account account, Container container, StoredObject object) {
this.swift = swift;
this.account = account;
Expand All @@ -45,12 +41,8 @@ public T call() {
HttpStatusChecker.verifyCode(getStatusCheckers(), result.getStatus());
return result.getPayload();
} catch (CommandException err) {
LOG.error(
"JOSS / "+this.getClass().getSimpleName()+
(err.getHttpStatusCode() == 0 ? "" : ", HTTP status "+err.getHttpStatusCode())+
(err.getError() == null ? "" : ", Error "+err.getError())+
(err.getMessage() == null ? "" : ", Message '"+err.getMessage()+"'")+
(err.getCause() == null ? "" : ", Cause "+err.getCause().getClass().getSimpleName()));
LOG.error("JOSS / "+this.getClass().getSimpleName()+
", HTTP status "+err.getHttpStatusCode());
throw err;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@

public class AuthenticationCommandMock extends CommandMock<AccessImpl> implements AuthenticationCommand {

private final String url;
private final String tenant;
private final String username;
private final String password;

public AuthenticationCommandMock(Swift swift, String url, String tenant, String username, String password) {
super(swift, null, null, null);
this.url = url;
this.tenant = tenant;
this.username = username;
this.password = password;
}

@Override
public SwiftResult<AccessImpl> callSwift() {
return null;
return swift.authenticate(tenant, username, password);
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/javaswift/joss/swift/MockUserStore.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.javaswift.joss.swift;

import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.exception.CommandExceptionError;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,12 +12,13 @@ public class MockUserStore {

private Map<String, String> users = new TreeMap<String, String>();

public void authenticate(String username, String password) {
public boolean authenticate(String tenant, String username, String password) {
String expectedPassword = users.get(username);
if (expectedPassword == null || !expectedPassword.equals(password)) {
LOG.warn("JOSS / Failed to authenticate with user '"+username+"'");
throw new CommandException(HttpStatus.SC_UNAUTHORIZED, CommandExceptionError.UNAUTHORIZED);
return false;
}
return true;
}

public void addUser(String username, String password) {
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/org/javaswift/joss/swift/Swift.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.javaswift.joss.swift;

import org.apache.commons.io.IOUtils;
import org.javaswift.joss.command.shared.identity.access.AccessImpl;
import org.javaswift.joss.headers.Header;
import org.javaswift.joss.headers.account.AccountBytesUsed;
import org.javaswift.joss.headers.account.AccountContainerCount;
Expand Down Expand Up @@ -30,6 +31,8 @@ public class Swift {

private ObjectDeleter objectDeleter;

private MockUserStore users = new MockUserStore();

private String publicUrl;

private HeaderStore headers = new HeaderStore();
Expand All @@ -52,6 +55,11 @@ public Swift setObjectDeleter(ObjectDeleter objectDeleter) {
return this;
}

public Swift setUserStore(MockUserStore users) {
this.users = users;
return this;
}

public Swift setPublicUrl(String publicUrl) {
this.publicUrl = publicUrl;
return this;
Expand All @@ -61,6 +69,14 @@ public ObjectDeleter getObjectDeleter() {
return this.objectDeleter;
}

public SwiftResult<AccessImpl> authenticate(String tenant, String username, String password) {
if (users.authenticate(tenant, username, password)) {
return new SwiftResult<AccessImpl>(null, HttpStatus.SC_OK);
} else {
return new SwiftResult<AccessImpl>(HttpStatus.SC_UNAUTHORIZED);
}
}

public SwiftResult<Collection<Container>> listContainers(Account account, ListInstructions listInstructions) {
Collection<SwiftContainer> pagedContainers = new PageServer<SwiftContainer>().createPage(
containers.values(),
Expand Down Expand Up @@ -251,7 +267,7 @@ public SwiftResult<Object> saveObjectMetadata(Container container, StoredObject
foundContainer.deleteObject(foundObject.getName());
} else {
this.objectDeleter.scheduleForDeletion(foundContainer, foundObject, deleteAt.getDate());
this.headers.addHeader(deleteAt);
foundObject.addHeader(deleteAt);
}
}
return foundObject.saveMetadata(headers);
Expand All @@ -266,19 +282,6 @@ private <T extends Header> T getSpecificHeader(Collection<? extends Header> head
return null;
}

// public StoredObject setDeleteAfter(long seconds) {
// return setDeleteAt(new Date(new Date().getTime() + seconds * 1000));
// }
//
// public StoredObject setDeleteAt(Date date) {
// ObjectDeleter objectDeleter = ((AccountMock)getContainer().getAccount()).getObjectDeleter();
// this.info.setDeleteAt(new DeleteAt(date));
// if (objectDeleter != null) {
// objectDeleter.scheduleForDeletion(this, date);
// }
// return this;
// }

public SwiftResult<ObjectInformation> getObjectInformation(Container container, StoredObject object) {
SwiftContainer foundContainer = containers.get(container.getName());
if (foundContainer == null) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/javaswift/joss/swift/SwiftStoredObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ public ObjectManifest getObjectManifest() {
return this.objectManifest;
}

public void addHeader(Header header) {
this.headers.addHeader(header);
}

}
25 changes: 5 additions & 20 deletions src/test/java/org/javaswift/joss/swift/MockUserStoreTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.javaswift.joss.swift;

import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.exception.CommandExceptionError;
import org.javaswift.joss.swift.MockUserStore;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

public class MockUserStoreTest {

Expand All @@ -20,28 +17,16 @@ public void prepare() {

@Test
public void authenticateSuccess() {
try {
users.authenticate("richard", "test123");
} catch (Exception err) {
fail("Should not have thrown an exception");
}
assertTrue(users.authenticate("", "richard", "test123"));
}

@Test
public void userDoesNotExist() {
try {
users.authenticate("charlie", "123test");
} catch (CommandException err) {
assertEquals(CommandExceptionError.UNAUTHORIZED, err.getError());
}
assertFalse(users.authenticate("", "charlie", "123test"));
}

@Test
public void userHasWrongPassword() {
try {
users.authenticate("richard", "321tset");
} catch (CommandException err) {
assertEquals(CommandExceptionError.UNAUTHORIZED, err.getError());
}
assertFalse(users.authenticate("", "richard", "321tset"));
}
}

0 comments on commit 800511f

Please sign in to comment.