Skip to content

Commit

Permalink
Merge pull request #11 from btison/incidents-by-name
Browse files Browse the repository at this point in the history
REST Endpoint to search victims by name
  • Loading branch information
btison authored May 2, 2019
2 parents 84e5e39 + c84d335 commit e8a3f94
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public ResponseEntity<Incident> incident(@PathVariable String id) {
}
}

@RequestMapping(value = "/victim/byname/{name}", method = RequestMethod.GET, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Incident>> incidentsByVictimName(@PathVariable String name) {
return new ResponseEntity<>(incidentService.incidentsByName(name), HttpStatus.OK);
}

@RequestMapping(value = "/reset", method = RequestMethod.POST)
public ResponseEntity reset() {
incidentService.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public List<Incident> findByStatus(String status) {
.setParameter("status", status.toUpperCase()).getResultList();
}

@SuppressWarnings("unchecked")
public List<Incident> findByName(String pattern) {
return entityManager.createQuery("SELECT i from Incident i WHERE LOWER(i.victimName) LIKE :pattern")
.setParameter("pattern", pattern.toLowerCase()).getResultList();
}

public void deleteAll() {
Query deleteAll = entityManager.createQuery("DELETE FROM Incident");
deleteAll.executeUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public List<Incident> incidentsByStatus(String status) {

}

@Transactional
public List<Incident> incidentsByName(String name) {

return incidentDao.findByName(name).stream().map(this::to).collect(Collectors.toList());

}

@Transactional
public void reset() {
incidentDao.deleteAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,103 @@ public void testFindReportedIncidentsByStatusEmptyList() {
return null;
});
}

@Test
@Transactional
public void testFindIncidentByName() {
//end the current transaction
TestTransaction.end();

new TransactionTemplate(transactionManager).execute(s -> {
incidentDao.deleteAll();
return null;
});

Incident incident1 = new Incident.Builder()
.incidentId(UUID.randomUUID().toString())
.latitude("30.12345")
.longitude("-70.98765")
.numberOfPeople(3)
.medicalNeeded(false)
.victimName("John Doe")
.victimPhoneNumber("123-456-789")
.status("REPORTED")
.reportedTime(System.currentTimeMillis())
.build();

Incident incident2 = new Incident.Builder()
.incidentId(UUID.randomUUID().toString())
.latitude("35.12345")
.longitude("-75.98765")
.numberOfPeople(4)
.medicalNeeded(false)
.victimName("John Foo")
.victimPhoneNumber("123-456-789")
.status("ASSIGNED")
.reportedTime(System.currentTimeMillis())
.build();


new TransactionTemplate(transactionManager).execute(s -> {
incidentDao.create(incident1);
incidentDao.create(incident2);
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("John Foo");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(1));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("%Foo");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(1));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("%foo");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(1));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("%Fo%");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(1));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("%Foo%");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(1));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("John%");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("john%");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
return null;
});

new TransactionTemplate(transactionManager).execute(s -> {
List<Incident> result = incidentDao.findByName("Peter%");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(0));
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,36 @@ public void testFindIncidentsByStatusEmptyList() {
verify(incidentDao).findByStatus("reported");
}

@Test
public void testFindIncidentsByName() {
com.redhat.cajun.navy.incident.entity.Incident incident1 = new com.redhat.cajun.navy.incident.entity.Incident.Builder(1L, 1L)
.incidentId("incident123")
.victimName("John Doe")
.victimPhoneNumber("111-222-333")
.latitude("30.12345")
.longitude("-77.98765")
.numberOfPeople(2)
.medicalNeeded(true)
.reportedTime(System.currentTimeMillis())
.status(IncidentStatus.REPORTED.name())
.build();

when(incidentDao.findByName("John Doe")).thenReturn(Collections.singletonList(incident1));

List<Incident> incidents = service.incidentsByName("John Doe");
assertThat(incidents, notNullValue());
assertThat(incidents.size(), equalTo(1));
Incident result = incidents.get(0);
assertThat(result.getId(), equalTo("incident123"));
assertThat(result.getVictimName(), equalTo("John Doe"));
assertThat(result.getVictimPhoneNumber(), equalTo("111-222-333"));
assertThat(result.getLat(), equalTo("30.12345"));
assertThat(result.getLon(), equalTo("-77.98765"));
assertThat(result.getNumberOfPeople(), equalTo(2));
assertThat(result.isMedicalNeeded(), equalTo(true));
assertThat(result.getStatus(), equalTo("REPORTED"));

verify(incidentDao).findByName("John Doe");
}

}

0 comments on commit e8a3f94

Please sign in to comment.