Added leftover files

parent 36d1bb70
package no.nsd.websurvey.tromso;
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 java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
* Persists test data for demonstration and tests.
* Populate the embedded H2 database with two {@link no.nsd.websurvey.tromso.domain.Survey} entities
* each containing 100 {@link no.nsd.websurvey.tromso.domain.Participant} entities.
*/
public class InitializeEmbeddedDataBaseData {
public InitializeEmbeddedDataBaseData(SurveyRepository surveyRepository, ParticipantRepository participantRepository) {
Survey surveyOne = new Survey();
Survey surveyTwo = new Survey();
surveyOne = surveyRepository.save(surveyOne);
surveyTwo = surveyRepository.save(surveyTwo);
Set<Participant> participantsOne = new HashSet<>();
for(int i = 0; i < 100; i++) {
Participant p = new Participant(surveyOne, statusCode());
participantRepository.save(p);
participantsOne.add(p);
}
surveyOne.setParticipants(participantsOne);
Set<Participant> participantsTwo = new HashSet<>();
for(int i = 0; i < 100; i++) {
Participant p = new Participant(surveyTwo, statusCode());
participantRepository.save(p);
participantsTwo.add(p);
}
surveyTwo.setParticipants(participantsTwo);
System.out.println(surveyOne);
for(Survey s: surveyRepository.findAll()) {
System.out.println(s);
}
}
/**
* Provide random status codes to provide the correct data
* necessary for the API.
* @return a random {@link java.lang.Integer} between 0 and 3.
*/
private static int statusCode() {
Random rand = new Random();
return rand.nextInt((3)+1);
}
}
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Survey;
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("survey")
@Description("Controller for presenting Surveys.")
public class SurveyController {
@Autowired
private SurveyService surveyService;
@RequestMapping(value = "all", method = RequestMethod.GET)
public List<Survey> getAllSurveys() {
return surveyService.findAll();
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Survey getSurvey(@PathVariable("id") Long id) {
return surveyService.findById(id);
}
}
package no.nsd.websurvey.tromso.domain;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "survey")
public class Survey {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonManagedReference
@OneToMany(fetch = FetchType.EAGER, mappedBy = "survey")
private Set<Participant> participants = new HashSet<>();
public Survey() {
//ORM
}
public Survey(Set<Participant> participants) {
this.participants = participants;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<Participant> getParticipants() {
return participants;
}
public void setParticipants(Set<Participant> participants) {
this.participants = participants;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Survey survey = (Survey) o;
return id.equals(survey.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return "Survey{" +
"id=" + id +
", participants=" + participants +
'}';
}
}
package no.nsd.websurvey.tromso.repository;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface ParticipantRepository extends CrudRepository<Participant, Long> {
public List<Participant> findBySurvey(Survey survey);
public List<Participant> findBySurveyId(Long id);
}
package no.nsd.websurvey.tromso.repository;
import no.nsd.websurvey.tromso.domain.Survey;
import org.springframework.data.repository.CrudRepository;
public interface SurveyRepository extends CrudRepository<Survey, Long> {
}
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.domain.Survey;
import java.util.List;
/**
* Provides access to all {@link no.nsd.websurvey.tromso.domain.Survey} entities
* made available through the persistence layer.
*/
public interface SurveyService {
/**
* Return one persisted {@link no.nsd.websurvey.tromso.domain.Survey} from the database.
* @param id of {@link no.nsd.websurvey.tromso.domain.Survey}
* @return {@link no.nsd.websurvey.tromso.domain.Survey}
*/
public Survey findById(Long id);
/**
* Return every persisted {@link no.nsd.websurvey.tromso.domain.Survey} in the database.
* @return {@link java.util.List} of all {@link no.nsd.websurvey.tromso.domain.Survey} in the database
*/
public List<Survey> findAll();
/**
* Save a {@link no.nsd.websurvey.tromso.domain.Survey} to the database.
* @param survey {@link no.nsd.websurvey.tromso.domain.Survey}
* @return the persisted {@link no.nsd.websurvey.tromso.domain.Survey} entity
*/
public Survey save(Survey survey);
}
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service("surveyService")
public class SurveyServiceImpl implements SurveyService {
@Autowired
private SurveyRepository surveyRepository;
@Override
@Transactional(readOnly = true)
public Survey findById(Long id) {
return surveyRepository.findOne(id);
}
@Override
@Transactional(readOnly = true)
public List<Survey> findAll() {
List<Survey> surveys = new ArrayList<>();
for(Survey survey : surveyRepository.findAll()) {
surveys.add(survey);
}
return surveys;
}
@Override
@Transactional(readOnly = false)
public Survey save(Survey survey) {
return null;
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.it;
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.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.*;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@WebAppConfiguration
@IntegrationTest
public class RestControllerTests {
@Autowired
private SurveyRepository surveyRepository;
@Autowired
private ParticipantRepository participantRepository;
@Before
public void setUp() {
new InitializeEmbeddedDataBaseData(
surveyRepository,
participantRepository
);
}
@Test
public void testGetOneParticipantFromUrl() throws Exception {
Long testId = 1L;
RestTemplate template = new RestTemplate();
ResponseEntity<Participant> patientEntity = template.getForEntity(
"http://localhost:8080/participant/"+testId, Participant.class);
Participant participant = patientEntity.getBody();
assertThat(patientEntity.getStatusCode().toString(), is("200"));
assertNotNull(participant);
assertTrue(participant.getId().equals(testId));
}
@Test
public void testGetOneSurveyFromUrl() throws Exception {
Long testId = 1L;
RestTemplate template = new RestTemplate();
ResponseEntity<Survey> surveyEntity = template.getForEntity(
"http://localhost:8080/survey/"+testId, Survey.class);
Survey survey = surveyEntity.getBody();
assertThat(surveyEntity.getStatusCode().toString(), is("200"));
assertNotNull(survey);
assertThat(survey.getParticipants(), is(notNullValue()));
assertTrue(survey.getId().equals(testId));
}
@Test
public void testGetAllSurveysFromUrl() throws Exception {
RestTemplate template = new RestTemplate();
ResponseEntity<Survey[]> surveysEntity = template.getForEntity(
"http://localhost:8080/survey/all", Survey[].class);
Survey[] surveys = surveysEntity.getBody();
assertThat(surveysEntity.getStatusCode().toString(), is("200"));
for(Survey survey : surveys) {
assertNotNull(survey);
assertThat(survey.getParticipants(), is(notNullValue()));
}
}
@Test
public void testGetAllParticipantsFromUrl() throws Exception {
RestTemplate template = new RestTemplate();
ResponseEntity<Participant[]> surveysEntity = template.getForEntity(
"http://localhost:8080/participant/all", Participant[].class);
Participant[] surveys = surveysEntity.getBody();
assertThat(surveysEntity.getStatusCode().toString(), is("200"));
for(Participant participant : surveys) {
assertNotNull(participant);
}
}
@Test
public void testGetSurveyByIdFromUrl() throws Exception {
RestTemplate template = new RestTemplate();
ResponseEntity<Survey> surveyEntity = template.getForEntity(
"http://localhost:8080/survey/1", Survey.class);
Survey survey = surveyEntity.getBody();
assertThat(surveyEntity.getStatusCode().toString(), is("200"));
for(Participant participant: survey.getParticipants()) {
assertNotNull(participant);
assertThat(participant.getSurvey().getId(), equalTo(survey.getId()));
}
}
/**
* This test does not check for the existence of a {@link no.nsd.websurvey.tromso.domain.Survey}
* entity on either of the returned {@link no.nsd.websurvey.tromso.domain.Participant}. This is
* intended as this is how the service layer JSON return data is designed.
* @throws Exception
*/
@Test
public void testGetParticipantsBySurveyIdFromUrl() throws Exception {
Long surveyId = 1L;
RestTemplate template = new RestTemplate();
ResponseEntity<Participant[]> participantsEntity = template.getForEntity(
"http://localhost:8080/participant/survey/"+surveyId, Participant[].class);
Participant[] participants = participantsEntity.getBody();
assertThat(participantsEntity.getStatusCode().toString(), is("200"));
for(Participant participant : participants) {
assertNotNull(participant);
}
}
}
\ 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.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 SurveyServiceTest {
@Autowired
private SurveyService surveyService;
@Autowired
private SurveyRepository surveyRepository;
@Autowired
private ParticipantRepository participantRepository;
@Before
public void setUp() {
new InitializeEmbeddedDataBaseData(
surveyRepository,
participantRepository
);
}
@Test
public void testFindOneSurveyById() throws Exception {
Long testId = 1L;
Survey survey = surveyService.findById(testId);
assertNotNull(survey);
assertTrue(survey.getId().equals(testId));
}
@Test
public void testFindAllSurveys() throws Exception {
List<Survey> surveys = surveyService.findAll();
assertNotNull(surveys);
}
}
\ 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