View Javadoc
1   package de.japrost.jabudget.spring;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Optional;
6   
7   import org.assertj.core.api.Assertions;
8   import org.junit.Before;
9   import org.junit.Test;
10  import org.mockito.Mockito;
11  
12  import de.japrost.jabudget.domain.DomainException;
13  import de.japrost.jabudget.domain.DomainFailure;
14  import de.japrost.jabudget.domain.account.Account;
15  import de.japrost.jabudget.domain.fixtures.account.AccountFixtureValues;
16  import de.japrost.jabudget.domain.fixtures.account.AccountFixtures;
17  import de.japrost.jabudget.service.AccountService;
18  
19  /**
20   * Test the {@link AccountController}.
21   */
22  public class AccountControllerTest {
23  
24  	private AccountController cut;
25  	private AccountService accountService;
26  	private final AccountFixtures accountFixtures = new AccountFixtures();
27  
28  	/**
29  	 * Set up each test.
30  	 */
31  	@Before
32  	public void setUp() {
33  		accountService = Mockito.mock(AccountService.class);
34  		cut = new AccountController(accountService);
35  	}
36  
37  	/**
38  	 * Simple delegation.
39  	 */
40  	@Test
41  	public void retrieveAllDelegates() {
42  		// given
43  		final List<Account> result = new ArrayList<>();
44  		Mockito.when(accountService.retrieveAll()).thenReturn(result);
45  		// when
46  		final List<Account> actual = cut.retrieveAll();
47  		//then
48  		// TODO use a comparison not equal for domain objects.
49  		Assertions.assertThat(actual).isEqualTo(result);
50  	}
51  
52  	/**
53  	 * Simple delegation.
54  	 *
55  	 * @throws DomainException never
56  	 */
57  	@Test
58  	public void retrieveByIdDelegates() throws DomainException {
59  		// given
60  		final Account account = accountFixtures.createDefault();
61  		Mockito.when(accountService.retrieveById(AccountFixtureValues.ACCOUNT_DEF_ID)).thenReturn(Optional.of(account));
62  		// when
63  		final Account actual = cut.retrieveById(AccountFixtureValues.ACCOUNT_DEF_ID);
64  		//then
65  		// TODO use a comparison not equal for domain objects.
66  		Assertions.assertThat(actual).isEqualTo(account);
67  	}
68  
69  	/**
70  	 * Fail on missing entity.
71  	 *
72  	 * @throws DomainException never
73  	 */
74  	@Test
75  	public void retrieveByIdFailsOnMissingEntity() throws DomainException {
76  		// given
77  		Mockito.when(accountService.retrieveById(AccountFixtureValues.ACCOUNT_DEF_ID))
78  				.thenReturn(Optional.ofNullable(null));
79  		// when
80  		final Throwable thrown = Assertions.catchThrowable(() -> {
81  			cut.retrieveById(AccountFixtureValues.ACCOUNT_DEF_ID);
82  		});
83  		//then
84  		Assertions.assertThat(thrown).isInstanceOf(DomainException.class);
85  		// TODO create Assertions for DomainException
86  		Assertions.assertThat(((DomainException) thrown).getFailure()).isEqualTo(DomainFailure.ENTITY_NOT_AVAILABLE);
87  	}
88  
89  	/**
90  	 * Simple delegation.
91  	 *
92  	 * @throws DomainException never
93  	 */
94  	@Test
95  	public void createDelegates() throws DomainException {
96  		// given
97  		final Account.Builder builder = accountFixtures.createDefaultBuilder();
98  		final Account result = builder.build();
99  		Mockito.when(accountService.create(Mockito.any(Account.class))).thenReturn(result);
100 		// when
101 		final Account actual = cut.create(builder);
102 		//then
103 		// TODO use a comparison not equal for domain objects.
104 		Assertions.assertThat(actual).isEqualTo(result);
105 	}
106 
107 	/**
108 	 * Delegation with setting id.
109 	 *
110 	 * @throws DomainException never
111 	 */
112 	@Test
113 	public void updateDelegates() throws DomainException {
114 		// given
115 		final Account.Builder builder = accountFixtures.createAlternateBuilder();
116 		final Account result = accountFixtures.createDefault();
117 		Mockito.when(accountService.update(Mockito.any())).thenReturn(result);
118 		// when
119 		final Account actual = cut.update(AccountFixtureValues.ACCOUNT_DEF_ID, builder);
120 		//then
121 		// TODO use a comparison not equal for domain objects.
122 		Assertions.assertThat(actual).isEqualTo(result);
123 	}
124 
125 	/**
126 	 * Simple delegation.
127 	 */
128 	@Test
129 	public void deleteDelegates() {
130 		// given
131 		// when
132 		cut.delete(AccountFixtureValues.ACCOUNT_DEF_ID);
133 		//then
134 		Mockito.verify(accountService).erase(AccountFixtureValues.ACCOUNT_DEF_ID);
135 	}
136 }