|
| 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.Filter; |
| 25 | +import com.jayway.jsonpath.JsonPath; |
| 26 | +import org.json.JSONArray; |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static com.jayway.jsonpath.Criteria.where; |
| 33 | + |
| 34 | +/** |
| 35 | + * It is required to provide the current_range_meters property in vehicle_status for motorized vehicles |
| 36 | + */ |
| 37 | +public class CurrentRangeMetersIsRequiredInVehicleStatusForMotorizedVehicles implements CustomRuleSchemaPatcher { |
| 38 | + |
| 39 | + private final String fileName; |
| 40 | + |
| 41 | + public CurrentRangeMetersIsRequiredInVehicleStatusForMotorizedVehicles(String fileName) { |
| 42 | + this.fileName = fileName; |
| 43 | + } |
| 44 | + |
| 45 | + private static final Filter motorizedVehicleTypesFilter = Filter.filter( |
| 46 | + where("propulsion_type").in( |
| 47 | + List.of( |
| 48 | + "electric_assist", "electric", "combustion" |
| 49 | + ) |
| 50 | + ) |
| 51 | + ); |
| 52 | + private static final String BIKE_ITEMS_SCHEMA_PATH = "$.properties.data.properties.bikes.items"; |
| 53 | + private static final String VEHICLE_ITEMS_SCHEMA_PATH = "$.properties.data.properties.vehicles.items"; |
| 54 | + |
| 55 | + @Override |
| 56 | + public DocumentContext addRule(DocumentContext rawSchemaDocumentContext, Map<String, JSONObject> feeds) { |
| 57 | + JSONObject vehicleTypesFeed = feeds.get("vehicle_types"); |
| 58 | + |
| 59 | + JSONArray motorizedVehicleTypeIds = null; |
| 60 | + |
| 61 | + if (vehicleTypesFeed != null) { |
| 62 | + motorizedVehicleTypeIds = JsonPath.parse(vehicleTypesFeed) |
| 63 | + .read("$.data.vehicle_types[?].vehicle_type_id", motorizedVehicleTypesFilter); |
| 64 | + } |
| 65 | + |
| 66 | + String schemaPath = VEHICLE_ITEMS_SCHEMA_PATH; |
| 67 | + |
| 68 | + if (fileName.equals("free_bike_status")) { |
| 69 | + schemaPath = BIKE_ITEMS_SCHEMA_PATH; |
| 70 | + } |
| 71 | + |
| 72 | + JSONObject bikeItemsSchema = rawSchemaDocumentContext.read(schemaPath); |
| 73 | + |
| 74 | + if (motorizedVehicleTypeIds != null && motorizedVehicleTypeIds.length() > 0) { |
| 75 | + bikeItemsSchema.put("errorMessage", new JSONObject().put("required", new JSONObject().put("vehicle_type_id", "'vehicle_type_id' is required for this vehicle type"))); |
| 76 | + bikeItemsSchema |
| 77 | + .put("if", |
| 78 | + new JSONObject() |
| 79 | + .put("properties", new JSONObject().put("vehicle_type_id", new JSONObject().put("enum", motorizedVehicleTypeIds))) |
| 80 | + |
| 81 | + // "required" so it only trigger "then" when "vehicle_type_id" is present. |
| 82 | + .put("required", new JSONArray().put("vehicle_type_id")) |
| 83 | + ) |
| 84 | + .put("then", new JSONObject().put("required", new JSONArray().put("current_range_meters"))); |
| 85 | + } |
| 86 | + |
| 87 | + return rawSchemaDocumentContext.set(schemaPath, bikeItemsSchema); |
| 88 | + } |
| 89 | +} |
0 commit comments