14
14
import com .fasterxml .jackson .databind .JsonNode ;
15
15
import com .fasterxml .jackson .databind .ObjectMapper ;
16
16
import com .fasterxml .jackson .databind .node .ObjectNode ;
17
+ import io .restassured .http .Method ;
17
18
import org .junit .jupiter .api .AfterAll ;
18
19
import org .junit .jupiter .api .BeforeAll ;
19
20
import org .junit .jupiter .api .Test ;
27
28
import java .io .IOException ;
28
29
import java .sql .SQLException ;
29
30
import java .util .Date ;
31
+ import java .util .Objects ;
30
32
import java .util .stream .Stream ;
31
33
32
34
import static com .conveyal .datatools .TestUtils .assertThatSqlCountQueryYieldsExpectedCount ;
@@ -47,6 +49,7 @@ public class EditorControllerTest extends UnitTest {
47
49
private static Project project ;
48
50
private static FeedSource feedSource ;
49
51
private static FeedSource feedSourceCascadeDelete ;
52
+ private static FeedSource faresV2FeedSource ;
50
53
private static FeedVersion feedVersion ;
51
54
private static FeedVersion feedVersionCascadeDelete ;
52
55
private static FeedVersion faresV2Version ;
@@ -75,13 +78,12 @@ public static void setUp() throws Exception {
75
78
feedSourceCascadeDelete .projectId = project .id ;
76
79
Persistence .feedSources .create (feedSourceCascadeDelete );
77
80
78
- FeedSource faresV2FeedSource = new FeedSource ("FaresV2" );
81
+ faresV2FeedSource = new FeedSource ("FaresV2" );
79
82
faresV2FeedSource .projectId = project .id ;
80
83
Persistence .feedSources .create (faresV2FeedSource );
81
84
82
85
feedVersion = createFeedVersionFromGtfsZip (feedSource , "bart_old.zip" );
83
86
feedVersionCascadeDelete = createFeedVersionFromGtfsZip (feedSourceCascadeDelete , "bart_old.zip" );
84
-
85
87
faresV2Version = createFeedVersion (faresV2FeedSource , zipFolderFiles ("fake-agency-with-fares-v2" ));
86
88
87
89
// Create and run snapshot jobs
@@ -96,6 +98,7 @@ public static void tearDown() {
96
98
project .delete ();
97
99
feedSource .delete ();
98
100
feedSourceCascadeDelete .delete ();
101
+ faresV2FeedSource .delete ();
99
102
}
100
103
101
104
/**
@@ -104,7 +107,13 @@ public static void tearDown() {
104
107
private static void createAndRunSnapshotJob (String feedVersionName , String feedSourceId , String namespace ) {
105
108
Snapshot snapshot = new Snapshot ("Snapshot of " + feedVersionName , feedSourceId , namespace );
106
109
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
+ );
108
117
createSnapshotJob .run ();
109
118
}
110
119
@@ -121,14 +130,7 @@ private static Stream<Arguments> createPatchTableTests() {
121
130
*/
122
131
@ ParameterizedTest
123
132
@ 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 {
132
134
LOG .info ("Making patch {} request" , table );
133
135
String value = "NEW" ;
134
136
ObjectNode jsonBody = mapper .createObjectNode ();
@@ -148,7 +150,7 @@ public void canPatchTableTests(
148
150
* Make sure the patch table endpoint can patch stops conditionally with query.
149
151
*/
150
152
@ Test
151
- public void canPatchStopsConditionally () throws IOException {
153
+ void canPatchStopsConditionally () throws IOException {
152
154
LOG .info ("Making conditional patch stops request" );
153
155
ObjectNode jsonBody = mapper .createObjectNode ();
154
156
String field = "stop_desc" ;
@@ -197,13 +199,7 @@ void canCascadeDeleteStop() throws IOException, SQLException {
197
199
stopId ,
198
200
feedVersionCascadeDelete .feedSourceId
199
201
);
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 , "" );
207
203
JsonNode json = mapper .readTree (response );
208
204
assertEquals (OK_200 , json .get ("code" ).asInt ());
209
205
@@ -214,42 +210,63 @@ void canCascadeDeleteStop() throws IOException, SQLException {
214
210
}
215
211
216
212
/**
217
- * Confirm that an existing fare product can be updated.
213
+ * Confirm that a fare product can be created, updated and deleted .
218
214
*/
219
215
@ 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 ()
227
253
.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 )
230
256
.then ()
231
257
.extract ()
232
258
.response ()
233
259
.asString ();
234
- JsonNode json = mapper .readTree (response );
235
- assertEquals ("AERIAL_TRAM_ROUND_TRIP" , json .get ("fare_product_id" ).asText ());
236
260
}
237
261
238
262
/**
239
263
* Perform patch table request on the feed source ID with the requested query and patch JSON. A null query will
240
264
* apply the patch JSON to the entire table.
241
265
*/
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 {
243
267
String path = String .format ("/api/editor/secure/%s?feedId=%s" , entity , feedId );
244
268
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 );
253
270
JsonNode json = mapper .readTree (response );
254
271
return json .get ("count" ).asInt ();
255
272
}
@@ -262,16 +279,9 @@ private static JsonNode graphqlQuery (String namespace, String graphQLQueryFile)
262
279
ObjectNode variables = mapper .createObjectNode ();
263
280
variables .put ("namespace" , namespace );
264
281
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 ) ));
266
283
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 );
275
285
return mapper .readTree (graphQLString );
276
286
}
277
287
0 commit comments