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

fix(typeform/Response): Handle null answers. #279

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading