Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit c848883

Browse files
committed
spring-jpa: added custom exception & HQuery
1 parent 6211ddf commit c848883

File tree

6 files changed

+206
-11
lines changed

6 files changed

+206
-11
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package es.msanchez.frameworks.java.spring.boot.dao;
22

33
import es.msanchez.frameworks.java.spring.boot.entity.Person;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
6+
7+
import java.util.List;
8+
import java.util.Optional;
49

510
public interface PersonDao extends RawDao<Person> {
11+
12+
@Query("SELECT p FROM Person p WHERE p.name = :name")
13+
Optional<Person> findOneByName(@Param("name") final String name);
14+
15+
List<Person> findAllByAge(final Integer age);
16+
617
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package es.msanchez.frameworks.java.spring.boot.exception;
2+
3+
public class DataTransferException extends RuntimeException {
4+
5+
private static final long serialVersionUID = 967831159289792636L;
6+
7+
public DataTransferException(final String message) {
8+
super(message);
9+
}
10+
11+
public DataTransferException(final String message,
12+
final Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
}
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package es.msanchez.frameworks.java.spring.boot.rest;
22

3-
import es.msanchez.frameworks.java.spring.boot.dao.PersonDao;
43
import es.msanchez.frameworks.java.spring.boot.entity.Person;
4+
import es.msanchez.frameworks.java.spring.boot.exception.DataTransferException;
5+
import es.msanchez.frameworks.java.spring.boot.service.PersonService;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
79
import org.springframework.web.bind.annotation.PathVariable;
8-
import org.springframework.web.bind.annotation.PutMapping;
910
import org.springframework.web.bind.annotation.RequestMapping;
1011
import org.springframework.web.bind.annotation.RestController;
1112

@@ -14,21 +15,28 @@
1415
@RequestMapping("/person")
1516
public class PersonRestResource {
1617

17-
private final PersonDao personDao;
18+
private final PersonService personService;
1819

1920
@Autowired
20-
public PersonRestResource(final PersonDao personDao) {
21-
this.personDao = personDao;
21+
public PersonRestResource(final PersonService personService) {
22+
this.personService = personService;
2223
}
2324

24-
@PutMapping("create/{name}")
25-
public String createPerson(@PathVariable("name") final String name) {
26-
log.info("create person called w. name '{}'", name);
25+
@GetMapping("create/{name}")
26+
public void createPerson(@PathVariable("name") final String name) {
27+
if (!this.personService.isValid(name)) {
28+
log.error("The given name '{}' is not valid", name);
29+
throw new DataTransferException("Tried to create a person with a wrong name.");
30+
}
31+
32+
this.personService.save(this.create(name));
33+
log.info("created person with name '{}'", name);
34+
}
35+
36+
private Person create(final String name) {
2737
final Person person = new Person();
2838
person.setName(name);
29-
30-
this.personDao.save(person);
31-
return "created";
39+
return person;
3240
}
3341

3442
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package es.msanchez.frameworks.java.spring.boot.service;
2+
3+
import es.msanchez.frameworks.java.spring.boot.dao.PersonDao;
4+
import es.msanchez.frameworks.java.spring.boot.entity.Person;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Optional;
9+
10+
@Slf4j
11+
@Component
12+
public class PersonService {
13+
14+
private final PersonDao personDao;
15+
16+
public PersonService(final PersonDao personDao) {
17+
this.personDao = personDao;
18+
}
19+
20+
public boolean isValid(final String name) {
21+
final Optional<Person> exists = this.personDao.findOneByName(name);
22+
return !exists.isPresent();
23+
}
24+
25+
public void save(final Person person) {
26+
this.personDao.save(person);
27+
}
28+
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package es.msanchez.frameworks.java.spring.boot.rest;
2+
3+
import com.googlecode.catchexception.apis.BDDCatchException;
4+
import es.msanchez.frameworks.java.spring.boot.entity.Person;
5+
import es.msanchez.frameworks.java.spring.boot.exception.DataTransferException;
6+
import es.msanchez.frameworks.java.spring.boot.service.PersonService;
7+
import org.assertj.core.api.BDDAssertions;
8+
import org.mockito.BDDMockito;
9+
import org.mockito.Mockito;
10+
import org.testng.annotations.BeforeMethod;
11+
import org.testng.annotations.Test;
12+
13+
public class PersonRestResourceTest {
14+
15+
private PersonRestResource restResource;
16+
17+
private PersonService serviceMock;
18+
19+
@BeforeMethod
20+
public void setUp() {
21+
this.serviceMock = Mockito.mock(PersonService.class);
22+
23+
this.restResource = new PersonRestResource(this.serviceMock);
24+
}
25+
26+
@Test
27+
public void testCreatePerson() {
28+
// @GIVEN
29+
final String name = "mario";
30+
31+
final boolean isValid = true;
32+
BDDMockito.given(this.serviceMock.isValid(name))
33+
.willReturn(isValid);
34+
35+
// @WHEN
36+
this.restResource.createPerson(name);
37+
38+
// @THEN
39+
BDDMockito.verify(this.serviceMock).isValid(name);
40+
BDDMockito.verify(this.serviceMock).save(Mockito.any(Person.class));
41+
}
42+
43+
@Test
44+
public void testCreatePersonCaseNameNotValid() {
45+
// @GIVEN
46+
final String name = "mario";
47+
48+
final boolean isValid = false;
49+
BDDMockito.given(this.serviceMock.isValid(name))
50+
.willReturn(isValid);
51+
52+
// @WHEN
53+
BDDCatchException.when(this.restResource).createPerson(name);
54+
55+
// @THEN
56+
BDDMockito.verify(this.serviceMock).isValid(name);
57+
BDDMockito.verify(this.serviceMock, Mockito.never()).save(Mockito.any(Person.class));
58+
BDDAssertions.assertThat(BDDCatchException.caughtException())
59+
.isInstanceOf(DataTransferException.class);
60+
}
61+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package es.msanchez.frameworks.java.spring.boot.service;
2+
3+
import es.msanchez.frameworks.java.spring.boot.dao.PersonDao;
4+
import es.msanchez.frameworks.java.spring.boot.entity.Person;
5+
import org.assertj.core.api.BDDAssertions;
6+
import org.mockito.BDDMockito;
7+
import org.mockito.Mockito;
8+
import org.testng.annotations.BeforeMethod;
9+
import org.testng.annotations.Test;
10+
11+
import java.util.Optional;
12+
13+
public class PersonServiceTest {
14+
15+
private PersonService service;
16+
17+
private PersonDao daoMock;
18+
19+
@BeforeMethod
20+
public void setUp() {
21+
this.daoMock = Mockito.mock(PersonDao.class);
22+
23+
this.service = new PersonService(this.daoMock);
24+
}
25+
26+
@Test
27+
public void testIsValidName() {
28+
// @GIVEN
29+
final String name = "mario";
30+
31+
BDDMockito.given(this.daoMock.findOneByName(name))
32+
.willReturn(Optional.empty());
33+
34+
// @WHEN
35+
final boolean isValid = this.service.isValid(name);
36+
37+
// @THEN
38+
BDDMockito.verify(this.daoMock).findOneByName(name);
39+
BDDAssertions.assertThat(isValid).isTrue();
40+
}
41+
42+
@Test
43+
public void testIsValidNameCaseFalse() {
44+
// @GIVEN
45+
final String name = "mario";
46+
47+
BDDMockito.given(this.daoMock.findOneByName(name))
48+
.willReturn(Optional.of(new Person()));
49+
50+
// @WHEN
51+
final boolean isValid = this.service.isValid(name);
52+
53+
// @THEN
54+
BDDMockito.verify(this.daoMock).findOneByName(name);
55+
BDDAssertions.assertThat(isValid).isFalse();
56+
}
57+
58+
@Test
59+
public void testSave() {
60+
// @GIVEN
61+
final Person person = new Person();
62+
person.setName("mario");
63+
64+
// @WHEN
65+
this.service.save(person);
66+
67+
// @THEN
68+
BDDMockito.verify(this.daoMock).save(person);
69+
}
70+
}

0 commit comments

Comments
 (0)