Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
#mpa-227 changing lime survey id for running study is now forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kurz committed Mar 25, 2024
1 parent 790286f commit 0f09ce4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

public class LimeSurveyObservation<C extends ObservationProperties> extends Observation<C> {

public static final String LIME_SURVEY_ID = "limeSurveyId";
private final LimeSurveyRequestService limeSurveyRequestService;

public LimeSurveyObservation(MoreObservationSDK sdk, C properties, LimeSurveyRequestService limeSurveyRequestService) throws ConfigurationValidationException {
Expand All @@ -29,9 +30,9 @@ public LimeSurveyObservation(MoreObservationSDK sdk, C properties, LimeSurveyReq

@Override
public void activate(){
String surveyId = checkAndGetSurveyId();

Set<Integer> participantIds = sdk.participantIds(MorePlatformSDK.ParticipantFilter.ALL);
String surveyId = properties.getString("limeSurveyId");
//TODO disable keys fromm removed?
participantIds.removeIf(id -> sdk.getPropertiesForParticipant(id).isPresent());
limeSurveyRequestService.activateParticipants(participantIds, surveyId)
.forEach(data -> {
Expand All @@ -45,6 +46,36 @@ public void activate(){
});
limeSurveyRequestService.setSurveyEndUrl(surveyId, sdk.getStudyId(), sdk.getObservationId());
limeSurveyRequestService.activateSurvey(surveyId);
sdk.setValue(LIME_SURVEY_ID, surveyId);
}

protected String checkAndGetSurveyId() {
String newSurveyId = properties.getString(LIME_SURVEY_ID);
String activeSurveyId = sdk.getValue(LIME_SURVEY_ID, String.class).orElse(null);

if(activeSurveyId != null && !activeSurveyId.equals(newSurveyId)) {
throw new RuntimeException(String.format(
"SurveyId on Observation %s must not be changed: %s -> %s",
sdk.getObservationId(),
activeSurveyId,
newSurveyId
));
} else {
return newSurveyId;
}
}



@Override
public void deactivate() {
// for downwards compatibility (already running studies)
String newSurveyId = properties.getString(LIME_SURVEY_ID);
String activeSurveyId = sdk.getValue(LIME_SURVEY_ID, String.class).orElse(null);

if(activeSurveyId == null || activeSurveyId.equals(newSurveyId)) {
sdk.setValue(LIME_SURVEY_ID, newSurveyId);
}
}

public boolean writeDataPoints(String token, int surveyId, int savedId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,22 @@ private List<ParticipantData> createParticipants(Set<Integer> participantIds, St
);
LOGGER.info("sent {} participants to lime", participantIds.size());

List<ParticipantData> data = mapper.readValue(client.send(request, HttpResponse.BodyHandlers.ofString()).body(),
LimeSurveyParticipantResponse.class)
.result();
String rsp = client.send(request, HttpResponse.BodyHandlers.ofString()).body();

if(rsp.contains("Error: Invalid survey ID")) {
throw new RuntimeException("Invalid survey ID: " + surveyId);
}

List<ParticipantData> data = mapper.readValue(
rsp,
LimeSurveyParticipantResponse.class
).result();

releaseSessionKey(sessionKey);

LOGGER.info("result: {}", data.stream().map(ParticipantData::toString).collect(Collectors.joining()));
return data;
}catch(IOException | InterruptedException e){
} catch( IOException | InterruptedException e){
LOGGER.error("Error creating participants for survey {}", surveyId);
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.redlink.more.studymanager.component.observation.lime;

import io.redlink.more.studymanager.core.properties.ObservationProperties;
import io.redlink.more.studymanager.core.sdk.MoreObservationSDK;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LimeSurveyObservationTest {
@Test
public void testLimeSurveyIdValidation() {
MoreObservationSDK sdk = mock(MoreObservationSDK.class);
ObservationProperties properties = mock(ObservationProperties.class);

when(sdk.getValue(anyString(), any()))
.thenReturn(Optional.empty(), Optional.of("equals"), Optional.of("other"));
when(properties.getString(anyString())).thenReturn("valid", "equals", "different");

LimeSurveyObservation o = new LimeSurveyObservation(sdk, properties, null);
Assertions.assertEquals("valid", o.checkAndGetSurveyId());
Assertions.assertEquals("equals", o.checkAndGetSurveyId());
boolean expectedError = false;
try {
o.checkAndGetSurveyId();
} catch (RuntimeException e) {
expectedError = true;
}

Assertions.assertTrue(expectedError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {

public BadRequestException(String cause) {
super(cause);
}

public BadRequestException(String cause, Throwable throwable) {
super(String.format("%s: %s", cause, throwable.getMessage()), throwable);
}

public BadRequestException(Throwable cause) {
super(cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void setStatus(Long studyId, Study.Status status, User user) {
//ROLLBACK
studyRepository.setStateById(studyId, oldState);
studyRepository.getById(studyId).ifPresent(this::alignWithStudyState);
throw new BadRequestException("Study cannot be initialized");
throw new BadRequestException("Study cannot be initialized",e);
}
});
}
Expand Down

0 comments on commit 0f09ce4

Please sign in to comment.