Skip to content

Commit

Permalink
test Cart class IQSS#5634
Browse files Browse the repository at this point in the history
  • Loading branch information
alexscheitlin committed Mar 13, 2019
1 parent 9b11e76 commit a9314e9
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ private Entry<String,String> createEntry(String title, String persistentId) {
}

public void addItem(String title, String persistentId) throws Exception{
if (!contents.contains(createEntry(title, persistentId))) {
if (!checkCartForItem(title, persistentId)) {
contents.add(createEntry(title, persistentId));
} else {
throw new Exception(title + "already in cart.");
throw new Exception(title + " already in cart.");
}
}

Expand Down
118 changes: 118 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/CartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package edu.harvard.iq.dataverse;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map.Entry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CartTest {

private Cart cart;
private String title;
private String persistentId;

@Before
public void setUp() {
this.cart = new Cart();
this.title = "title";
this.persistentId = "persistentId";
}

@After
public void tearDwon() {
this.cart = null;
this.title = null;
this.persistentId = null;
}

@Test
public void addNonExistingItem() {
try {
this.cart.addItem(this.title, this.persistentId);
} catch (Exception e) {
fail("Item not added.");
}
}

@Test
public void addExistingItem() {
try {
this.cart.addItem(this.title, this.persistentId);
this.cart.addItem(this.title, this.persistentId);
} catch (Exception e) {
assertEquals(this.title + " already in cart.", e.getMessage());
return;
}

fail("Added same item twice!");
}

@Test
public void removeExistingItem() {
try {
this.cart.addItem(this.title, this.persistentId);
this.cart.removeItem(this.title, this.persistentId);
} catch (Exception e) {
fail("Item not removed.");
}
}

@Test
public void removeNonExistingItem() {
try {
this.cart.removeItem(this.title, this.persistentId);
} catch (Exception e) {
assertEquals(this.title + " not in cart.", e.getMessage());
return;
}

fail("Non-existing item removed.");
}

@Test
public void getContents() {
try {
this.cart.addItem(this.title, this.persistentId);
} catch (Exception e) {
fail("Item not added.");
return;
}

List<Entry<String, String>> contents = this.cart.getContents();
assertEquals(1, contents.size());
assertEquals(this.title, contents.get(0).getKey());
assertEquals(this.persistentId, contents.get(0).getValue());
}

@Test
public void checkCartForItem() {
try {
this.cart.addItem(this.title, this.persistentId);
} catch (Exception e) {
fail("Item not added.");
return;
}

assertTrue(this.cart.checkCartForItem(this.title, this.persistentId));
}

@Test
public void clear() {
try {
this.cart.addItem(this.title, this.persistentId);
} catch (Exception e) {
fail("Item not added.");
return;
}

assertEquals(1, this.cart.getContents().size());
this.cart.clear();
assertEquals(0, this.cart.getContents().size());
}
}

0 comments on commit a9314e9

Please sign in to comment.