Skip to content

Commit

Permalink
New methods retrieveCurrentSessions + retrievePersonsForSession + bea…
Browse files Browse the repository at this point in the history
…ns + integration tests
  • Loading branch information
KarlLevik committed Feb 14, 2022
1 parent 7303e0c commit a16c64a
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/uk/ac/diamond/ispyb/api/IspybSpyCatApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface IspybSpyCatApi extends Closeable {
public List<String> retrieveVisitsForUser(String beamline, String username, Integer tolerance);
public Optional<String> retrieveLatestVisitWithPrefix(String beamline, String proposalCode);
public Optional<String> retrieveTitleForProposal(String proposalCode, Integer proposalNumber);
public List<Session> retrieveCurrentSessions(String beamline, Integer toleranceMinutes);
public List<SessionPerson> retrievePersonsForSession(String proposalCode, Integer proposalNumber, Integer visitNumber);
}
52 changes: 52 additions & 0 deletions src/main/java/uk/ac/diamond/ispyb/api/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.ac.diamond.ispyb.api;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import java.sql.Timestamp;

public class Session {
private String session;
private Timestamp startDate;
private Timestamp endDate;

public String getSession() {
return session;
}

public void setSession(String session) {
this.session = session;
}

public Timestamp getStartDate() {
return startDate;
}

public void setStartDate(Timestamp startDate) {
this.startDate = startDate;
}

public Timestamp getEndDate() {
return endDate;
}

public void setEndDate(Timestamp endDate) {
this.endDate = endDate;
}

@Override
public String toString(){
return ReflectionToStringBuilder.toString(this);
}

@Override
public int hashCode(){
return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public boolean equals(Object that){
return EqualsBuilder.reflectionEquals(this, that);
}
}
77 changes: 77 additions & 0 deletions src/main/java/uk/ac/diamond/ispyb/api/SessionPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package uk.ac.diamond.ispyb.api;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public class SessionPerson {
private String title;
private String givenName;
private String familyName;
private String login;
private String role;
private Integer remote;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getGivenName() {
return givenName;
}

public void setGivenName(String givenName) {
this.givenName = givenName;
}

public String getFamilyName() {
return familyName;
}

public void setFamilyName(String familyName) {
this.familyName = familyName;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public Integer getRemote() {
return remote;
}

public void setRemote(Integer remote) {
this.remote = remote;
}

@Override
public String toString(){
return ReflectionToStringBuilder.toString(this);
}

@Override
public int hashCode(){
return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public boolean equals(Object that){
return EqualsBuilder.reflectionEquals(this, that);
}
}
20 changes: 20 additions & 0 deletions src/main/java/uk/ac/diamond/ispyb/dao/IspybSpyCatDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import uk.ac.diamond.ispyb.api.Session;
import uk.ac.diamond.ispyb.api.SessionPerson;

public class IspybSpyCatDAO implements IspybSpyCatApi {

Expand Down Expand Up @@ -44,6 +46,24 @@ public Optional<String> retrieveTitleForProposal(String proposalCode, Integer pr
return templateWrapper.callIspybFunction("retrieve_proposal_title_v2", String.class, params);
}

@Override
public List<Session> retrieveCurrentSessions(String beamline, Integer toleranceMinutes) {
Map<String, Object> params = new HashMap<>();
params.put("beamline", beamline);
params.put("tolerance_minutes", toleranceMinutes);
return templateWrapper.callIspybForListBeans("retrieve_current_sessions", Session.class, params);
}

@Override
public List<SessionPerson> retrievePersonsForSession(String proposalCode, Integer proposalNumber, Integer visitNumber) {
Map<String, Object> params = new HashMap<>();
params.put("proposal_code", proposalCode);
params.put("proposal_number", proposalNumber);
params.put("visit_number", visitNumber);
return templateWrapper.callIspybForListBeans("retrieve_persons_for_session", SessionPerson.class, params);
}


@Override
public void close() throws IOException {
try {
Expand Down
60 changes: 52 additions & 8 deletions src/test/java/uk/ac/diamond/ispyb/test/SpyCatIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package uk.ac.diamond.ispyb.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import uk.ac.diamond.ispyb.api.IspybSpyCatApi;
import uk.ac.diamond.ispyb.dao.IspybSpyCatDaoFactory;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import uk.ac.diamond.ispyb.api.IspybSpyCatApi;
import uk.ac.diamond.ispyb.api.Session;
import uk.ac.diamond.ispyb.api.SessionPerson;
import uk.ac.diamond.ispyb.dao.IspybSpyCatDaoFactory;

public class SpyCatIntegrationTest {
private final static IntegrationTestHelper<IspybSpyCatApi> helper = new IntegrationTestHelper<>(new IspybSpyCatDaoFactory());

Expand All @@ -35,18 +41,56 @@ public void testRetrieveVisitsForUser() throws SQLException, IOException, Interr
}

@Test
public void testRetrieveLatestVisitWithPrefix() throws SQLException, IOException, InterruptedException {
public void testRetrieveLatestVisitWithPrefix() throws SQLException, IOException, InterruptedException {
String visit = helper.execute(api -> api.retrieveLatestVisitWithPrefix("i02-2", "cm")).get();
String expected = "cm14451-99";

assertThat(visit, is(equalTo(expected)));
}

@Test
public void testRetrieveTitleForProposal() throws SQLException, IOException, InterruptedException{
public void testRetrieveTitleForProposal() throws SQLException, IOException, InterruptedException{
String title = helper.execute(api -> api.retrieveTitleForProposal("cm", 1)).get();
String expected = "Test Proposal cm-0001";

assertThat(title, is(equalTo(expected)));
}

@Test
public void retrieveCurrentSessions() throws SQLException, IOException, InterruptedException {
List<Session> sessions = helper.execute(api -> api.retrieveCurrentSessions("i03", 26280000));
String expected = "cm14451-1";

String foundSession = "";
Iterator<Session> it = sessions.iterator();
while (it.hasNext()) {
Session s = it.next();
if (s.getSession().equals(expected)) {
foundSession = s.getSession();
break;
}
}

assertThat(foundSession, is(equalTo(expected)));
}

@Test
public void testRetrievePersonsForSession() throws SQLException, IOException, InterruptedException {
List<SessionPerson> persons = helper.execute(api -> api.retrievePersonsForSession("cm", 14451, 1));
String expected = "boaty";

String foundLogin = "";
Iterator<SessionPerson> it = persons.iterator();
while (it.hasNext()) {
SessionPerson p = it.next();
if (p.getLogin().equals(expected)) {
foundLogin = p.getLogin();
break;
}
}

assertThat(foundLogin, is(equalTo(expected)));
}


}

0 comments on commit a16c64a

Please sign in to comment.