-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from mobidata-bw/issues/431_service_unavailable
Respond with status code 502 if configured feed not yet available
- Loading branch information
Showing
6 changed files
with
384 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/org/entur/lamassu/controller/UpstreamFeedNotYetAvailableException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* | ||
* | ||
* * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by | ||
* * the European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* * You may not use this work except in compliance with the Licence. | ||
* * You may obtain a copy of the Licence at: | ||
* * | ||
* * https://joinup.ec.europa.eu/software/page/eupl | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the Licence is distributed on an "AS IS" basis, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the Licence for the specific language governing permissions and | ||
* * limitations under the Licence. | ||
* | ||
*/ | ||
|
||
package org.entur.lamassu.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(value = HttpStatus.BAD_GATEWAY, reason = "Feed not (yet) available") | ||
public class UpstreamFeedNotYetAvailableException extends RuntimeException { | ||
|
||
public UpstreamFeedNotYetAvailableException() { | ||
super(); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
src/test/java/org/entur/lamassu/controller/GBFSV2FeedControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.entur.lamassu.controller; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.entur.gbfs.v2_3.gbfs.GBFS; | ||
import org.entur.gbfs.v2_3.gbfs.GBFSFeed; | ||
import org.entur.gbfs.v2_3.gbfs.GBFSFeedName; | ||
import org.entur.gbfs.v2_3.gbfs.GBFSFeeds; | ||
import org.entur.lamassu.cache.GBFSV2FeedCache; | ||
import org.entur.lamassu.model.provider.FeedProvider; | ||
import org.entur.lamassu.service.FeedProviderService; | ||
import org.entur.lamassu.service.SystemDiscoveryService; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
public class GBFSV2FeedControllerTest { | ||
|
||
public static final String KNOWN_SYSTEM_ID = "knownSystem"; | ||
private GBFSV2FeedController feedController; | ||
private FeedProviderService mockedFeedProviderService; | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
private GBFSV2FeedCache mockedFeedCache; | ||
|
||
@Before | ||
public void before() { | ||
SystemDiscoveryService systemDiscoveryService = mock(SystemDiscoveryService.class); | ||
mockedFeedCache = mock(GBFSV2FeedCache.class); | ||
mockedFeedProviderService = mock(FeedProviderService.class); | ||
|
||
feedController = | ||
new GBFSV2FeedController( | ||
systemDiscoveryService, | ||
mockedFeedCache, | ||
mockedFeedProviderService | ||
); | ||
} | ||
|
||
@Test | ||
public void throws400OnNonGBFSFeedRequest() { | ||
expectedException.expect(ResponseStatusException.class); | ||
expectedException.expectMessage("400 BAD_REQUEST"); | ||
|
||
feedController.getGbfsFeedForProvider("anySystem", "no-gbfs-feed"); | ||
} | ||
|
||
@Test | ||
public void throws404OnNonConfiguredSystemRequest() { | ||
expectedException.expect(ResponseStatusException.class); | ||
expectedException.expectMessage("404 NOT_FOUND"); | ||
feedController.getGbfsFeedForProvider("unknownSystem", "gbfs"); | ||
} | ||
|
||
@Test | ||
public void throws502OnConfiguredSystemButUnavailableFeedRequest() { | ||
FeedProvider feedProvider = new FeedProvider(); | ||
feedProvider.setSystemId(KNOWN_SYSTEM_ID); | ||
when(mockedFeedProviderService.getFeedProviderBySystemId(KNOWN_SYSTEM_ID)) | ||
.thenReturn(feedProvider); | ||
|
||
expectedException.expect(UpstreamFeedNotYetAvailableException.class); | ||
feedController.getGbfsFeedForProvider(KNOWN_SYSTEM_ID, "gbfs"); | ||
} | ||
|
||
@Test | ||
public void throws404OnConfiguredSystemButUndeclaredFeedRequest() { | ||
var feedProvider = new FeedProvider(); | ||
feedProvider.setSystemId(KNOWN_SYSTEM_ID); | ||
var gbfs = createDiscoveryFileWithFeed(GBFSFeedName.GBFS); | ||
|
||
when(mockedFeedProviderService.getFeedProviderBySystemId(KNOWN_SYSTEM_ID)) | ||
.thenReturn(feedProvider); | ||
when(mockedFeedCache.find(GBFSFeedName.GBFS, feedProvider)).thenReturn(gbfs); | ||
when(mockedFeedCache.find(GBFSFeedName.GeofencingZones, feedProvider)) | ||
.thenReturn(null); | ||
expectedException.expect(ResponseStatusException.class); | ||
expectedException.expectMessage("404 NOT_FOUND"); | ||
feedController.getGbfsFeedForProvider(KNOWN_SYSTEM_ID, "geofencing_zones"); | ||
} | ||
|
||
@Test | ||
public void throws502OnConfiguredSystemAndDeclaredFeedRequest() { | ||
var feedProvider = new FeedProvider(); | ||
feedProvider.setSystemId(KNOWN_SYSTEM_ID); | ||
var gbfs = createDiscoveryFileWithFeed(GBFSFeedName.GeofencingZones); | ||
|
||
when(mockedFeedProviderService.getFeedProviderBySystemId(KNOWN_SYSTEM_ID)) | ||
.thenReturn(feedProvider); | ||
when(mockedFeedCache.find(GBFSFeedName.GBFS, feedProvider)).thenReturn(gbfs); | ||
when(mockedFeedCache.find(GBFSFeedName.GeofencingZones, feedProvider)) | ||
.thenReturn(null); | ||
expectedException.expect(UpstreamFeedNotYetAvailableException.class); | ||
feedController.getGbfsFeedForProvider(KNOWN_SYSTEM_ID, "geofencing_zones"); | ||
} | ||
|
||
@Test | ||
public void throws502OnConfiguredSystemAndMalformedDiscoveryFeedRequest() { | ||
var feedProvider = new FeedProvider(); | ||
feedProvider.setSystemId(KNOWN_SYSTEM_ID); | ||
// GBFS is malformed, as it has no feeds defined | ||
var gbfs = new GBFS(); | ||
|
||
when(mockedFeedProviderService.getFeedProviderBySystemId(KNOWN_SYSTEM_ID)) | ||
.thenReturn(feedProvider); | ||
when(mockedFeedCache.find(GBFSFeedName.GBFS, feedProvider)).thenReturn(gbfs); | ||
when(mockedFeedCache.find(GBFSFeedName.GeofencingZones, feedProvider)) | ||
.thenReturn(null); | ||
expectedException.expect(UpstreamFeedNotYetAvailableException.class); | ||
feedController.getGbfsFeedForProvider(KNOWN_SYSTEM_ID, "geofencing_zones"); | ||
} | ||
|
||
public GBFS createDiscoveryFileWithFeed(GBFSFeedName feedName) { | ||
var gbfs = new GBFS(); | ||
var feeds = new GBFSFeeds(); | ||
var feed = new GBFSFeed(); | ||
feed.setName(feedName); | ||
feeds.setFeeds(List.of(feed)); | ||
gbfs.setFeedsData(Map.of("en", feeds)); | ||
return gbfs; | ||
} | ||
} |
Oops, something went wrong.