Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kata/collections/test #47

Open
wants to merge 17 commits into
base: kata/collections/start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
machine:
java:
version: oraclejdk8

test:
post:
- mkdir -p $CIRCLE_TEST_REPORTS/junit/
- find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \;
- find . -type f -regex ".*/target/site/serenity/SERENITY-JUNIT-*.xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \;
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/vetclinic/Breed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package vetclinic;

public enum Breed {
Cat, Dog, Rabbit, Fish, Parrot
}
57 changes: 57 additions & 0 deletions src/main/java/vetclinic/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package vetclinic;

import com.google.common.base.Objects;

public class Pet {
private final String name;
private final Breed breed;

public Pet(String name, Breed breed) {
this.name = name;
this.breed = breed;
}

public String getName() {
return name;
}

public Breed getBreed() {
return breed;
}

public static PetBuilder dog() { return new PetBuilder(Breed.Dog);}
public static PetBuilder cat() { return new PetBuilder(Breed.Cat);}
public static PetBuilder rabbit() { return new PetBuilder(Breed.Rabbit);}
public static PetBuilder parrot() { return new PetBuilder(Breed.Parrot);}
public static PetBuilder fish() { return new PetBuilder(Breed.Fish);}

public static class PetBuilder {
private final Breed breed;

public PetBuilder(Breed breed) {
this.breed = breed;
}

public Pet named(String name) {
return new Pet(name, breed);
}
}

@Override
public String toString() {
return "a " + breed + " called " + name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pet pet = (Pet) o;
return Objects.equal(name, pet.name) && breed == pet.breed;
}

@Override
public int hashCode() {
return Objects.hashCode(name, breed);
}
}
88 changes: 88 additions & 0 deletions src/main/java/vetclinic/collections/katas/APetHotel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package vetclinic.collections.katas;

import com.google.common.collect.ImmutableList;
import vetclinic.Breed;
import vetclinic.Pet;

import java.util.*;

