Skip to content

Commit

Permalink
Merge pull request #28 from elwyncrestha/inventory-test
Browse files Browse the repository at this point in the history
Inventory all TDD test case
  • Loading branch information
elwyncrestha committed Jul 8, 2020
2 parents dbe4dd0 + d911808 commit 9e9981d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.pemits.webcare.api.inventory.repository;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.pemits.webcare.BaseJpaTest;
import com.pemits.webcare.api.inventory.entity.Inventory;

public class InventoryRepositoryTest extends BaseJpaTest {

private static final String MOCK_INVENTORY_NAME1 = "Inventory1";
private static final String MOCK_INVENTORY_NAME2 = "Inventory2";
private static final long MOCK_INVENTORY_ID_1 = 1L;
private static final long MOCK_INVENTORY_ID_2 = 2L;

@Autowired
private InventoryRepository repository;
Expand Down Expand Up @@ -59,4 +66,68 @@ public void testSaveAllShouldReturnAllSavedInventories() {
assertThat(saved.get(1).getId(), notNullValue());
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testSaveInventoryShouldUpdateInventory() {
final Optional<Inventory> inventory = repository.findById(MOCK_INVENTORY_ID_1);

assertThat(inventory.isPresent(), equalTo(true));
inventory.get().setName(MOCK_INVENTORY_NAME1);
inventory.get().setPrice(1200);
inventory.get().setQuantity(2);
inventory.get().setTotalPrice(1200 * 2);
inventory.get().setPurchaseDate(LocalDate.now());
Inventory update = repository.save(inventory.get());

assertThat(repository.getOne(MOCK_INVENTORY_ID_1), is(update));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testFindInventoryByIdShouldReturnInventory() {

final Optional<Inventory> inventory = repository.findById(MOCK_INVENTORY_ID_2);
assertThat(inventory.isPresent(), equalTo(true));

assertThat(inventory.get().getName(), is(MOCK_INVENTORY_NAME2));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testFindAllShouldReturnNotEmptyList() {
final List<Inventory> inventories = repository.findAll();
assertThat(inventories.size(), greaterThan(0));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testGetOneShouldReturnInventory() {
final Inventory inventory = repository.getOne(MOCK_INVENTORY_ID_1);
assertThat(inventory.getName(), is(MOCK_INVENTORY_NAME1));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testDeleteByIdShouldDeleteInventory() {
long count = repository.count();
repository.deleteById(MOCK_INVENTORY_ID_1);
assertThat(repository.findAll(), hasSize((int) count - 1));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testDeleteShouldDeleteInventory() {
long count = repository.count();
final Inventory inventory = repository.getOne(MOCK_INVENTORY_ID_2);
repository.delete(inventory);
assertThat(repository.findAll(), hasSize((int) count - 1));
}

@Test
@DatabaseSetup("/dataset/inventory/inventory-config.xml")
public void testDeleteAllShouldReturnCountZero() {
repository.deleteAll();
assertThat(repository.findAll(), hasSize(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<dataset>
<inventory id="1"
created_at="2020-07-08 15:30:00"
last_modified_at="2020-07-08 15:30:00"
version="0"
name="Inventory1"
price="1200"
quantity="2"
total_price="2400"
purchase_date="2020-07-08"
/>
<inventory id="2"
created_at="2020-07-08 15:30:00"
last_modified_at="2020-07-08 15:30:00"
version="0"
name="Inventory2"
price="1200"
quantity="2"
total_price="2400"
purchase_date="2020-07-08"
/>
</dataset>

0 comments on commit 9e9981d

Please sign in to comment.