Skip to content

Commit

Permalink
Merge pull request #279 from ibi-group/handle-blank-typeform-answers
Browse files Browse the repository at this point in the history
fix(typeform/Response): Handle null answers.
  • Loading branch information
binh-dam-ibigroup authored Dec 11, 2024
2 parents 13c2676 + c5cf943 commit b66c267
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/** Data structure for TypeForm survey responses. Only including relevant fields. */
Expand Down Expand Up @@ -83,7 +84,12 @@ public String toCsvRow() {
hidden.notification_id,
hidden.trip_id,
hidden.user_id,
answers.stream().map(Answer::toCsvContent).collect(Collectors.joining(","))
answers == null
? ""
: answers.stream()
.filter(Objects::nonNull)
.map(Answer::toCsvContent)
.collect(Collectors.joining(","))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package org.opentripplanner.middleware.typeform;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ResponseTest {

public static final String EXPECTED_CSV_ROW = "response-id-0,completed,2024-10-25T15:37:42Z,2024-10-25T15:46:27Z,notification-id-1,trip-id-2,user-id-3,\"Field1 choice\",\"Field2, ChoiceA;Field2, ChoiceB\",\"Field3 answer\"";
public static final String EXPECTED_CSV_BEGINNING = "response-id-0,completed,2024-10-25T15:37:42Z,2024-10-25T15:46:27Z,notification-id-1,trip-id-2,user-id-3,";
public static final String EXPECTED_CSV_ENDING = "\"Field1 choice\",\"Field2, ChoiceA;Field2, ChoiceB\",\"Field3 answer\"";
public static final String EXPECTED_CSV_ROW = EXPECTED_CSV_BEGINNING + EXPECTED_CSV_ENDING;

@ParameterizedTest
@MethodSource("createToCsvRowCases")
void toCsvRow(Response response, String expected) {
assertEquals(expected, response.toCsvRow());
}

@Test
void toCsvRow() {
Response response = makeResponse();
assertEquals(EXPECTED_CSV_ROW, response.toCsvRow());
private static Stream<Arguments> createToCsvRowCases() {
Response responseWithoutAnswers = makeResponse();
responseWithoutAnswers.answers = null;
return Stream.of(
Arguments.of(makeResponse(), EXPECTED_CSV_ROW),
Arguments.of(responseWithoutAnswers, EXPECTED_CSV_BEGINNING)
);
}

public static Response makeResponse() {
Expand Down

0 comments on commit b66c267

Please sign in to comment.