|
| 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 org.json.JSONArray; |
| 26 | +import org.json.JSONObject; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * It is required to provide ios and android store uris in system_information if vehicle_status |
| 32 | + * or station_information has ios and android rental uris respectively |
| 33 | + */ |
| 34 | +public class NoMissingStoreUriInSystemInformation implements CustomRuleSchemaPatcher { |
| 35 | + |
| 36 | + private static final String DATA_REQUIRED_SCHEMA_PATH = "$.properties.data.required"; |
| 37 | + private static final String RENTAL_APPS_SCHEMA_PATH = "$.properties.data.properties.rental_apps"; |
| 38 | + |
| 39 | + |
| 40 | + private final String vehicleStatusFileName; |
| 41 | + |
| 42 | + |
| 43 | + public NoMissingStoreUriInSystemInformation(String vehicleStatusFileName) { |
| 44 | + this.vehicleStatusFileName = vehicleStatusFileName; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public DocumentContext addRule(DocumentContext rawSchemaDocumentContext, Map<String, JSONObject> feeds) { |
| 49 | + boolean hasIosRentalUris = false; |
| 50 | + boolean hasAndroidRentalUris = false; |
| 51 | + |
| 52 | + JSONObject vehicleStatusFeed = feeds.get(vehicleStatusFileName); |
| 53 | + |
| 54 | + if (vehicleStatusFeed != null) { |
| 55 | + String vehiclesKey = vehicleStatusFileName.equals("vehicle_status") ? "vehicles" : "bikes"; |
| 56 | + |
| 57 | + if (!((JSONArray) JsonPath.parse(vehicleStatusFeed) |
| 58 | + .read("$.data." + vehiclesKey + "[:1].rental_uris.ios")).isEmpty()) { |
| 59 | + hasIosRentalUris = true; |
| 60 | + } |
| 61 | + |
| 62 | + if (!((JSONArray) JsonPath.parse(vehicleStatusFeed) |
| 63 | + .read("$.data." + vehiclesKey + "[:1].rental_uris.android")).isEmpty()) { |
| 64 | + hasAndroidRentalUris = true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + JSONObject stationInformationFeed = feeds.get("station_information"); |
| 69 | + |
| 70 | + if (stationInformationFeed != null) { |
| 71 | + if (!((JSONArray) JsonPath.parse(stationInformationFeed) |
| 72 | + .read("$.data.stations[:1].rental_uris.ios")).isEmpty()) { |
| 73 | + hasIosRentalUris = true; |
| 74 | + } |
| 75 | + |
| 76 | + if (!((JSONArray) JsonPath.parse(stationInformationFeed) |
| 77 | + .read("$.data.stations[:1].rental_uris.android")).isEmpty()) { |
| 78 | + hasAndroidRentalUris = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (hasIosRentalUris || hasAndroidRentalUris) { |
| 83 | + |
| 84 | + JSONArray systemInformationDataRequiredSchema = rawSchemaDocumentContext.read(DATA_REQUIRED_SCHEMA_PATH); |
| 85 | + systemInformationDataRequiredSchema.put("rental_apps"); |
| 86 | + |
| 87 | + JSONObject rentalAppsSchema = rawSchemaDocumentContext.read(RENTAL_APPS_SCHEMA_PATH); |
| 88 | + JSONArray rentalAppRequired = new JSONArray(); |
| 89 | + |
| 90 | + |
| 91 | + if (hasIosRentalUris) { |
| 92 | + rentalAppRequired.put("ios"); |
| 93 | + } |
| 94 | + |
| 95 | + if (hasAndroidRentalUris) { |
| 96 | + rentalAppRequired.put("android"); |
| 97 | + } |
| 98 | + |
| 99 | + rentalAppsSchema.put("required", rentalAppRequired); |
| 100 | + } |
| 101 | + |
| 102 | + return rawSchemaDocumentContext; |
| 103 | + } |
| 104 | +} |
0 commit comments