Skip to content

Commit 8c9ad6b

Browse files
fix(FeedUpdater): Handle unknown feed ids from MTC.
1 parent 1f799b8 commit 8c9ad6b

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

src/main/java/com/conveyal/datatools/manager/jobs/FeedUpdater.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,17 @@ public Map<String, String> checkForUpdatedFeeds() {
162162
String filename = keyName.split("/")[1];
163163
String feedId = filename.replace(".zip", "");
164164
FeedSource feedSource = getFeedSource(feedId);
165+
if (feedSource == null) {
166+
LOG.error("No feed source found for feed ID {}", feedId);
167+
continue;
168+
}
165169

166170
if (shouldMarkFeedAsProcessed(eTag, feedSource)) {
167171
// Don't add object if it is a dir
168172
// Skip object if the filename is null
169173
if ("null".equals(feedId)) continue;
170174
try {
171175
LOG.info("New version found for {} at s3://{}/{}. ETag = {}.", feedId, feedBucket, keyName, eTag);
172-
if (feedSource == null) {
173-
LOG.error("No feed source found for feed ID {}", feedId);
174-
continue;
175-
}
176176
updatePublishedFeedVersion(feedId, feedSource);
177177
// TODO: Explore if MD5 checksum can be used to find matching feed version.
178178
// findMatchingFeedVersion(md5, feedId, feedSource);

src/test/java/com/conveyal/datatools/manager/jobs/AutoPublishJobTest.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.google.common.collect.Lists;
1313
import org.junit.jupiter.api.AfterAll;
1414
import org.junit.jupiter.api.BeforeAll;
15-
import org.junit.jupiter.api.Test;
1615
import org.junit.jupiter.params.ParameterizedTest;
1716
import org.junit.jupiter.params.provider.Arguments;
1817
import org.junit.jupiter.params.provider.MethodSource;
@@ -32,6 +31,7 @@
3231
import static org.junit.jupiter.api.Assertions.assertEquals;
3332
import static org.junit.jupiter.api.Assertions.assertFalse;
3433
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
3535
import static org.junit.jupiter.api.Assertions.assertTrue;
3636

3737
/**
@@ -131,8 +131,9 @@ private static Stream<Arguments> createPublishFeedCases() {
131131
);
132132
}
133133

134-
@Test
135-
void shouldUpdateFeedInfoAfterPublishComplete() {
134+
@ParameterizedTest
135+
@MethodSource("createUpdateFeedInfoCases")
136+
void shouldUpdateFeedInfoAfterPublishComplete(String agencyId, boolean isUnknownFeedId) {
136137
// Add the version to the feed source
137138
FeedVersion createdVersion = createFeedVersionFromGtfsZip(feedSource, "bart_new_lite.zip");
138139

@@ -149,7 +150,7 @@ void shouldUpdateFeedInfoAfterPublishComplete() {
149150
assertNotNull(updatedFeedVersion.sentToExternalPublisher);
150151

151152
// Create a test FeedUpdater instance, and simulate running the task.
152-
TestCompletedFeedRetriever completedFeedRetriever = new TestCompletedFeedRetriever();
153+
TestCompletedFeedRetriever completedFeedRetriever = new TestCompletedFeedRetriever(agencyId);
153154
FeedUpdater feedUpdater = FeedUpdater.createForTest(completedFeedRetriever);
154155

155156
// The list of feeds processed externally (completed) should be empty at this point.
@@ -163,38 +164,66 @@ void shouldUpdateFeedInfoAfterPublishComplete() {
163164
// If a feed has been republished since last check, it will have a new etag/file hash,
164165
// and the scenario below should apply.
165166
Map<String, String> etagsAfter = feedUpdater.checkForUpdatedFeeds();
166-
assertEquals(1, etagsAfter.size());
167-
assertTrue(etagsAfter.containsValue("test-etag"));
168167

169-
// Make sure that the publish-complete attribute has been set for the feed version in Mongo.
170168
FeedVersion updatedFeedVersionAfter = Persistence.feedVersions.getById(createdVersion.id);
171169
Date updatedDate = updatedFeedVersionAfter.processedByExternalPublisher;
172170
String namespace = updatedFeedVersionAfter.namespace;
173-
assertNotNull(updatedDate);
174-
175-
// At the next check for updates, the metadata for the feeds completed above
176-
// should not be updated again.
177-
feedUpdater.checkForUpdatedFeeds();
178-
FeedVersion updatedFeedVersionAfter2 = Persistence.feedVersions.getById(createdVersion.id);
179-
assertEquals(updatedDate, updatedFeedVersionAfter2.processedByExternalPublisher);
180-
assertEquals(namespace, updatedFeedVersionAfter2.namespace);
171+
172+
if (!isUnknownFeedId) {
173+
// Regular scenario: updating a known/existing feed.
174+
assertEquals(1, etagsAfter.size());
175+
assertTrue(etagsAfter.containsValue("test-etag"));
176+
177+
// Make sure that the publish-complete attribute has been set for the feed version in Mongo.
178+
assertNotNull(updatedDate);
179+
180+
// At the next check for updates, the metadata for the feeds completed above
181+
// should not be updated again.
182+
feedUpdater.checkForUpdatedFeeds();
183+
FeedVersion updatedFeedVersionAfter2 = Persistence.feedVersions.getById(createdVersion.id);
184+
assertEquals(updatedDate, updatedFeedVersionAfter2.processedByExternalPublisher);
185+
assertEquals(namespace, updatedFeedVersionAfter2.namespace);
186+
} else {
187+
// Edge case: an unknown feed id was provided,
188+
// so no update of the feed should be happening (and there should not be an exception).
189+
assertEquals(0, etagsAfter.size());
190+
assertNull(updatedDate);
191+
}
192+
}
193+
194+
private static Stream<Arguments> createUpdateFeedInfoCases() {
195+
return Stream.of(
196+
Arguments.of(
197+
TEST_AGENCY,
198+
false
199+
),
200+
Arguments.of(
201+
"12345",
202+
true
203+
)
204+
);
181205
}
182206

183207
/**
184208
* Mocks the results of an {@link S3ObjectSummary} retrieval before/after the
185209
* external MTC publishing process is complete.
186210
*/
187211
private static class TestCompletedFeedRetriever implements FeedUpdater.CompletedFeedRetriever {
212+
private final String agencyId;
188213
public boolean isPublishingComplete;
189214

215+
public TestCompletedFeedRetriever(String agencyId) {
216+
this.agencyId = agencyId;
217+
}
218+
190219
@Override
191220
public List<S3ObjectSummary> retrieveCompletedFeeds() {
192221
if (!isPublishingComplete) {
193222
return new ArrayList<>();
194223
} else {
195224
S3ObjectSummary objSummary = new S3ObjectSummary();
196225
objSummary.setETag("test-etag");
197-
objSummary.setKey(String.format("%s/%s", TEST_COMPLETED_FOLDER, TEST_AGENCY));
226+
objSummary.setKey(String.format("%s/%s", TEST_COMPLETED_FOLDER, agencyId));
198227
return Lists.newArrayList(objSummary);
199228
}
200229
}

0 commit comments

Comments
 (0)