|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * |
| 5 | + * * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by |
| 6 | + * * * the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 7 | + * * * You may not use this work except in compliance with the Licence. |
| 8 | + * * * You may obtain a copy of the Licence at: |
| 9 | + * * * |
| 10 | + * * * https://joinup.ec.europa.eu/software/page/eupl |
| 11 | + * * * |
| 12 | + * * * Unless required by applicable law or agreed to in writing, software |
| 13 | + * * * distributed under the Licence is distributed on an "AS IS" basis, |
| 14 | + * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * * * See the Licence for the specific language governing permissions and |
| 16 | + * * * limitations under the Licence. |
| 17 | + * * |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +package org.entur.gbfs.validation.validator.rules; |
| 22 | + |
| 23 | +import com.jayway.jsonpath.DocumentContext; |
| 24 | +import com.jayway.jsonpath.JsonPath; |
| 25 | +import java.util.Map; |
| 26 | +import org.json.JSONArray; |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +/** |
| 30 | + * References to stations in station_information must exist in station_status file and vice versa. |
| 31 | + */ |
| 32 | +public class NoInvalidReferenceToStation implements CustomRuleSchemaPatcher { |
| 33 | + |
| 34 | + public static final String STATION_IDS_SCHEMA_PATH = |
| 35 | + "$.properties.data.properties.stations.items.properties.station_id"; |
| 36 | + |
| 37 | + private final String stationReferenceFileName; |
| 38 | + |
| 39 | + public NoInvalidReferenceToStation(String stationReferenceFileName) { |
| 40 | + this.stationReferenceFileName = stationReferenceFileName; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds an enum to the station_id schema of stations.station_id with the station ids from station_status.json |
| 45 | + */ |
| 46 | + @Override |
| 47 | + public DocumentContext addRule( |
| 48 | + DocumentContext rawSchemaDocumentContext, |
| 49 | + Map<String, JSONObject> feeds |
| 50 | + ) { |
| 51 | + JSONObject stationReferenceFeed = feeds.get(stationReferenceFileName); |
| 52 | + |
| 53 | + JSONObject stationIdSchema = rawSchemaDocumentContext.read( |
| 54 | + STATION_IDS_SCHEMA_PATH |
| 55 | + ); |
| 56 | + |
| 57 | + JSONArray stationIds = stationReferenceFeed != null |
| 58 | + ? JsonPath |
| 59 | + .parse(stationReferenceFeed) |
| 60 | + .read("$.data.stations[*].station_id") |
| 61 | + : new JSONArray(); |
| 62 | + |
| 63 | + stationIdSchema.put("enum", stationIds); |
| 64 | + |
| 65 | + return rawSchemaDocumentContext.set( |
| 66 | + STATION_IDS_SCHEMA_PATH, |
| 67 | + stationIdSchema |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments