Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questionnaire completion log #118

Merged
merged 16 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ targetCompatibility = '1.8'
sourceCompatibility = '1.8'

ext.grizzly = '2.4.3'
ext.jersey = '2.26'
ext.jersey = '2.27'
ext.junitVersion = '4.12'
ext.hamcrestVersion = '1.3'
ext.logback = '1.2.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.radarcns.integration.util;

import static java.time.temporal.ChronoUnit.SECONDS;
import static org.radarcns.mongo.data.monitor.ApplicationStatusRecordCounter.RECORD_COLLECTION;
import static org.radarcns.mongo.data.monitor.ApplicationStatusServerStatus.STATUS_COLLECTION;
import static org.radarcns.mongo.data.monitor.ApplicationStatusUpTime.UPTIME_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusRecordCounter.RECORD_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusServerStatus.STATUS_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusUpTime.UPTIME_COLLECTION;
import static org.radarcns.mongo.util.MongoHelper.ID;
import static org.radarcns.mongo.util.MongoHelper.KEY;
import static org.radarcns.mongo.util.MongoHelper.PROJECT_ID;
Expand Down Expand Up @@ -139,6 +139,28 @@ private static Map<String, Object> getBoth(ObservationKey key,
return map;
}

/**
* Generates and returns a randomly generated
* {@link org.radarcns.domain.restapi.monitor.QuestionnaireCompletionStatus} mock data
* sent by RADAR-CNS aRMT.
**/
public static Document getRandomQuestionnaireCompletionLog(String project,
String user, String source) {

ThreadLocalRandom random = ThreadLocalRandom.current();

double timestamp = random.nextDouble();
double completionPercentage = random.nextDouble(0d,100d);
String name = "PHQ8";
Document completionDoc = new Document()
.append("time", timestamp)
.append("name", name)
.append("completionPercentage", completionPercentage);

return buildDocumentWithObservationKey(project, user, source, completionDoc);

}

/**
* Generates and returns a randomly generated {@code ApplicationStatus} mocking data sent by
* RADAR-CNS pRMT.
Expand Down Expand Up @@ -184,9 +206,12 @@ public static Map<String, Document> getRandomApplicationStatus(String project, S
.append("recordsUnsent", recordsUnsent);

Map<String, Document> documents = new HashMap<>();
documents.put(STATUS_COLLECTION, buildAppStatusDocument(project, user, source, statusDoc));
documents.put(RECORD_COLLECTION, buildAppStatusDocument(project, user, source, recordsDoc));
documents.put(UPTIME_COLLECTION, buildAppStatusDocument(project, user, source, uptimeDoc));
documents.put(STATUS_COLLECTION,
buildDocumentWithObservationKey(project, user, source, statusDoc));
documents.put(RECORD_COLLECTION,
buildDocumentWithObservationKey(project, user, source, recordsDoc));
documents.put(UPTIME_COLLECTION,
buildDocumentWithObservationKey(project, user, source, uptimeDoc));
return documents;
}

Expand All @@ -197,7 +222,7 @@ private static Document buildKeyDocument(String projectName, String subjectId,
.append(SOURCE_ID, sourceId);
}

private static Document buildAppStatusDocument(String projectName, String subjectId,
private static Document buildDocumentWithObservationKey(String projectName, String subjectId,
String sourceId,
Document value) {
return new Document().append(ID, "{"
Expand Down
58 changes: 37 additions & 21 deletions src/integrationTest/java/org/radarcns/integration/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,51 @@

package org.radarcns.integration.util;

import static org.radarcns.mongo.data.monitor.ApplicationStatusRecordCounter.RECORD_COLLECTION;
import static org.radarcns.mongo.data.monitor.ApplicationStatusServerStatus.STATUS_COLLECTION;
import static org.radarcns.mongo.data.monitor.ApplicationStatusUpTime.UPTIME_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusRecordCounter.RECORD_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusServerStatus.STATUS_COLLECTION;
import static org.radarcns.mongo.data.monitor.application.ApplicationStatusUpTime.UPTIME_COLLECTION;
import static org.radarcns.mongo.util.MongoHelper.VALUE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.radarcns.domain.restapi.Application;
import org.radarcns.domain.restapi.dataset.DataItem;
import org.radarcns.domain.restapi.dataset.Dataset;
import org.radarcns.domain.restapi.format.Acceleration;
import org.radarcns.domain.restapi.header.Header;
import org.radarcns.domain.restapi.header.TimeFrame;
import org.radarcns.domain.restapi.monitor.ApplicationStatus;
import org.radarcns.domain.restapi.monitor.QuestionnaireCompletionStatus;
import org.radarcns.util.RadarConverter;

public class Utility {
/**
* Converts Bson Document into an ApplicationConfig.
* Converts Bson Document into an QuestionnaireCompletionStatus.
*
* @param documents map containing variables to create the ApplicationConfig class
* @param document Document data to create the QuestionnaireCompletionStatus class
* @return an ApplicationConfig class
* @see Application
* @see ApplicationStatus
*/
//TODO take field names from RADAR Mongo Connector
public static Application convertDocToApplication(Map<String, Document> documents) {
return new Application(
public static QuestionnaireCompletionStatus convertDocToQuestionnaireCompletionStatus(Document
document) {
return new QuestionnaireCompletionStatus(
((Document)document.get(VALUE)).getDouble("time"),
((Document)document.get(VALUE)).getString("name"),
((Document)document.get(VALUE)).getDouble("completionPercentage"));
}

/**
* Converts Bson Document into an ApplicationStatus.
*
* @param documents map containing variables to create the ApplicationStatus class
* @return an ApplicationStatus class
* @see ApplicationStatus
*/
public static ApplicationStatus convertDocToApplicationStatus(Map<String, Document> documents) {
return new ApplicationStatus(
((Document) documents.get(STATUS_COLLECTION).get(VALUE)).getString("clientIP"),
((Document) documents.get(UPTIME_COLLECTION).get(VALUE)).getDouble("uptime"),
RadarConverter.getServerStatus(
Expand All @@ -52,8 +69,8 @@ public static Application convertDocToApplication(Map<String, Document> document
((Document) documents.get(RECORD_COLLECTION).get(VALUE))
.getInteger("recordsCached"),
((Document) documents.get(RECORD_COLLECTION).get(VALUE)).getInteger("recordsSent"),
((Document) documents.get(RECORD_COLLECTION).get(VALUE)).getInteger("recordsUnsent")
);
((Document) documents.get(RECORD_COLLECTION).get(VALUE))
.getInteger("recordsUnsent"));
}

/**
Expand All @@ -64,17 +81,16 @@ public static Application convertDocToApplication(Map<String, Document> document
*/
public static Dataset cloneDataset(Dataset input) {
Header inputHeader = input.getHeader();
TimeFrame cloneEffectiveTimeFrame = new TimeFrame(
inputHeader.getEffectiveTimeFrame().getStartDateTime(),
inputHeader.getEffectiveTimeFrame().getEndDateTime());
TimeFrame cloneTimeFrame = new TimeFrame(
inputHeader.getTimeFrame().getStartDateTime(),
TimeFrame cloneEffectiveTimeFrame =
new TimeFrame(inputHeader.getEffectiveTimeFrame().getStartDateTime(),
inputHeader.getEffectiveTimeFrame().getEndDateTime());
TimeFrame cloneTimeFrame = new TimeFrame(inputHeader.getTimeFrame().getStartDateTime(),
inputHeader.getTimeFrame().getEndDateTime());
Header cloneHeader = new Header(inputHeader.getProjectId(), inputHeader.getSubjectId(),
inputHeader.getSourceId(), inputHeader.getSourceType(),
inputHeader.getSourceDataType(),
inputHeader.getDescriptiveStatistic(), inputHeader.getUnit(),
inputHeader.getTimeWindow(), cloneTimeFrame, cloneEffectiveTimeFrame);
inputHeader.getSourceDataType(), inputHeader.getDescriptiveStatistic(),
inputHeader.getUnit(), inputHeader.getTimeWindow(), cloneTimeFrame,
cloneEffectiveTimeFrame);

List<DataItem> cloneItem = new ArrayList<>();
Object value;
Expand All @@ -86,8 +102,8 @@ public static Dataset cloneDataset(Dataset input) {
Acceleration temp = (Acceleration) item.getValue();
value = new Acceleration(temp.getX(), temp.getY(), temp.getZ());
} else {
throw new IllegalArgumentException(item.getValue().getClass().getCanonicalName()
+ " is not supported yet");
throw new IllegalArgumentException(
item.getValue().getClass().getCanonicalName() + " is not supported yet");
}

cloneItem.add(new DataItem(value, item.getStartDateTime()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
package org.radarcns.webapp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.radarcns.domain.restapi.header.MonitorHeader.MonitorCategory.PASSIVE;
import static org.radarcns.domain.restapi.header.MonitorHeader.MonitorCategory.QUESTIONNAIRE;
import static org.radarcns.mongo.data.monitor.questionnaire.QuestionnaireCompletionLogWrapper.QUESTIONNAIRE_COMPLETION_LOG_COLLECTION;
import static org.radarcns.webapp.SampleDataHandler.PROJECT;
import static org.radarcns.webapp.SampleDataHandler.SOURCE;
import static org.radarcns.webapp.SampleDataHandler.SUBJECT;
import static org.radarcns.webapp.resource.BasePath.APPLICATION_STATUS;

import java.io.IOException;
import java.util.Map;
import javax.ws.rs.core.Response.Status;

import org.bson.Document;
import org.junit.Rule;
import org.junit.Test;
import org.radarcns.domain.restapi.Application;
import org.radarcns.domain.restapi.ServerStatus;
import org.radarcns.domain.restapi.header.MonitorHeader;
import org.radarcns.domain.restapi.monitor.ApplicationStatus;
import org.radarcns.domain.restapi.monitor.MonitorData;
import org.radarcns.domain.restapi.monitor.QuestionnaireCompletionStatus;
import org.radarcns.integration.MongoRule;
import org.radarcns.integration.util.ApiClient;
import org.radarcns.integration.util.RandomInput;
Expand All @@ -40,8 +45,6 @@

public class AppStatusEndPointTest {

private static final String SOURCE_PATH =
APPLICATION_STATUS + '/' + PROJECT + '/' + SUBJECT + '/' + SOURCE;

@Rule
public final ApiClient apiClient = new ApiClient(
Expand All @@ -52,21 +55,67 @@ public class AppStatusEndPointTest {

@Test
public void getStatusTest200Unknown() throws IOException {
Application actual = apiClient.getJson(SOURCE_PATH, Application.class, Status.OK);
assertSame(ServerStatus.UNKNOWN, actual.getServerStatus());
String sourceId = "c4064d12-b963-47ae-b560-067131e321ea";
String subjectId = "sub-2";

MonitorHeader monitorHeader = new MonitorHeader(PROJECT, subjectId, sourceId, null);
assertRequestsMatch(subjectId, sourceId, monitorHeader);
}

@Test
public void getStatusTest200() throws IOException {
String sourceId = "c4064d12-b963-47ae-b560-067131e321ea";
String subjectId = "sub-2";

Map<String, Document> map = RandomInput.getRandomApplicationStatus(
PROJECT, SUBJECT, SOURCE);
PROJECT, subjectId, sourceId);

map.forEach((k, v) -> mongoRule.getCollection(k).insertOne(v));

Application expected = Utility.convertDocToApplication(map);
Application actual = apiClient.getJson(SOURCE_PATH, Application.class, Status.OK);
ApplicationStatus expected = Utility.convertDocToApplicationStatus(map);
MonitorHeader monitorHeader = new MonitorHeader(PROJECT, subjectId, sourceId, PASSIVE);
MonitorData actual = assertRequestsMatch(subjectId, sourceId, monitorHeader);

assertTrue(actual.getData() instanceof Map);
Map<String, String> status = (Map<String, String>) actual.getData();

assertEquals(expected.getServerStatus().toString(), status.get("serverStatus"));
}

@Test
public void getQuestionnaireCompletionStatusTest200() throws IOException {
String sourceId = "0d29b9eb-289a-4dc6-b969-534dca72a187";
String subjectId = "sub-3";

Document document = RandomInput.getRandomQuestionnaireCompletionLog(
PROJECT, subjectId, sourceId);

mongoRule.getCollection(QUESTIONNAIRE_COMPLETION_LOG_COLLECTION).insertOne(document);

QuestionnaireCompletionStatus expected = Utility
.convertDocToQuestionnaireCompletionStatus(document);
MonitorHeader monitorHeader = new MonitorHeader(PROJECT, subjectId, sourceId,
QUESTIONNAIRE);
MonitorData actual = assertRequestsMatch(subjectId, sourceId, monitorHeader);

assertTrue(actual.getData() instanceof Map);
Map<String, String> status = (Map<String, String>) actual.getData();

assertEquals(expected.getCompletionPercentage(), status.get("completionPercentage"));
}

private MonitorData assertRequestsMatch(String subjectId, String sourceId, MonitorHeader
expectedHeader)
throws IOException {
String relativeUrl =
APPLICATION_STATUS + '/' + PROJECT + '/' + subjectId + '/' + sourceId;

assertEquals(expected.getServerStatus(), actual.getServerStatus());
assertEquals(expected.getIpAddress(), actual.getIpAddress());
MonitorData actual = apiClient.getJson(relativeUrl, MonitorData.class, Status.OK);
assertNotNull(actual);
MonitorHeader actualHeader = actual.getHeader();
assertEquals(expectedHeader.getProjectId(), actualHeader.getProjectId());
assertEquals(expectedHeader.getSubjectId(), actualHeader.getSubjectId());
assertEquals(expectedHeader.getSourceId(), actualHeader.getSourceId());
return actual;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"ID";"PRODUCER";"MODEL";"catalog_version";"SOURCE_TYPE_SCOPE";"dynamic_registration";"created_by"
"1";"Empatica";"E4";"v1";"PASSIVE";"FALSE";"system"
"2";"THINC-IT App";"App";"v1";"ACTIVE";"true";"system"
"3";"RADAR";"pRMT";"1.0.0";"MONITOR";"false";"system"
"4";"RADAR";"aRMT-App";"1.2.0";"ACTIVE";"true";"system"
4 changes: 4 additions & 0 deletions src/integrationTest/resources/config/liquibase/sources.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"ID";"source_id";"source_name";"assigned";"created_by";"source_type_id";"project_id"
"1";"03d28e5c-e005-46d4-a9b3-279c27fbbc83";"source-1";"TRUE";"system";"1";"1"
"2";"c4064d12-b963-47ae-b560-067131e321ea";"source-2";"TRUE";"system";"3";"1"
"3";"0d29b9eb-289a-4dc6-b969-534dca72a187";"source-3";"TRUE";"system";"4";"1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"SUBJECTS_ID";"SOURCES_ID"
"1";"1"
"2";"2"
"3";"3"

Loading