Refactor for testing, add tests

This commit is contained in:
dotasek 2022-03-02 09:43:41 -05:00
parent 27732602e3
commit cb7cfe9141
4 changed files with 116 additions and 36 deletions

View File

@ -16,7 +16,6 @@ import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@ -192,47 +191,51 @@ public class PackageClient {
}
}
protected PackageInfo getPackageInfoFromJSON(JsonObject o, String name, String canonical, String fhirVersion) {
String id = JSONUtil.str(o, "npm-name");
String pname = JSONUtil.str(o, "name");
String pcanonical = JSONUtil.str(o, "canonical");
String description = JSONUtil.str(o, "description");
boolean ok = true;
if (ok && !Utilities.noString(name)) {
ok = (pname != null && pname.contains(name)) || (description != null && description.contains(name)) || (id != null && id.contains(name));
}
if (ok && !Utilities.noString(canonical)) {
ok = pcanonical.contains(canonical);
}
String version = null;
String fVersion = null;
String url = null;
if (ok) {
// if we can find something...
for (JsonObject e : JSONUtil.objects(o, "editions")) {
if (fhirVersion == null || fhirVersion.equals(JSONUtil.str(e, "fhir-version"))) {
String v = JSONUtil.str(e, "ig-version");
if (version == null || VersionUtilities.isThisOrLater(version, v)) {
version = v;
fVersion = e.getAsJsonArray("fhir-version").get(0).getAsString();
url = JSONUtil.str(e, "url");
String npmPackage = JSONUtil.str(e, "package");
if (npmPackage != null && id == null) {
id = npmPackage.substring(0, npmPackage.indexOf("#"));
}
}
}
}
}
return new PackageInfo(id, version, fVersion, description, url, pcanonical, address);
}
public List<PackageInfo> listFromRegistry(String name, String canonical, String fhirVersion) throws IOException {
List<PackageInfo> result = new ArrayList<>();
JsonObject packages = JsonTrackingParser.fetchJson("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json?nocache=" + System.currentTimeMillis());
for (JsonObject o : JSONUtil.objects(packages, "guides")) {
if (o.has("canonical")) {
String id = JSONUtil.str(o, "npm-name");
String pname = JSONUtil.str(o, "name");
String pcanonical = JSONUtil.str(o, "canonical");
String description = JSONUtil.str(o, "description");
boolean ok = true;
if (ok && !Utilities.noString(name)) {
ok = (pname != null && pname.contains(name)) || (description != null && description.contains(name)) || (id != null && id.contains(name));
}
if (ok && !Utilities.noString(canonical)) {
ok = pcanonical.contains(canonical);
}
String version = null;
String fVersion = null;
String url = null;
if (ok) {
// if we can find something...
for (JsonObject e : JSONUtil.objects(o, "editions")) {
if (fhirVersion == null || fhirVersion.equals(JSONUtil.str(e, "fhir-version"))) {
String v = JSONUtil.str(e, "ig-version");
if (version == null || VersionUtilities.isThisOrLater(version, v)) {
version = v;
fVersion = e.getAsJsonArray("fhir-version").get(0).getAsString();
url = JSONUtil.str(e, "url");
String npmPackage = JSONUtil.str(e, "package");
if (npmPackage != null && id == null) {
id = npmPackage.substring(0, npmPackage.indexOf("#"));
}
}
}
}
}
if (version != null) {
result.add(new PackageInfo(id, version, fVersion, description, url, pcanonical, address));
final PackageInfo packageInfo = getPackageInfoFromJSON(o, name, canonical, fhirVersion);
if (packageInfo.getVersion() != null) {
result.add(packageInfo);
}
}
}

View File

@ -0,0 +1,44 @@
package org.hl7.fhir.utilities.npm;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PackageClientTest {
PackageClient packageClient = new PackageClient(PackageClient.PRIMARY_SERVER);
private void assertExpectedFields(final PackageInfo packageInfo) {
assertEquals("dummy.package", packageInfo.getId());
assertEquals("1.2.3", packageInfo.getVersion());
assertEquals("4.5.6", packageInfo.getFhirVersion());
assertEquals("Dummy description",
packageInfo.getDescription());
assertEquals("https://d.e.f", packageInfo.getUrl());
assertEquals("https://a.b.c", packageInfo.getCanonical());
}
@Test
@DisplayName("test getting package from JSON works")
public void getPackageInfoFromJSONTest() throws java.io.IOException{
final JsonObject jsonObject = new Gson().fromJson(Files.newBufferedReader(Paths.get("src", "test", "resources", "npm", "PackageClient-baseTestCase.json")), JsonObject.class);
final PackageInfo packageInfo = packageClient.getPackageInfoFromJSON(jsonObject, null, null, null);
assertExpectedFields(packageInfo);
}
@Test
@DisplayName("test getting package from JSON works")
public void getPackageInfoWithIdFromJSONTest() throws java.io.IOException {
final JsonObject jsonObject = new Gson().fromJson(Files.newBufferedReader(Paths.get("src", "test", "resources", "npm", "PackageClient-testCaseWithId.json")), JsonObject.class);
final PackageInfo packageInfo = packageClient.getPackageInfoFromJSON(jsonObject, null, null, null);
assertExpectedFields(packageInfo);
}
}

View File

@ -0,0 +1,16 @@
{
"name": "Pan-Canadian Patient Summary",
"description": "Dummy description",
"canonical": "https://a.b.c",
"editions": [
{
"name": "Dummy name",
"ig-version": "1.2.3",
"package": "dummy.package#1.2.3",
"fhir-version": [
"4.5.6"
],
"url": "https://d.e.f"
}
]
}

View File

@ -0,0 +1,17 @@
{
"name": "Pan-Canadian Patient Summary",
"description": "Dummy description",
"canonical": "https://a.b.c",
"npm-name": "dummy.package",
"editions": [
{
"name": "Dummy name",
"ig-version": "1.2.3",
"package": "not.a.dummy.package#1.2.3",
"fhir-version": [
"4.5.6"
],
"url": "https://d.e.f"
}
]
}