Adding a new feature

Once we have the necessary test environment in place, we can add the new feature.

As a library manager, I want to know all the history for a given book so that I can measure which books are more in demand than others.

We will start with a red specification:

public class BooksSpec { 
 
    @Test 
    public void should_search_for_any_past_state() { 
        Book book1 = new Book("title", "author", 
States.AVAILABLE); book1.censor(); Books books = new Books(); books.add(book1); String available = String.valueOf(States.AVAILABLE); assertThat( books.filterByState(available).isEmpty(),
is(false)); } }

Run all the tests and see the last one fail.

Implement the search on all states (fragment):

public class Book { 
 
    private ArrayList<Integer> status; 
 
    public Book(String title, String author, int status) { 
        this.title = title; 
        this.author = author; 
        this.status = new ArrayList<>(); 
        this.status.add(status); 
    } 
 
    public int getStatus() { 
        return status.get(status.size()-1); 
    } 
 
     public void rent() { 
        status.add(States.RENTED); 
    } 
    [...] 
 
    public List<Integer> anyState() { 
        return status; 
    } 
    [...] 

In this fragment, we have omitted the irrelevant parts—things that were not modified, or more modifier methods, such as rent, that have changed the implementation in the same fashion:

public class Books { 
    public Books filterByState(String state) { 
        Integer expectedState = Integer.valueOf(state); 
        return new Books( 
            new ConcurrentLinkedQueue<>( 
                books.stream() 
                  .filter(x 
-> x.anyState() .contains(expectedState)) .collect(toList()))); } [...]

The outside methods, especially the serialization to JSON, are not affected, as the getStatus method still returns an int value.

We run all the tests and everything is green.

Tasks performed:

  • Dependency injection in BooksEndpoint for books
  • Change status for statuses

The to-do list:

  • Remove the primitive obsession with status (optional)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68