Skip to content

Commit fe4e278

Browse files
committed
improvement(EditorControllerTest.java): Updated fare product test to include create and delete reque
1 parent 7b446d1 commit fe4e278

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

src/test/java/com/conveyal/datatools/editor/controllers/api/EditorControllerTest.java

+59-49
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.fasterxml.jackson.databind.JsonNode;
1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import com.fasterxml.jackson.databind.node.ObjectNode;
17+
import io.restassured.http.Method;
1718
import org.junit.jupiter.api.AfterAll;
1819
import org.junit.jupiter.api.BeforeAll;
1920
import org.junit.jupiter.api.Test;
@@ -27,6 +28,7 @@
2728
import java.io.IOException;
2829
import java.sql.SQLException;
2930
import java.util.Date;
31+
import java.util.Objects;
3032
import java.util.stream.Stream;
3133

3234
import static com.conveyal.datatools.TestUtils.assertThatSqlCountQueryYieldsExpectedCount;
@@ -47,6 +49,7 @@ public class EditorControllerTest extends UnitTest {
4749
private static Project project;
4850
private static FeedSource feedSource;
4951
private static FeedSource feedSourceCascadeDelete;
52+
private static FeedSource faresV2FeedSource;
5053
private static FeedVersion feedVersion;
5154
private static FeedVersion feedVersionCascadeDelete;
5255
private static FeedVersion faresV2Version;
@@ -75,13 +78,12 @@ public static void setUp() throws Exception {
7578
feedSourceCascadeDelete.projectId = project.id;
7679
Persistence.feedSources.create(feedSourceCascadeDelete);
7780

78-
FeedSource faresV2FeedSource = new FeedSource("FaresV2");
81+
faresV2FeedSource = new FeedSource("FaresV2");
7982
faresV2FeedSource.projectId = project.id;
8083
Persistence.feedSources.create(faresV2FeedSource);
8184

8285
feedVersion = createFeedVersionFromGtfsZip(feedSource, "bart_old.zip");
8386
feedVersionCascadeDelete = createFeedVersionFromGtfsZip(feedSourceCascadeDelete, "bart_old.zip");
84-
8587
faresV2Version = createFeedVersion(faresV2FeedSource, zipFolderFiles("fake-agency-with-fares-v2"));
8688

8789
// Create and run snapshot jobs
@@ -96,6 +98,7 @@ public static void tearDown() {
9698
project.delete();
9799
feedSource.delete();
98100
feedSourceCascadeDelete.delete();
101+
faresV2FeedSource.delete();
99102
}
100103

101104
/**
@@ -104,7 +107,13 @@ public static void tearDown() {
104107
private static void createAndRunSnapshotJob(String feedVersionName, String feedSourceId, String namespace) {
105108
Snapshot snapshot = new Snapshot("Snapshot of " + feedVersionName, feedSourceId, namespace);
106109
CreateSnapshotJob createSnapshotJob =
107-
new CreateSnapshotJob(Auth0UserProfile.createTestAdminUser(), snapshot, true, false, false);
110+
new CreateSnapshotJob(
111+
Auth0UserProfile.createTestAdminUser(),
112+
snapshot,
113+
true,
114+
false,
115+
false
116+
);
108117
createSnapshotJob.run();
109118
}
110119

@@ -121,14 +130,7 @@ private static Stream<Arguments> createPatchTableTests() {
121130
*/
122131
@ParameterizedTest
123132
@MethodSource("createPatchTableTests")
124-
public void canPatchTableTests(
125-
String field,
126-
String entity,
127-
int expectedCount,
128-
String graphQLQueryFile,
129-
String table
130-
) throws IOException {
131-
133+
void canPatchTableTests(String field, String entity, int expectedCount, String graphQLQueryFile, String table) throws IOException {
132134
LOG.info("Making patch {} request", table);
133135
String value = "NEW";
134136
ObjectNode jsonBody = mapper.createObjectNode();
@@ -148,7 +150,7 @@ public void canPatchTableTests(
148150
* Make sure the patch table endpoint can patch stops conditionally with query.
149151
*/
150152
@Test
151-
public void canPatchStopsConditionally() throws IOException {
153+
void canPatchStopsConditionally() throws IOException {
152154
LOG.info("Making conditional patch stops request");
153155
ObjectNode jsonBody = mapper.createObjectNode();
154156
String field = "stop_desc";
@@ -197,13 +199,7 @@ void canCascadeDeleteStop() throws IOException, SQLException {
197199
stopId,
198200
feedVersionCascadeDelete.feedSourceId
199201
);
200-
String response = given()
201-
.port(DataManager.PORT)
202-
.delete(path)
203-
.then()
204-
.extract()
205-
.response()
206-
.asString();
202+
String response = makeRequest(Method.DELETE, path, "");
207203
JsonNode json = mapper.readTree(response);
208204
assertEquals(OK_200, json.get("code").asInt());
209205

@@ -214,42 +210,63 @@ void canCascadeDeleteStop() throws IOException, SQLException {
214210
}
215211

216212
/**
217-
* Confirm that an existing fare product can be updated.
213+
* Confirm that a fare product can be created, updated and deleted.
218214
*/
219215
@Test
220-
void canUpdateFareProduct() throws IOException {
221-
String path = String.format(
222-
"/api/editor/secure/fareproduct/%s?feedId=%s&sessionId=test",
223-
2,
224-
faresV2Version.feedSourceId
225-
);
226-
String response = given()
216+
void canCreateUpdateAndDeleteFareProduct() throws IOException {
217+
String fareProductId = "AERIAL_TRAM_ROUND_TRIP";
218+
String fareProductIdUpdated = "AERIAL_TRAM_ROUND_TRIP_UPDATED";
219+
String urlPrefix = "/api/editor/secure/fareproduct";
220+
String urlSuffix = String.format("?feedId=%s&sessionId=test", faresV2Version.feedSourceId);
221+
String payload =
222+
"{" +
223+
"\"fare_product_id\":\"" + fareProductId + "\"," +
224+
"\"fare_product_name\":\"Portland Aerial Tram Single Round Trip\"," +
225+
"\"fare_media_id\":\"1\"," +
226+
"\"amount\":\"13.5\"," +
227+
"\"currency\":\"USD\"" +
228+
"}";
229+
230+
// Create.
231+
String response = makeRequest(Method.POST, String.format("%s%s", urlPrefix, urlSuffix), payload);
232+
JsonNode json = mapper.readTree(response);
233+
String id = json.get("id").asText();
234+
assertEquals(fareProductId, json.get("fare_product_id").asText());
235+
236+
// Update.
237+
payload = payload.replace(fareProductId, fareProductIdUpdated);
238+
response = makeRequest(Method.PUT, String.format("%s/%s%s", urlPrefix, id, urlSuffix), payload);
239+
json = mapper.readTree(response);
240+
assertEquals(fareProductIdUpdated, json.get("fare_product_id").asText());
241+
242+
// Delete.
243+
response = makeRequest(Method.DELETE, String.format("%s/%s%s", urlPrefix, id, urlSuffix), "");
244+
json = mapper.readTree(response);
245+
assertEquals(200, json.get("code").asInt());
246+
}
247+
248+
/**
249+
* Make request and return the response.
250+
*/
251+
private static String makeRequest(Method method, String path, Object payload) {
252+
return given()
227253
.port(DataManager.PORT)
228-
.body("{\"id\":2,\"fare_product_id\":\"AERIAL_TRAM_ROUND_TRIP\",\"fare_product_name\":\"Portland Aerial Tram Single Round Trip\",\"fare_media_id\":\"1\",\"amount\":\"13.5\",\"currency\":\"USD\"}")
229-
.put(path)
254+
.body(payload)
255+
.request(method, path)
230256
.then()
231257
.extract()
232258
.response()
233259
.asString();
234-
JsonNode json = mapper.readTree(response);
235-
assertEquals("AERIAL_TRAM_ROUND_TRIP", json.get("fare_product_id").asText());
236260
}
237261

238262
/**
239263
* Perform patch table request on the feed source ID with the requested query and patch JSON. A null query will
240264
* apply the patch JSON to the entire table.
241265
*/
242-
private static int patchTableRequest(String entity, String feedId, String query, JsonNode oatchJSON) throws IOException {
266+
private static int patchTableRequest(String entity, String feedId, String query, JsonNode patchJSON) throws IOException {
243267
String path = String.format("/api/editor/secure/%s?feedId=%s", entity, feedId);
244268
if (query != null) path += "&" + query;
245-
String response = given()
246-
.port(DataManager.PORT)
247-
.body(oatchJSON)
248-
.patch(path)
249-
.then()
250-
.extract()
251-
.response()
252-
.asString();
269+
String response = makeRequest(Method.PATCH, path, patchJSON);
253270
JsonNode json = mapper.readTree(response);
254271
return json.get("count").asInt();
255272
}
@@ -262,16 +279,9 @@ private static JsonNode graphqlQuery (String namespace, String graphQLQueryFile)
262279
ObjectNode variables = mapper.createObjectNode();
263280
variables.put("namespace", namespace);
264281
graphQLBody.set("variables", variables);
265-
String query = IOUtils.toString(EditorControllerTest.class.getClassLoader().getResourceAsStream(graphQLQueryFile));
282+
String query = IOUtils.toString(Objects.requireNonNull(EditorControllerTest.class.getClassLoader().getResourceAsStream(graphQLQueryFile)));
266283
graphQLBody.put("query", query);
267-
String graphQLString = given()
268-
.port(DataManager.PORT)
269-
.body(graphQLBody)
270-
.post("api/manager/secure/gtfs/graphql")
271-
.then()
272-
.extract()
273-
.response()
274-
.asString();
284+
String graphQLString = makeRequest(Method.POST, "api/manager/secure/gtfs/graphql", graphQLBody);
275285
return mapper.readTree(graphQLString);
276286
}
277287

0 commit comments

Comments
 (0)