New ids, simpler url, new query.

As specified by client.
parent 8c32856e
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
......@@ -10,6 +9,8 @@ apply plugin: 'war'
//}
war {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
baseName = 'wst'
// version = '0.0.1'
}
......@@ -42,8 +43,6 @@ dependencies {
"com.h2database:h2"
)
// providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile(
"junit:junit",
"org.springframework:spring-test"
......@@ -51,5 +50,5 @@ dependencies {
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
gradleVersion = '2.2.1'
}
\ No newline at end of file
......@@ -7,9 +7,8 @@ import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.io.*;
import java.util.*;
/**
* Persists test data for demonstration and tests.
......@@ -23,31 +22,42 @@ public class InitializeEmbeddedDataBaseData {
public InitializeEmbeddedDataBaseData(SurveyRepository surveyRepository, ParticipantRepository participantRepository) {
logger.info("Creating entities and populating H2 embedded datasource");
Survey surveyOne = new Survey();
Survey surveyTwo = new Survey();
surveyOne = surveyRepository.save(surveyOne);
surveyTwo = surveyRepository.save(surveyTwo);
Survey surveyOne = surveyRepository.save(new Survey());
Survey surveyTwo = surveyRepository.save(new Survey());
Set<Participant> participantsOne = new HashSet<>();
for(int i = 0; i < 100; i++) {
Participant p = new Participant(surveyOne, statusCode());
generateIdsFromFile().forEach(i -> {
final Participant p = new Participant(i, statusCode(), surveyOne);
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());
generateIdsFromFile().forEach(i -> {
Participant p = new Participant(i, statusCode(), surveyTwo);
participantRepository.save(p);
participantsTwo.add(p);
});
}
private List<Integer> generateIdsFromFile() {
List<Integer> list = new ArrayList<>();
try(
InputStreamReader fileReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("ids"));
BufferedReader bufferedReader = new BufferedReader(fileReader)
) {
String s;
while((s = bufferedReader.readLine()) != null) {
list.add(Integer.parseInt(s));
}
} catch (IOException e) {
e.printStackTrace();
}
surveyTwo.setParticipants(participantsTwo);
logger.info("Size participantsOne: " + participantsOne.size());
logger.info("Size participantsTwo: " + participantsTwo.size());
return list;
}
/**
* Provide random status codes to provide the correct data
* necessary for the API.
......
package no.nsd.websurvey.tromso.config;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Simple filter to enable CORS for requests.
* Currently only GET is implemented, the application is
* only supposed to send headers for GET.
*/
@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
......@@ -21,8 +21,16 @@ public class QueryController {
@RequestMapping(value = "/{surveyId}/participant/{participantId}")
public Participant getParticipantFromSurvey(
@PathVariable("surveyId") Long surveyId,
@PathVariable("participantId") Long participantId) {
@PathVariable("participantId") int remoteId) {
return participantService.findByIdAndSurveyId(participantId, surveyId);
return participantService.findByRemoteIdAndSurveyId(remoteId, surveyId);
}
@RequestMapping(value = "/{surveyId}/{participantId}")
public Participant getParticipantFromSurveyTwo(
@PathVariable("surveyId") Long surveyId,
@PathVariable("participantId") int remoteId) {
return participantService.findByRemoteIdAndSurveyId(remoteId, surveyId);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.domain;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
......@@ -18,7 +17,10 @@ public class Participant {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "statusCode")
@Column(name = "remoteid")
private Integer remoteId;
@Column(name = "statuscode")
private int statusCode;
@JsonBackReference
......@@ -26,8 +28,20 @@ public class Participant {
@JoinColumn(name = "survey_id")
private Survey survey;
public Participant() {
//ORM.
public Participant() {}
public Participant(Integer remoteId, int statusCode, Survey survey) {
this.remoteId = remoteId;
this.statusCode = statusCode;
this.survey = survey;
}
public Integer getRemoteId() {
return remoteId;
}
public void setRemoteId(Integer remoteId) {
this.remoteId = remoteId;
}
public Participant(int statusCode) {
......@@ -63,30 +77,34 @@ public class Participant {
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Participant that = (Participant) o;
if (statusCode != that.statusCode) return false;
if (!id.equals(that.id)) return false;
if (!remoteId.equals(that.remoteId)) return false;
if (survey != null ? !survey.equals(that.survey) : that.survey != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + statusCode;
result = 31 * result + (survey != null ? survey.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Participant{" +
"id=" + id +
", remoteId=" + remoteId +
", statusCode=" + statusCode +
'}';
}
......
......@@ -13,4 +13,9 @@ public interface ParticipantRepository extends CrudRepository<Participant, Long>
public List<Participant> findBySurveyId(Long id);
public Participant findByIdAndSurveyId(Long id, Long surveyId);
public List<Participant> findByStatusCodeNot(int statusCode);
public Participant findByRemoteIdAndSurveyId(int remoteId, Long surveyId);
}
......@@ -51,5 +51,15 @@ public interface ParticipantService {
* @return the resulting {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public Participant findByIdAndSurveyId(Long id, Long surveyId);
/**
* Return a specific participant based on his remote ID and survey id.
* @param remoteId id given from remote system
* @param surveyId id for the survey participant data should come from
* @return
*/
public Participant findByRemoteIdAndSurveyId(int remoteId, Long surveyId);
public List<Participant> findByStatusCodeNotZero();
}
......@@ -46,6 +46,16 @@ public class ParticipantServiceImpl implements ParticipantService {
return participantRepository.findByIdAndSurveyId(id, surveyId);
}
@Override
public Participant findByRemoteIdAndSurveyId(int remoteId, Long surveyId) {
return participantRepository.findByRemoteIdAndSurveyId(remoteId, surveyId);
}
@Override
public List<Participant> findByStatusCodeNotZero() {
return toList(participantRepository.findByStatusCodeNot(0));
}
/**
* 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.
......
logging.level.org.springframework=INFO
## H2
spring.datasource.url=jdbc:h2:mem:ap;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=ap
spring.datasource.password=ap
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
\ No newline at end of file
90000312
90077629
90133420
90277631
90530522
90572730
90622423
90626831
90717024
90944733
91070219
91327123
91485027
91561123
91605021
91858132
92057427
92113925
92197331
92337327
92500016
92513323
92544731
92582632
92589639
92640526
92830123
92894638
92902729
92926331
92932732
92947637
93003318
93079331
93082426
93124928
93192933
93212017
93322120
93334931
\ No newline at end of file
......@@ -17,7 +17,9 @@ import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
......@@ -93,4 +95,23 @@ public class ParticipantServiceTest {
assertTrue(participant.getId().equals(participantId));
assertTrue(participant.getSurvey().getId().equals(surveyId));
}
@Test
public void testFindByStatusCodeNotZero() throws Exception {
List<Participant> participants = participantService.findByStatusCodeNotZero();
assertNotNull(participants);
for(Participant p : participants) {
assertThat(p.getStatusCode(), not(0));
}
}
@Test
public void testFindByRemoteIdAndSurveyId() throws Exception {
Participant participant = participantService.findByRemoteIdAndSurveyId(91561123, (long)1);
assertNotNull(participant.getRemoteId());
System.out.println(participant);
}
}
\ 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