Skip to content

Commit

Permalink
DICE-UNC#388 cart remove
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed May 27, 2021
1 parent 9a58487 commit b033be9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public String serializeShoppingCartContentsToStringOneItemPerLine() {
return sb.toString();
}

/**
* Remove an item from the cart, ignore if item is not in the cart
*
* @param itemPath {@code String} with the item to remove
*/
public void removeItem(final String itemPath) {
if (itemPath == null || itemPath.isEmpty()) {
throw new IllegalArgumentException("null or empty itemPath");
}

this.shoppingCartEntries.remove(itemPath);

}

/**
* Given a string representation (one file per line, separated by the \n
* character, build a {@code FileShoppingCart}. The cart will be empty if no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ String serializeShoppingCartAsSpecifiedUser(FileShoppingCart fileShoppingCart, S
*/
FileShoppingCart appendToShoppingCart(final String key, final List<String> fileList) throws JargonException;

FileShoppingCart removeSpecifiedItemsFromShoppingCart(final String key, final List<String> fileList) throws JargonException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ public FileShoppingCart appendToShoppingCart(final String key, final List<String

}

@Override
public FileShoppingCart removeSpecifiedItemsFromShoppingCart(final String key, final List<String> fileList)
throws JargonException {
log.info("removeSpecifiedItemsFromShoppingCart()");

if (key == null || key.isEmpty()) {
throw new IllegalArgumentException("null or empty key");
}

log.info("key:{}", key);

if (fileList == null) {
throw new IllegalArgumentException("null or empty fileList");
}

// retrieve the cart

FileShoppingCart cart = this.retreiveShoppingCartAsLoggedInUser(key);

for (String removeItem : fileList) {
log.debug("remove item:{}", removeItem);
cart.removeAnItem(removeItem);
}

this.serializeShoppingCartAsLoggedInUser(cart, key);

log.info("items removed");
return cart;

}

/*
* (non-Javadoc)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.irods.jargon.core.connection.IRODSAccount;
Expand Down Expand Up @@ -110,6 +111,39 @@ public final void testSerializeDeserializeShoppingCartAsLoggedInUser() throws Ex

}

@Test
public final void testRemoveItemsFromCart() throws Exception {
String key = "key";
String expectedPath1 = "/a/path";
String expectedPath2 = "/a/path2";
String expectedPath3 = "/a/path3";

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

DataCacheServiceFactory dataCacheServiceFactory = new DataCacheServiceFactoryImpl(
irodsFileSystem.getIRODSAccessObjectFactory());

ShoppingCartService shoppingCartService = new ShoppingCartServiceImpl(
irodsFileSystem.getIRODSAccessObjectFactory(), irodsAccount, dataCacheServiceFactory);
FileShoppingCart fileShoppingCart = FileShoppingCart.instance();
fileShoppingCart.addAnItem(ShoppingCartEntry.instance(expectedPath1));
fileShoppingCart.addAnItem(ShoppingCartEntry.instance(expectedPath2));
fileShoppingCart.addAnItem(ShoppingCartEntry.instance(expectedPath3));
shoppingCartService.serializeShoppingCartAsLoggedInUser(fileShoppingCart, key);

// remove items 1 and 3

List<String> itemsToRemove = new ArrayList<>();
itemsToRemove.add(expectedPath1);
itemsToRemove.add(expectedPath3);
shoppingCartService.removeSpecifiedItemsFromShoppingCart(key, itemsToRemove);

FileShoppingCart cart = shoppingCartService.retreiveShoppingCartAsLoggedInUser(key);
Assert.assertEquals("did not delete expected items", 1, cart.getShoppingCartFileList().size());
String itemLeft = cart.getShoppingCartFileList().get(0);
Assert.assertEquals("did not get expected item", expectedPath2, itemLeft);
}

@Test
public final void testAddItemToCartWithADuplicate() throws Exception {
String key = "key";
Expand Down

0 comments on commit b033be9

Please sign in to comment.