/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.dom.customer;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.pragprog.dhnako.carserv.dom.vehicle.Vehicle;

@RunWith(JMock.class)
public class GivenCustomerWithSomeVehiclesTest {

	private Mockery context = new JUnit4Mockery() {{
		setImposteriser(ClassImposteriser.INSTANCE);
	}};
	private Vehicle mockOwnedVehicle, mockOtherVehicle;
	private Customer customer;
	
	@Before
	public void setUp() throws Exception {
		customer = new Customer();
		mockOwnedVehicle = context.mock(Vehicle.class, "owned");
		mockOtherVehicle = context.mock(Vehicle.class, "other");
		customer.getVehicles().add(mockOwnedVehicle);
	}
	
	@Test
	public void whenAttemptToDeleteVehicleNotOwnedByCustomerThenShouldBeInvalid() throws Exception {
		assertThat(customer.validateDeleteVehicle(mockOtherVehicle), is("Customer does not own this vehicle"));
	}

	@Test
	public void whenAttemptToDeleteVehicleOwnedByCustomerThenShouldBeValid() throws Exception {
		assertThat(customer.validateDeleteVehicle(mockOwnedVehicle), is(nullValue()));
	}

	@Test
	public void whenDeleteVehicleThenShouldAskVehicleToDeleteItself() throws Exception {
		context.checking(new Expectations() {
			{
				one(mockOwnedVehicle).delete();
			}
		});
		customer.deleteVehicle(mockOwnedVehicle);
	}
}
