Skip to content

Lab03 repository

Have a way of storing and finding orders in memory, hidden behind a single interface.

Steps

Be sure to skim through all of the steps before getting started, especially the Notes & Questions.

1. Give MealOrder an Identity

This will make an MealOrder fulfill the Entity pattern (but we're not using any Spring or anything, so no annotations will be used).

  • Add a private Long id member variable to the MealOrder class.

    Note

    Make sure you name the member variable exactly this way, as a Long with the name id.

  • Create a setter & getter for the id property.

2. Create a Repository

  • Create a new class MealOrderRepository that implements the methods below. Do not change the method names or return values.

  • Write the code Test First to implement each method:

    1. Write a failing test
    2. Write just enough code to make the test pass
    3. Make sure all tests pass at each step
public class MealOrderRepository {

    // Returns the MealOrder that is associated with the given id.
    public MealOrder findOne(Long id) {
      // Return the meal order matching the given id.
      // If there's no MealOrder with that id, return null.
    }

    // This method stores the given MealOrder in the repository so it can be found later.
    public MealOrder save(MealOrder entity) {
      //      
      // If the MealOrder came in with NO id, SET it with a UNIQUE one that
      // you (that is, the repository) generates
      //
      // If the incoming MealOrder object's id is ALREADY set, DON'T modify it
      //
      // return the MealOrder object that must now have its id set (i.e., is NOT null)
    }

    // This method returns all MealOrders as a List<MealOrder>
    public List<MealOrder> findAll() {
      // If there are no MealOrders, return an empty List, e.g., Collections.emptyList() 
    }
}

Notes and Questions

  • What is appropriate Java Collection class to use to store the meal orders so that they're easy to find MealOrder's by their id?

  • You will want an MealOrderRepository constructor that can take a MealOrder to "pre-load" the repository with some "sample" data. For example, you should be able to (in your test) create an MealOrderRepository like this:

@Test
public void findAllShouldReturn1MealOrder() {
  MealOrder mealOrder = new MealOrder();
  mealOrder.addBurger(BurgerTopping.CHEESE);
  mealOrder.setId(1L);

  MealOrderRepository repo = new MealOrderRepository(mealOrder);
  assertThat(repo.findAll())
    .hasSize(1);
}

You will do the minimum to make this compile, but not pass.

Test Suggestions

  • To test the save() method, you'll want to pay attention to the assignment of IDs to each meal order. For example, this test should pass once save() is implemented correctly:

    @Test
    public void newlySavedMealOrdersHaveUniqueIds() {
      MealOrderRepository mealOrderRepository = new MealOrderRepository();
      MealOrder mealOrder1 = new MealOrder();
      mealOrderRepository.save(mealOrder1);
      MealOrder mealOrder2 = new MealOrder();
      mealOrderRepository.save(mealOrder2);
    
      assertThat(mealOrder1.getId())
          .isNotEqualTo(mealOrder2.getId());
    }
    

Instructor Check-in

Before going any further, have the instructor look over your work (are all of your tests passing?).