Added a service/repository layer with an embedded data source and wrote a few tests.

parent 6de4e121
......@@ -35,7 +35,11 @@ configurations {
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile(
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-data-jpa",
"com.h2database:h2"
)
testCompile(
"junit:junit",
......
package no.nsd.websurvey.tromso;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* Primary configuration class, and also the entry point for local
......@@ -16,7 +21,22 @@ import org.springframework.context.annotation.Configuration;
@ComponentScan
public class WebSurveyTromso extends SpringBootServletInitializer {
@Bean
public HttpMessageConverter httpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setPrefixJson(false);
converter.setPrettyPrint(true);
return converter;
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(WebSurveyTromso.class);
//Initialize the data for the application.
new InitializeEmbeddedDataBaseData(
ctx.getBean(SurveyRepository.class),
ctx.getBean(ParticipantRepository.class)
);
}
}
\ No newline at end of file
......@@ -2,27 +2,39 @@ package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.service.ParticipantService;
import no.nsd.websurvey.tromso.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/participant")
@Description("Controller for presenting Participants.")
public class ParticipantController {
@Autowired
private ParticipantService participantService;
/**
* Return an instance of {@link no.nsd.websurvey.tromso.domain.Participant} based on
* parameters from the url
* @param id for the {@link no.nsd.websurvey.tromso.domain.Participant} that should be presented
* @return {@link no.nsd.websurvey.tromso.domain.Participant}
*/
@Autowired
private SurveyService surveyService;
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Participant getPatient(@PathVariable("id") Long id) {
public Participant getParticipant(@PathVariable("id") Long id) {
return participantService.findById(id);
}
@RequestMapping(value = "all", method = RequestMethod.GET)
public List<Participant> getAllParticipants() {
return participantService.findAll();
}
@RequestMapping(value = "survey/{id}", method = RequestMethod.GET)
public List<Participant> getParticipantsFromSurveyId(@PathVariable("id") Long id) {
return participantService.findBySurveyId(id);
}
}
\ No newline at end of file
......@@ -13,9 +13,10 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping
public class WSTErrorController implements ErrorController {
@RequestMapping(value = "error", method = RequestMethod.GET)
public String error() {
return "Unknown resource. Valid resource is /participant/{id}";
return "Unknown resource or path";
}
/**
......
package no.nsd.websurvey.tromso.domain;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
/**
* Dummy Participant implementation class.
* Not currently connected to any repositories or
* services to provide actual data.
*/
@Entity
@Table(name = "participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "statusCode")
private int statusCode;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "survey_id")
private Survey survey;
public Participant() {
//ORM.
}
public Participant(Long id, int statusCode) {
this.id = id;
public Participant(int statusCode) {
this.statusCode = statusCode;
}
public Participant(Survey survey, int statusCode) {
this.survey = survey;
this.statusCode = statusCode;
}
......@@ -35,25 +55,34 @@ public class Participant {
this.statusCode = statusCode;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Participant participant = (Participant) o;
return statusCode == participant.statusCode && !
(id != null ? !id.equals(participant.id) : participant.id != null);
public Survey getSurvey() {
return survey;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + statusCode;
return result;
public void setSurvey(Survey survey) {
this.survey = survey;
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
//
// Participant that = (Participant) o;
//
// if (id != null ? !id.equals(that.id) : that.id != null) return false;
// if (survey != null ? !survey.equals(that.survey) : that.survey != null) return false;
//
// return true;
// }
//
// @Override
// public int hashCode() {
// int result = id != null ? id.hashCode() : 0;
// result = 31 * result + (survey != null ? survey.hashCode() : 0);
// return result;
// }
@Override
public String toString() {
return "Participant{" +
......
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import java.util.List;
/**
* Provides access to all {@link no.nsd.websurvey.tromso.domain.Participant} entities
* made available through the persistence layer.
*/
public interface ParticipantService {
/**
......@@ -10,5 +17,30 @@ public interface ParticipantService {
* @return a poulated {@link no.nsd.websurvey.tromso.domain.Participant} object.
*/
public Participant findById(Long id);
/**
* Find and return all {@link no.nsd.websurvey.tromso.domain.Participant} from the database.
* This method fetch and maps every entity in the database and will
* become slower the more data available. {@link org.springframework.data.repository.PagingAndSortingRepository}
* should be considered in such cases.
* @return {@link java.util.List} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public List<Participant> findAll();
/**
* Find and return all {@link no.nsd.websurvey.tromso.domain.Participant} entities belonging to
* the given {@link no.nsd.websurvey.tromso.domain.Survey}.
* @param survey {@link no.nsd.websurvey.tromso.domain.Survey} to query
* @return {@link java.util.List} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public List<Participant> findBySurvey(Survey survey);
/**
* Find and return all {@link no.nsd.websurvey.tromso.domain.Participant} entities belonging to
* the given {@link java.lang.Long} id for the {@link no.nsd.websurvey.tromso.domain.Survey}
* @param id {@link java.lang.Long} of the {@link no.nsd.websurvey.tromso.domain.Survey} to query
* @return {@link java.util.List} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public List<Participant> findBySurveyId(Long id);
}
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
@Service("patientService")
public class ParticipantServiceImpl implements ParticipantService {
@Autowired
private ParticipantRepository participantRepository;
@Override
@Transactional(readOnly = true)
public Participant findById(Long id) {
if(id == 1) {
return new Participant(id, 0);
}
return new Participant(id, statusCode());
return participantRepository.findOne(id);
}
@Override
@Transactional(readOnly = true)
public List<Participant> findAll() {
return toList(participantRepository.findAll());
}
@Override
@Transactional(readOnly = true)
public List<Participant> findBySurvey(Survey survey) {
return toList(participantRepository.findBySurvey(survey));
}
@Override
@Transactional(readOnly = true)
public List<Participant> findBySurveyId(Long id) {
return toList(participantRepository.findBySurveyId(id));
}
/**
* Provide random return codes for testing purposes.
* @return a random {@link java.lang.Integer} between 1 and 3.
* Transform results in a {@link java.lang.Iterable} into an {@link java.util.ArrayList}
* @param iterable {@link java.lang.Iterable} containg {@link no.nsd.websurvey.tromso.domain.Participant} objects.
* @return an {@link java.util.ArrayList} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
private int statusCode() {
Random rand = new Random();
return rand.nextInt((3 - 1) +1) + 1;
private List<Participant> toList(Iterable<Participant> iterable) {
List<Participant> participants = new ArrayList<>();
for(Participant participant : iterable) {
participants.add(participant);
}
return participants;
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso;
import no.nsd.websurvey.tromso.it.ParticipantControllerIntegrationTest;
import no.nsd.websurvey.tromso.it.RestControllerTests;
import no.nsd.websurvey.tromso.service.ParticipantServiceTest;
import no.nsd.websurvey.tromso.service.SurveyServiceTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.transaction.annotation.Transactional;
/**
* Test suit will currently lets each class set up and populated the H2
* embedded data source every time a class finish running its tests.
*/
@RunWith(Suite.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@Suite.SuiteClasses({
ParticipantControllerIntegrationTest.class,
ParticipantServiceTest.class
RestControllerTests.class,
ParticipantServiceTest.class,
SurveyServiceTest.class
})
@Transactional
public class WebSurveyTromsoTestSuite {
}
package no.nsd.websurvey.tromso.it;
import no.nsd.websurvey.tromso.WebSurveyTromso;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.service.ParticipantService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@WebAppConfiguration
@IntegrationTest
public class ParticipantControllerIntegrationTest {
@Autowired
private ParticipantService participantService;
@Test()
public void participantFromUrl() throws Exception {
Long testId = 10L;
RestTemplate template = new RestTemplate();
ResponseEntity<Participant> patientEntity = template.getForEntity(
"http://localhost:8080/participant/"+testId, Participant.class);
Participant participant = patientEntity.getBody();
assertNotNull(participant);
assertTrue(participant.getId().equals(testId));
assertThat(patientEntity.getStatusCode().toString(), is("200"));
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.InitializeEmbeddedDataBaseData;
import no.nsd.websurvey.tromso.WebSurveyTromso;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@IntegrationTest
public class ParticipantServiceTest {
@Autowired
private ParticipantService participantService;
@Autowired
private SurveyService surveyService;
@Autowired
private SurveyRepository surveyRepository;
@Autowired
private ParticipantRepository participantRepository;
@Before
public void setUp() {
new InitializeEmbeddedDataBaseData(
surveyRepository,
participantRepository
);
}
@Test()
public void findOne() throws Exception {
Long testId = 10L;
@Test
public void testFindOneParticipantById() throws Exception {
Long testId = 1L;
Participant participant = participantService.findById(testId);
assertNotNull(participant);
assertTrue(participant.getId().equals(testId));
}
@Test
public void testFindAllParticipants() throws Exception {
List<Participant> participants = participantService.findAll();
assertNotNull(participants);
}
@Test
public void testFindParticipantsBySurvey() throws Exception {
Long surveyId = 1L;
Survey survey = surveyService.findById(surveyId);
List<Participant> participants = participantService.findBySurvey(survey);
assertNotNull(participants);
for(Participant participant : participants) {
assertTrue(participant.getSurvey().getId().equals(surveyId));
}
}
@Test
public void testFindParticipantsBySurveyId() throws Exception {
Long surveyId = 1L;
List<Participant> participants = participantService.findBySurveyId(surveyId);
assertNotNull(participants);
for(Participant participant : participants) {
assertTrue(participant.getSurvey().getId().equals(surveyId));
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment