Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test')
implementation("org.springframework.boot:spring-boot-starter-jooq")
implementation('org.flywaydb:flyway-core')
runtimeOnly('com.h2database:h2')
}

test {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/wilkins/showcase/controller/JsonPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wilkins.showcase.controller;

import com.wilkins.showcase.domain.Person;

public record JsonPerson(String id,
String title,
String forename,
String surname) {
public static JsonPerson from(Person person) {
return new JsonPerson(person.id(), person.title(), person.forename(), person.surname());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.wilkins.showcase.controller;

import com.wilkins.showcase.service.PersonService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/persons")
public class PersonController {
private final PersonService personService;

public PersonController(PersonService personService) {
this.personService = personService;
}

@GetMapping
public List<JsonPerson> getPersons() {
return personService.findAll()
.stream()
.map(JsonPerson::from)
.toList();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/wilkins/showcase/domain/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wilkins.showcase.domain;

public record Person(String id,
String title,
String forename,
String surname) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wilkins.showcase.service;

import com.wilkins.showcase.domain.Person;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public record DefaultPersonService(PersonRepository personRepository) implements PersonService{
@Override
public List<Person> findAll() {
return personRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wilkins.showcase.service;

import com.wilkins.showcase.domain.Person;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

import static org.jooq.impl.DSL.field;

@Service
public class JooqPersonRepository implements PersonRepository {

private final DSLContext dsl;

public JooqPersonRepository(DataSource dataSource) throws SQLException {
this.dsl = DSL.using(dataSource.getConnection());
}

@Override
public List<Person> findAll() {
return dsl.select(personFields)
.from(personTable)
.fetch(asPerson());
}

private static RecordMapper<Record, Person> asPerson() {
return r -> new Person(r.get(externalId),
r.get(title),
r.get(forename),
r.get(surname));
}

private static final Table personTable = DSL.table("person");
private static final Field<String> externalId = field("person.external_id", String.class);
private static final Field<String> title = field("person.title", String.class);
private static final Field<String> forename = field("person.forename", String.class);
private static final Field<String> surname = field("person.surname", String.class);
private static final Field[] personFields = {
externalId, title, forename, surname
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wilkins.showcase.service;

import com.wilkins.showcase.domain.Person;

import java.util.List;

public interface PersonRepository {
List<Person> findAll();
}
9 changes: 9 additions & 0 deletions src/main/java/com/wilkins/showcase/service/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wilkins.showcase.service;

import com.wilkins.showcase.domain.Person;

import java.util.List;

public interface PersonService {
List<Person> findAll();
}
8 changes: 8 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
h2:
console:
enabled: true
datasource:
url: jdbc:h2:mem:showcase
username: showcase
password: Passw0rd!
12 changes: 12 additions & 0 deletions src/main/resources/db/migration/V0__add_person.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table person(
id bigint not null auto_increment,
external_id varchar(120) not null,
title varchar(120),
forename varchar(120),
surname varchar(120),
constraint person_pk primary key (id),
constraint person_external_id unique (external_id)
);

insert into person(external_id, title, forename, surname)
values ( 'system-user', null, 'system', 'user' );
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest
@WebMvcTest(controllers = GreetingController.class)
public class GreetingControllerTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.wilkins.showcase.controller;

import com.wilkins.showcase.domain.Person;
import com.wilkins.showcase.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = PersonController.class)
public class PersonControllerTest {

@Autowired
MockMvc mockMvc;

@MockBean
PersonService personService;

@Test
void canGetPersons() throws Exception {
var person1 = new Person("ms-lisa-simpson", "Ms", "Lisa", "Simpson");
when(personService.findAll()).thenReturn(List.of(person1));
mockMvc.perform(get("/persons"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is("ms-lisa-simpson")))
.andExpect(jsonPath("$[0].title", is("Ms")))
.andExpect(jsonPath("$[0].forename", is("Lisa")))
.andExpect(jsonPath("$[0].surname", is("Simpson")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wilkins.showcase.service;

import com.wilkins.showcase.domain.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;

@SpringBootTest
public class JooqPersonRepositoryTest {
@Autowired
JooqPersonRepository underTest;

@Test
void canFindPersons() {
assertThat(underTest.findAll())
.extracting(Person::id, Person::title, Person::forename, Person::surname)
.contains(tuple("system-user", null, "system", "user"));
}
}