|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.sbm.helpers; |
| 17 | + |
| 18 | +import net.minidev.json.JSONArray; |
| 19 | +import net.minidev.json.JSONObject; |
| 20 | +import net.minidev.json.parser.JSONParser; |
| 21 | +import net.minidev.json.parser.ParseException; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.URI; |
| 25 | +import java.net.URISyntaxException; |
| 26 | +import java.net.URLEncoder; |
| 27 | +import java.net.http.HttpClient; |
| 28 | +import java.net.http.HttpRequest; |
| 29 | +import java.net.http.HttpResponse; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.text.MessageFormat; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Test helper that provides various information about the dependencies. |
| 37 | + * |
| 38 | + * @author Szymon Sadowski |
| 39 | + */ |
| 40 | +public class DependencyVersionHelper { |
| 41 | + public static final String MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE = |
| 42 | + "https://search.maven.org/solrsearch/select?q={0}&rows=1&wt=json"; |
| 43 | + public static final String MAVEN_DEPENDENCY_QUERY_TEMPLATE = "g:\"{0}\" AND a:\"{1}\""; |
| 44 | + public static final String RESPONSE_JSON_KEY = "response"; |
| 45 | + public static final String DOCS_JSON_KEY = "docs"; |
| 46 | + public static final String LATEST_VERSION_JSON_KEY = "latestVersion"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Finds the latest release version for a given dependency. |
| 50 | + * @param groupId GroupId of a sought dependency. |
| 51 | + * @param artifactId ArtifactId of a sought dependency. |
| 52 | + * @return Optional string with the version number of the latest release for a given dependency |
| 53 | + * if the search was successful. Empty optional if the search was not successful. |
| 54 | + */ |
| 55 | + public static Optional<String> getLatestReleaseVersion(String groupId, String artifactId) { |
| 56 | + String url = MessageFormat.format( |
| 57 | + MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE, |
| 58 | + URLEncoder.encode( |
| 59 | + MessageFormat.format(MAVEN_DEPENDENCY_QUERY_TEMPLATE, groupId, artifactId), |
| 60 | + StandardCharsets.UTF_8 |
| 61 | + ) |
| 62 | + ); |
| 63 | + |
| 64 | + final HttpResponse<String> response; |
| 65 | + |
| 66 | + try { |
| 67 | + HttpClient client = HttpClient.newHttpClient(); |
| 68 | + |
| 69 | + HttpRequest request2 = HttpRequest.newBuilder() |
| 70 | + .uri(new URI(url)) |
| 71 | + .GET() |
| 72 | + .build(); |
| 73 | + |
| 74 | + response = client.send(request2, HttpResponse.BodyHandlers.ofString()); |
| 75 | + } catch (URISyntaxException | IOException | InterruptedException e) { |
| 76 | + return Optional.empty(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + JSONObject json; |
| 81 | + |
| 82 | + try { |
| 83 | + json = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(response.body(), JSONObject.class); |
| 84 | + } catch(ParseException jsonException) { |
| 85 | + return Optional.empty(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + if (!json.containsKey(RESPONSE_JSON_KEY)) { |
| 90 | + return Optional.empty(); |
| 91 | + } |
| 92 | + |
| 93 | + JSONObject responseSection = (JSONObject) json.get(RESPONSE_JSON_KEY); |
| 94 | + |
| 95 | + if(!responseSection.containsKey(DOCS_JSON_KEY)) { |
| 96 | + return Optional.empty(); |
| 97 | + } |
| 98 | + |
| 99 | + JSONArray docs = (JSONArray) responseSection.get(DOCS_JSON_KEY); |
| 100 | + |
| 101 | + if (docs.size() == 0) { |
| 102 | + return Optional.empty(); |
| 103 | + } |
| 104 | + |
| 105 | + JSONObject docEntry = (JSONObject) docs.get(0); |
| 106 | + |
| 107 | + if(!docEntry.containsKey(LATEST_VERSION_JSON_KEY)) { |
| 108 | + return Optional.empty(); |
| 109 | + } |
| 110 | + |
| 111 | + return Optional.of(docEntry.getAsString(LATEST_VERSION_JSON_KEY)); |
| 112 | + } |
| 113 | +} |
0 commit comments