/**
* A utility class to generate pet hotels with pets already booked
*/
public class APetHotel {
private final String name;
private final int count;
private Collection<Pet> pets = new TreeSet<>(Comparator.comparing(Pet::getName));
private Queue<Pet> waitingPets = new LinkedList<>();

public Queue<Pet> getWaitingPets() {
return waitingPets;
}

public APetHotel(String hotelName, int petCount) {
this.name=hotelName;
this.count=petCount;
}

public static PetHotelBuilder with(int petCount) {
return new PetHotelBuilder(petCount);
}

public Collection<Pet> getPets() {
return pets;
}

public BookingConfirmation checkIn(Pet pet) {
BookingStratgey bookingStrategy = BOOKING_STARTEGY.get(currentAvailability());
return bookingStrategy.checkIn(pet);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of the strategy pattern, well done!

}


private static final Map<HotelAvailability,BookingStratgey> BOOKING_STARTEGY= new HashMap<>();{
BOOKING_STARTEGY.put(HotelAvailability.AVAILABLE,new ConfirmBookingStrategy(pets));
BOOKING_STARTEGY.put(HotelAvailability.FULL,new WaitingStrategy(waitingPets));
}

private HotelAvailability currentAvailability() {
return ((pets.size() < count)) ? HotelAvailability.AVAILABLE : HotelAvailability.FULL;
}

public void checkOut(Pet pet) {
pets.remove(pet);
if(!waitingPets.isEmpty()){
pets.add(waitingPets.poll());
}

}

public static class PetHotelBuilder {
private final int petCount;

public PetHotelBuilder(int petCount) {
this.petCount=petCount;
}

public APetHotel named(String hotelName) {
return new APetHotel(hotelName,petCount);
}

private Pet somePet(int petCount) {
return new Pet(someName(petCount), someBreed());
}

private final static Random random = new Random();

private Breed someBreed() {
return Breed.values()[ random.nextInt(Breed.values().length) ];
}

private final static List<String> PET_NAMES = ImmutableList.of("Fido","Felix","Rover","Spot");

public void checkInDefaultPets() {
somePet(petCount);
}
private String someName(int petCount) {
return PET_NAMES.get(random.nextInt(PET_NAMES.size())) + " " + petCount;
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/vetclinic/collections/katas/BookingConfirmation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by sapurani on 9/20/2016.
*/
public abstract class BookingConfirmation implements BookingResponse{
private static AtomicInteger bookingNumberCounter = new AtomicInteger();
private final int number;
private final Pet pet;

public BookingConfirmation (int number, Pet pet) {
this.number = number;
this.pet = pet;
}

public static BookingConfirmation confirmedFor(Pet pet) {
return new ConfirmedBooking(bookingNumberCounter.get(),pet);
}

public static BookingConfirmation waitingFor(Pet pet) {
return new PlacedOnWaitingList(bookingNumberCounter.get(),pet);
}

public Pet getPet() {
return pet;
}


}
11 changes: 11 additions & 0 deletions src/main/java/vetclinic/collections/katas/BookingResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package vetclinic.collections.katas;

/**
* Created by sapurani on 9/20/2016.
*/
public interface BookingResponse {
public boolean isConfirmed();
public boolean isWaiting();


}
11 changes: 11 additions & 0 deletions src/main/java/vetclinic/collections/katas/BookingStratgey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

/**
* Created by sapurani on 9/20/2016.
*/
public interface BookingStratgey {

public BookingConfirmation checkIn( Pet pet);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "public" keyword on methods is not required for an interface.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

/**
* Created by sapurani on 9/20/2016.
*/
public class ConfirmBookingStrategy implements BookingStratgey {
private Collection pets= new TreeSet<>(Comparator.comparing(Pet::getName));

public ConfirmBookingStrategy(Collection<Pet> pets) {
this.pets=pets;
}

public Collection getPets() {
return pets;
}

@Override
public BookingConfirmation checkIn(Pet pet) {
pets.add(pet);
return BookingConfirmation.confirmedFor(pet);
}
}
22 changes: 22 additions & 0 deletions src/main/java/vetclinic/collections/katas/ConfirmedBooking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

/**
* Created by sapurani on 9/20/2016.
*/
public class ConfirmedBooking extends BookingConfirmation {
public ConfirmedBooking(int incrementAndget, Pet pet) {
super(incrementAndget,pet);
}

@Override
public boolean isConfirmed() {
return true;
}

@Override
public boolean isWaiting() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package vetclinic.collections.katas;

/**
* Created by sapurani on 9/20/2016.
*/
public enum HotelAvailability {
AVAILABLE,FULL;
}
22 changes: 22 additions & 0 deletions src/main/java/vetclinic/collections/katas/PlacedOnWaitingList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

/**
* Created by sapurani on 9/20/2016.
*/
public class PlacedOnWaitingList extends BookingConfirmation {

public PlacedOnWaitingList(int incrementAndget, Pet pet) {
super(incrementAndget,pet);
}
@Override
public boolean isConfirmed() {
return false;
}

@Override
public boolean isWaiting() {
return true;
}
}
23 changes: 23 additions & 0 deletions src/main/java/vetclinic/collections/katas/WaitingStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vetclinic.collections.katas;

import vetclinic.Pet;

import java.util.LinkedList;
import java.util.Queue;

/**
* Created by sapurani on 9/20/2016.
*/
public class WaitingStrategy implements BookingStratgey {
private Queue<Pet> pets = new LinkedList<>();

public WaitingStrategy(Queue<Pet> waitingPets) {
this.pets=waitingPets;
}

@Override
public BookingConfirmation checkIn(Pet pet) {
pets.add(pet);
return PlacedOnWaitingList.waitingFor(pet);
}
}
4 changes: 4 additions & 0 deletions src/main/java/vetclinic/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Our fantastic Vet Clinic app starts here
**/
package vetclinic;
1 change: 1 addition & 0 deletions src/test/java/serenitylabs/tutorials/vetclinic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your test code goes here.
Loading