Skip to content

Commit

Permalink
Kata/Collections/practice
Browse files Browse the repository at this point in the history
  • Loading branch information
kiran1552 committed Sep 20, 2016
1 parent 2a4dec4 commit ebf5486
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* A utility class to generate pet hotels with pets already booked
*/
public class APetHotel {

public static PetAdder with(int petCount) {
return new PetAdder(petCount);
}
Expand Down Expand Up @@ -38,5 +39,13 @@ private Breed someBreed() {
private String someName(int petCount) {
return PET_NAMES.get(random.nextInt(PET_NAMES.size())) + " " + petCount;
}

public PetHotel petsCheckIn() {
PetHotel petHotel= new PetHotel();
for (int i=0;i<petCount; i++)
petHotel.checkIn(Pet.dog().named("Fido-"+i));

return petHotel;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package serenitylabs.tutorials.vetclinic.collections.katas;

/**
* Created by kimahale on 9/20/2016.
*/
public class BookingResponse {

private final boolean confirmedStatus;

public BookingResponse(boolean confirmedStatus) {
this.confirmedStatus = confirmedStatus;
}

public boolean isConfirmed() {
return confirmedStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package serenitylabs.tutorials.vetclinic.collections.katas;

import serenitylabs.tutorials.vetclinic.Pet;

import java.util.*;

/**
* Created by kimahale on 9/20/2016.
*/
public class PetHotel {

private Collection<Pet> pets= new TreeSet<>(Comparator.comparing(Pet::getName));
public static int DEFAULT_MAXIMUM_CAPACITY = 20;

public List<Pet> getPets() {
return new ArrayList(pets);
}

public BookingResponse checkIn(Pet pet) {

if (pets.size() <DEFAULT_MAXIMUM_CAPACITY)
pets.add(pet);
return new BookingResponse(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,111 @@
package serenitylabs.tutorials.vetclinic.collections.katas;

import org.junit.Test;
import serenitylabs.tutorials.vetclinic.Pet;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class WhenBookingPetsIntoAPetHotel {


@Test
public void the_hotel_should_initially_have_no_pets_booked() {
// When
PetHotel petHotel = new PetHotel();
// Then
assertThat(petHotel.getPets(), hasSize(0));
}

@Test
public void should_be_able_to_check_a_pet_into_the_hotel() throws Exception {
// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
//When
petHotel.checkIn(fido);
// Then
assertThat(petHotel.getPets(), hasItems(fido));
}

@Test
public void should_be_able_to_check_in_several_pets() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");

//When
petHotel.checkIn(fido);
petHotel.checkIn(felix);
// Then
assertThat(petHotel.getPets(), hasItems(fido, felix));

}


@Test
public void should_not_be_able_to_check_in_the_same_pet_twice() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");

petHotel.checkIn(fido);
petHotel.checkIn(felix);

// When
petHotel.checkIn(fido);
// Then
assertThat(petHotel.getPets(), containsInAnyOrder(fido, felix));

}

@Test
public void should_be_able_to_retrieve_checked_in_pets_in_alphabetical_order() throws Exception {
// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");
Pet tommy= Pet.dog().named("Tommy");


// When
petHotel.checkIn(fido);
petHotel.checkIn(felix);
petHotel.checkIn(tommy);
// Then
assertThat(petHotel.getPets(), contains(felix, fido, tommy));


}

@Test
public void should_be_able_to_obtain_a_booking_confirmation_when_we_check_in_a_pet() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
// When
BookingResponse bookingResponse= petHotel.checkIn(fido);
// Then
assertThat(bookingResponse.isConfirmed(), equalTo(true));
}

@Test
public void should_not_be_able_to_check_in_pets_beyond_hotel_capacity() throws Exception {
// Given
PetHotel petHotel=APetHotel.with(20).petsCheckIn();
Pet fido= Pet.dog().named("Fido");

// When
petHotel.checkIn(fido);

// Then
assertThat(petHotel.getPets(), hasSize(20));

}

@Test
Expand Down

0 comments on commit ebf5486

Please sign in to comment.