Package org.bigk.invoices.test.services

Source Code of org.bigk.invoices.test.services.PurchasersServiceTest

package org.bigk.invoices.test.services;

import static org.junit.Assert.*;
import static org.bigk.invoices.model.builder.PurchaserBuilder.*;

import org.bigk.invoices.model.Purchaser;
import org.bigk.invoices.services.PurchasersService;
import org.bigk.invoices.test.SpringJPATests;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;


@ContextConfiguration("/test-appcontext-dao.xml")
public class PurchasersServiceTest extends SpringJPATests {

  @Autowired
  private PurchasersService service;
 
  @Before
  public void loadFixtures() {
    loadFixture("purchasers.yaml");   
  }
 
  @Test
  public void nullReturnedForNonExistingPurchaserId() throws Exception {
   
    // given
    Long nonExistingPurchasersId = maxExistingPurchasersId() + 1;
   
    // when
    Purchaser purchaser = service.getPurchaser(nonExistingPurchasersId);
   
    // then
    assertNull(purchaser);
  }
 
  private Purchaser purchaserWithMaxId() {
    return em.createQuery("SELECT p FROM Purchaser p ORDER BY p.id DESC", Purchaser.class)
        .setMaxResults(1).getSingleResult();
  }
 
  private Long maxExistingPurchasersId() {
    Purchaser purchaserWithMaxId = purchaserWithMaxId();
    if (purchaserWithMaxId != null) {
      return purchaserWithMaxId.getId();
    }
    return 0L;
  }
 
  @Test
  public void objectReturnedForExistingPurchaserId() throws Exception {
   
    // given
    Purchaser anExistingPurchaser = anyExistingPurchaser();
   
    // when
    Purchaser purchaser = service.getPurchaser(anExistingPurchaser.getId());
   
    // then
    assertNotNull(purchaser);
    assertEquals(anExistingPurchaser, purchaser);
  }
 
  private Purchaser anyExistingPurchaser() {
    return em.createQuery("SELECT p FROM Purchaser p", Purchaser.class)
        .setMaxResults(1).getSingleResult();
  }
 
  @Test
  public void purchaserIsSaved() throws Exception {

    // given
    Purchaser aPurchaser = aPurchaser()
        .withName("New Name CO")
        .withAddress("Neue Strasse 7B, 13456 Wunderbar Staedtchen")
        .withNip("111-22-33-444")
        .build();

    // when
    service.savePurchaser(aPurchaser);
   
    // then
    Purchaser savedPurchaser = em.createQuery("SELECT p FROM Purchaser p WHERE p.name = :name", Purchaser.class)
        .setParameter("name", aPurchaser.getName())
        .getSingleResult();
    assertNotNull(savedPurchaser.getId());
    assertEquals(aPurchaser.getName(), savedPurchaser.getName());
    assertEquals(aPurchaser.getAddress(), savedPurchaser.getAddress());
    assertEquals(aPurchaser.getNip(), savedPurchaser.getNip());
  }
 
 
  @Test
  public void purchaserIsUpdated() throws Exception {
   
    // given
    Purchaser aPurchaser = anyExistingPurchaser();
    aPurchaser.setAddress("Neue Strasse 7B, 13456 Wunderbar Staedtchen");
    em.detach(aPurchaser);
   
    // when
    service.updatePurchaser(aPurchaser);
   
    // then
    Purchaser updatedPurchaser = em.find(Purchaser.class, aPurchaser.getId());
    assertEquals(aPurchaser.getId(), updatedPurchaser.getId());
    assertEquals(aPurchaser.getAddress(), updatedPurchaser.getAddress());
    assertEquals(aPurchaser, updatedPurchaser);
  }
 
  @Test
  public void existingPurchaserDeletedSuccessfully() throws Exception {

    // given
    long expectedCountAfterRemoval = countPurchasers() - 1;
    Purchaser aPurchaser = anyExistingPurchaser();
    em.detach(aPurchaser);
   
    // when
    service.deletePurchaser(aPurchaser);
   
    // then
    long countAfterRemoval = countPurchasers();
    assertEquals(expectedCountAfterRemoval, countAfterRemoval);
    assertNull(em.find(Purchaser.class, aPurchaser.getId()));
  }
 
  private Long countPurchasers() {
    return em.createQuery("SELECT COUNT(p) FROM Purchaser p", Long.class).getSingleResult();
  }
 
  @Test
  public void nonExistingPurchaserCanNotBeDeleted() throws Exception {
   
    // given
    Long expectedCountAfterRemoval = countPurchasers();
    Purchaser aPurchaser = purchaserWithMaxId();
    em.detach(aPurchaser);
    aPurchaser.setId(aPurchaser.getId() + 1);
   
    // when
    service.deletePurchaser(aPurchaser);
   
    // then
    Long countAfterRemoval = countPurchasers();
    assertEquals(expectedCountAfterRemoval, countAfterRemoval);
  }
}
TOP

Related Classes of org.bigk.invoices.test.services.PurchasersServiceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.