Skip to content
This repository was archived by the owner on May 18, 2025. It is now read-only.

Commit d400017

Browse files
committed
Add computer service unit tests
1 parent b99c608 commit d400017

4 files changed

Lines changed: 315 additions & 7 deletions

File tree

src/main/java/com/excilys/cdb/validators/utils/ComputerValidator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public static void checkDatesConsistence(LocalDateTime intro, LocalDateTime disc
5353
}
5454
}
5555

56+
57+
5658
/**
5759
* Check starting row.
5860
*
@@ -89,6 +91,7 @@ public static void validate(Computer computer) throws ValidatorException {
8991
ComputerValidator.checkValidId(computer.getId());
9092
ComputerValidator.checkNameNotNull(computer.getName());
9193
ComputerValidator.checkNameNotEmpty(computer.getName());
94+
ComputerValidator.checkDatesConsistence(computer.getIntroduced(), computer.getDiscontinued());
9295
// Check related company
9396
if (computer.getCompany() != null) {
9497
CompanyValidator.checkValidId(computer.getCompany().getId());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package junit.com.excilys.cdb.services;
2+
3+
import com.excilys.cdb.daos.CompanyDao;
4+
import com.excilys.cdb.services.CompanyService;
5+
6+
import org.junit.After;
7+
import org.junit.AfterClass;
8+
import org.junit.Before;
9+
import org.junit.BeforeClass;
10+
import org.junit.runner.RunWith;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration("classpath:/spring/cdb-context-test.xml")
19+
public class CompanyServiceTest {
20+
21+
// Logger
22+
private static final Logger LOGGER = LoggerFactory.getLogger(CompanyServiceTest.class);
23+
24+
@Autowired
25+
CompanyService companyService;
26+
27+
@Autowired
28+
CompanyDao computerDao;
29+
30+
// Hook methods
31+
@BeforeClass
32+
public static void prepareTest() {
33+
LOGGER.info("---------------- START ComputerServiceTest ----------------\n");
34+
}
35+
36+
@AfterClass
37+
public static void endTest() {
38+
LOGGER.info("---------------- END ComputerServiceTest ----------------\n");
39+
}
40+
41+
@Before
42+
public void setUp() {
43+
LOGGER.info("START TEST CASE");
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
LOGGER.info("END TEST CASE\n\n");
49+
}
50+
51+
52+
53+
54+
}

src/test/java/junit/com/excilys/cdb/services/ComputerServiceTest.java

Lines changed: 256 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
package 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;
36
import static org.mockito.Mockito.when;
47

58
import com.excilys.cdb.daos.ComputerDao;
9+
import com.excilys.cdb.models.Company;
610
import 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;
714
import com.excilys.cdb.services.ComputerService;
815
import com.excilys.cdb.validators.ValidatorException;
916

1017
import org.easymock.EasyMock;
18+
import org.junit.After;
19+
import org.junit.AfterClass;
1120
import org.junit.Before;
21+
import org.junit.BeforeClass;
1222
import org.junit.Test;
1323
import org.junit.runner.RunWith;
24+
import org.mockito.Mockito;
1425
import org.mockito.MockitoAnnotations;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
1528
import org.springframework.beans.factory.annotation.Autowired;
1629
import org.springframework.test.context.ContextConfiguration;
1730
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1831

32+
import java.time.LocalDateTime;
33+
import java.util.ArrayList;
1934
import java.util.List;
2035

2136
/**
@@ -25,6 +40,9 @@
2540
@ContextConfiguration("classpath:/spring/cdb-context-test.xml")
2641
public 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

Comments
 (0)