11package junit .com .excilys .cdb .services ;
22
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotNull ;
5+ import static org .junit .Assert .fail ;
36import static org .mockito .Mockito .when ;
47
58import com .excilys .cdb .daos .ComputerDao ;
9+ import com .excilys .cdb .models .Company ;
610import com .excilys .cdb .models .Computer ;
11+ import com .excilys .cdb .models .Order ;
12+ import com .excilys .cdb .models .OrderBy ;
13+ import com .excilys .cdb .models .QueryPageParameter ;
714import com .excilys .cdb .services .ComputerService ;
815import com .excilys .cdb .validators .ValidatorException ;
916
1017import org .easymock .EasyMock ;
18+ import org .junit .After ;
19+ import org .junit .AfterClass ;
1120import org .junit .Before ;
21+ import org .junit .BeforeClass ;
1222import org .junit .Test ;
1323import org .junit .runner .RunWith ;
24+ import org .mockito .Mockito ;
1425import org .mockito .MockitoAnnotations ;
26+ import org .slf4j .Logger ;
27+ import org .slf4j .LoggerFactory ;
1528import org .springframework .beans .factory .annotation .Autowired ;
1629import org .springframework .test .context .ContextConfiguration ;
1730import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1831
32+ import java .time .LocalDateTime ;
33+ import java .util .ArrayList ;
1934import java .util .List ;
2035
2136/**
2540@ ContextConfiguration ("classpath:/spring/cdb-context-test.xml" )
2641public class ComputerServiceTest {
2742
43+ // Logger
44+ private static final Logger LOGGER = LoggerFactory .getLogger (ComputerServiceTest .class );
45+
2846 @ Autowired
2947 ComputerService computerService ;
3048
@@ -34,15 +52,38 @@ public class ComputerServiceTest {
3452
3553 /** The computers. */
3654 List <Computer > computers ;
55+ QueryPageParameter qp ;
56+ Computer computer ;
57+
58+ // Hook methods
59+ @ BeforeClass
60+ public static void prepareTest () {
61+ LOGGER .info ("---------------- START ComputerServiceTest ----------------\n " );
62+ }
63+
64+ @ AfterClass
65+ public static void endTest () {
66+ LOGGER .info ("---------------- END ComputerServiceTest ----------------\n " );
67+ }
3768
38- /**
39- * Inits the ComputerDaoImpl mock.
40- */
4169 @ Before
42- public void init () {
70+ public void setUp () {
71+ LOGGER .info ("START TEST CASE" );
4372 MockitoAnnotations .initMocks (this );
73+ computers = new ArrayList <Computer >();
74+ qp = Mockito .mock (QueryPageParameter .class );
75+ computer = Mockito .mock (Computer .class );
76+ }
77+
78+ @ After
79+ public void tearDown () {
80+ computers = null ;
81+ qp = null ;
82+ computer = null ;
83+ LOGGER .info ("END TEST CASE\n \n " );
4484 }
4585
86+ // ***************** FINDBYID TEST *****************
4687 /**
4788 * Find by id negative param test.
4889 */
@@ -53,6 +94,21 @@ public void findByIdNegativeParamTest() {
5394 computerService .findById (testedId );
5495 }
5596
97+ /**
98+ * Find by id that is valid param test.
99+ */
100+ @ Test
101+ public void findByValidIdTest () {
102+ long testedId = 64L ;
103+ computers = new ArrayList <Computer >();
104+ computers .add (new Computer (testedId , "TestComputer" , null , null , null ));
105+ when (computerDao .findById (testedId )).thenReturn (computers );
106+ long result = computerService .findById (testedId ).get (0 ).getId ();
107+
108+ assertEquals (testedId , result );
109+ }
110+
111+ // ***************** FINDBYNAME TEST *****************
56112 /**
57113 * Find by name null param test.
58114 */
@@ -74,13 +130,208 @@ public void findByNameEmptyStringParamTest() {
74130 computerService .findByName (testedName );
75131 }
76132
133+ /**
134+ * Test find by name method with a valid name.
135+ */
136+ @ Test
137+ public void findByNameValidTest () {
138+ String testedName = "testComputer" ;
139+ computers .add (new Computer (1 , testedName , null , null , null ));
140+ when (computerDao .findByName (testedName )).thenReturn (computers );
141+ String result = computerService .findByName (testedName ).get (0 ).getName ();
142+
143+ assertEquals (testedName , result );
144+ }
145+
146+ // ***************** DELETEBYID TEST *****************
77147 /**
78148 * Delete by id negative param test.
79149 */
80150 @ Test (expected = ValidatorException .class )
81151 public void deleteByIdNegativeParamTest () {
82- long testedId = -2 ;
152+ long testedId = -2L ;
83153 computerService .deleteComputer (testedId );
84154 EasyMock .expectLastCall ().once ();
85155 }
156+
157+ /**
158+ * Delete by valid id param test.
159+ */
160+ @ Test
161+ public void deleteByIdValidTest () {
162+ long testedId = 52L ;
163+ try {
164+ computerService .deleteComputer (testedId );
165+ } catch (Throwable t ) {
166+ fail ("Exception thrown on valid id deletion" );
167+ }
168+ }
169+
170+ // ***************** FINDBYQUERYPARAMETER TEST *****************
171+ /**
172+ * Find by qp invalid page index test.
173+ */
174+ @ Test (expected = ValidatorException .class )
175+ public void findByQpInvalidPageIndexTest () {
176+ int testedPage = -1 ;
177+ when (qp .getLimit ()).thenReturn (2 );
178+ when (qp .getOffset ()).thenReturn (30 );
179+ when (qp .getPageIndex ()).thenReturn (testedPage );
180+ when (computerDao .findByQuery (qp )).thenReturn (null );
181+ computerService .findByQuery (qp );
182+ }
183+
184+ /**
185+ * Find by qp invalid page size test.
186+ */
187+ @ Test (expected = ValidatorException .class )
188+ public void findByQpInvalidPageSizeTest () {
189+ int testedPageSize = -1 ;
190+ when (qp .getLimit ()).thenReturn (2 );
191+ when (qp .getOffset ()).thenReturn (30 );
192+ when (qp .getPageSize ()).thenReturn (testedPageSize );
193+ when (computerDao .findByQuery (qp )).thenReturn (null );
194+ computerService .findByQuery (qp );
195+ }
196+
197+ /**
198+ * Find by qp invalid search test.
199+ */
200+ @ Test (expected = ValidatorException .class )
201+ public void findByQpInvalidSearchTest () {
202+ String testedSearch = null ;
203+ when (qp .getLimit ()).thenReturn (2 );
204+ when (qp .getOffset ()).thenReturn (30 );
205+ when (qp .getSearch ()).thenReturn (testedSearch );
206+ when (computerDao .findByQuery (qp )).thenReturn (null );
207+ computerService .findByQuery (qp );
208+ }
209+
210+ /**
211+ * Find by qp invalid order test.
212+ */
213+ @ Test (expected = ValidatorException .class )
214+ public void findByQpInvalidOrderTest () {
215+ Order testedOrder = null ;
216+ when (qp .getLimit ()).thenReturn (2 );
217+ when (qp .getOffset ()).thenReturn (30 );
218+ when (qp .getOrder ()).thenReturn (testedOrder );
219+ when (computerDao .findByQuery (qp )).thenReturn (null );
220+ computerService .findByQuery (qp );
221+ }
222+
223+ /**
224+ * Find by qp invalid order by test.
225+ */
226+ @ Test (expected = ValidatorException .class )
227+ public void findByQpInvalidOrderByTest () {
228+ OrderBy testedOrderBy = null ;
229+ when (qp .getLimit ()).thenReturn (2 );
230+ when (qp .getOffset ()).thenReturn (30 );
231+ when (qp .getOrderBy ()).thenReturn (testedOrderBy );
232+ when (computerDao .findByQuery (qp )).thenReturn (null );
233+ computerService .findByQuery (qp );
234+ }
235+
236+ /**
237+ * Find by valid qp by test.
238+ */
239+ @ Test
240+ public void findByQpValidTest () {
241+
242+ int testedPage = 2 ;
243+ int testedPageSize = 30 ;
244+ OrderBy testedOrderBy = OrderBy .id ;
245+ Order testedOrder = Order .ASC ;
246+ String testedSearch = "Mac" ;
247+
248+ when (qp .getPageIndex ()).thenReturn (testedPage );
249+ when (qp .getPageSize ()).thenReturn (testedPageSize );
250+ when (qp .getOrder ()).thenReturn (testedOrder );
251+ when (qp .getOrderBy ()).thenReturn (testedOrderBy );
252+ when (qp .getSearch ()).thenReturn (testedSearch );
253+ when (qp .getLimit ()).thenReturn (testedPageSize );
254+ when (qp .getOffset ()).thenReturn (testedPage * testedPageSize );
255+
256+ when (computerDao .findByQuery (qp )).thenReturn (computers );
257+ List <Computer > results = computerService .findByQuery (qp );
258+ assertNotNull (results );
259+ }
260+
261+ // ***************** COUNT TEST *****************
262+ /**
263+ * Count invalid search field.
264+ */
265+ @ Test (expected = ValidatorException .class )
266+ public void countInvalidSearchFieldTest () {
267+ String testedSearch = null ;
268+ long count = 2L ;
269+ when (qp .getLimit ()).thenReturn (2 );
270+ when (qp .getOffset ()).thenReturn (30 );
271+ when (qp .getSearch ()).thenReturn (testedSearch );
272+ when (computerDao .count (qp )).thenReturn (count );
273+ long result = computerService .count (qp );
274+ assertEquals (true , (count != result ));
275+ }
276+
277+ /**
278+ * Count invalid search field.
279+ */
280+ @ Test
281+ public void countvalidSearchFieldTest () {
282+ String testedSearch = "MAC" ;
283+ long count = 2L ;
284+ when (qp .getLimit ()).thenReturn (2 );
285+ when (qp .getOffset ()).thenReturn (30 );
286+ when (qp .getSearch ()).thenReturn (testedSearch );
287+ when (computerDao .count (qp )).thenReturn (count );
288+ long result = computerService .count (qp );
289+ assertEquals (true , (count == result ));
290+ }
291+
292+ // ***************** CREATE COMPUTER TEST *****************
293+ /**
294+ * Create a valid computer.
295+ */
296+ @ Test
297+ public void createAValidComputerTest () {
298+ long testedId = 58L ;
299+ String testedName = "Mac" ;
300+ LocalDateTime testedIntro = LocalDateTime .now ();
301+ LocalDateTime testedDisc = testedIntro .plusDays (2 );
302+ Company company = new Company (20L , null );
303+
304+ when (computer .getId ()).thenReturn (testedId );
305+ when (computer .getName ()).thenReturn (testedName );
306+ when (computer .getIntroduced ()).thenReturn (testedIntro );
307+ when (computer .getDiscontinued ()).thenReturn (testedDisc );
308+ when (computer .getCompany ()).thenReturn (company );
309+
310+ when (computerDao .insertComputer (computer )).thenReturn (58L );
311+
312+ computerDao .insertComputer (computer );
313+ }
314+
315+ /**
316+ * Create a computer with inconsistent dates.
317+ */
318+ @ Test (expected = ValidatorException .class )
319+ public void createInconsistentDatesTest () {
320+ long testedId = 58L ;
321+ String testedName = "Mac" ;
322+ LocalDateTime testedIntro = LocalDateTime .now ();
323+ LocalDateTime testedDisc = testedIntro .minusDays (2 );
324+ Company company = new Company (20L , null );
325+
326+ when (computer .getId ()).thenReturn (testedId );
327+ when (computer .getName ()).thenReturn (testedName );
328+ when (computer .getIntroduced ()).thenReturn (testedIntro );
329+ when (computer .getDiscontinued ()).thenReturn (testedDisc );
330+ when (computer .getCompany ()).thenReturn (company );
331+
332+ when (computerDao .insertComputer (computer )).thenReturn (58L );
333+
334+ computerService .createComputer (computer );
335+ }
336+
86337}
0 commit comments