package it.gcatania.auctionservice.services.impl;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import it.gcatania.auctionservice.dao.PersistentBidRepository;
import it.gcatania.auctionservice.dataobjects.Bid;
import it.gcatania.auctionservice.dataobjects.Item;
import it.gcatania.auctionservice.dataobjects.User;
import it.gcatania.auctionservice.dataobjects.impl.PersistentBid;
import it.gcatania.auctionservice.dataobjects.impl.PersistentBidKey;
import it.gcatania.auctionservice.services.AuctionService;

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration
public class AuctionServiceTest {

  @Autowired
  private AuctionService auctionService;

  @Autowired
  private PersistentBidRepository pbRepo;

  @Test
  public void testPlaceBid() {
    User alice = new User("Alice");
    Item oldSofa = new Item("Old sofa");
    Bid bid = new Bid(alice, oldSofa, 50);
    auctionService.placeBid(bid);

    PersistentBid pb = pbRepo.findOne(new PersistentBidKey("Alice", "Old sofa"));
    assertThat(pb, notNullValue());
    assertThat(pb.getBidAmount(), equalTo(50d));
  }

}

@@ -124,86 +124,85 @@
 est.
-Core
 Matcher
-s.notNullValue;%0Aimport static org.hamcrest.MatcherAssert.assertThat
+Assert.assertThat;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Optional
 ;%0A%0Ai
@@ -1452,24 +1452,519 @@
 t(pb
-, notNullValue()
+.getBidAmount(), equalTo(50d));%0A  %7D%0A%0A  @Test%0A  public void testFindWinningBid() %7B%0A    String itemName = %22A pineapple%22;%0A    pbRepo.save(Arrays.asList(new PersistentBid(%22Alice%22, itemName, 11)));%0A    pbRepo.save(Arrays.asList(new PersistentBid(%22Bob%22, itemName, 33)));%0A    pbRepo.save(Arrays.asList(new PersistentBid(%22Carl%22, itemName, 18)));%0A    pbRepo.save(Arrays.asList(new PersistentBid(%22Dave%22, itemName, 32)));%0A%0A    Optional%3CBid%3E b = auctionService.getWinningBid(new Item(itemName));%0A    Bid winningBid = b.get(
 );%0A 
@@ -1969,34 +1969,42 @@
 %0A    assertThat(
-pb
+winningBid
 .getBidAmount(),
@@ -2004,30 +2004,96 @@
 ount(), equalTo(
-50d
+33d));%0A    assertThat(winningBid.getBidder(), equalTo(new User(%22Bob%22)
 ));%0A  %7D%0A%0A%7D%0A

test for winning bid