Skip to content

Commit e110eed

Browse files
Made all REST calls test with and without country code
Depending on whether we are syncing with a national node or with the central Directory, we may or may not need to incoporate the country code into the URL for REST API calls. The changes test both possibilities.
1 parent 868e45c commit e110eed

File tree

5 files changed

+61
-43
lines changed

5 files changed

+61
-43
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>de.samply</groupId>
1212
<artifactId>directory_sync_service</artifactId>
13-
<version>1.5.10</version>
13+
<version>1.5.11</version>
1414
<name>directory_sync_service</name>
1515
<description>Directory sync</description>
1616
<url>https://github.com/samply/directory_sync_service</url>

src/main/java/de/samply/directory_sync_service/directory/DirectoryApi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected boolean deleteStarModel(StarModelData starModelInputData) {
155155
// We need to do things this way, because the Directory implements paging
156156
// and a single pass may not get all facts.
157157
do {
158-
List<String> factIds = getNextPageOfFactIdsForCollection(countryCode, collectionId);
158+
List<String> factIds = getNextPageOfFactIdsForCollection(collectionId);
159159

160160
if (factIds == null) {
161161
logger.warn("deleteStarModel: Problem getting facts for collection: " + collectionId);
@@ -186,11 +186,10 @@ protected boolean deleteStarModel(StarModelData starModelInputData) {
186186
/**
187187
* Retrieves a list of fact IDs from the Directory associated with a specific collection.
188188
*
189-
* @param countryCode The country code, e.g. DE.
190189
* @param collectionId The ID of the collection to retrieve fact IDs for.
191190
* @return A list of fact IDs for the specified collection, or null if there is an issue retrieving the data. An empty list indicates that there are no more facts left to be retrieved.
192191
*/
193-
protected abstract List<String> getNextPageOfFactIdsForCollection(String countryCode, String collectionId);
192+
protected abstract List<String> getNextPageOfFactIdsForCollection(String collectionId);
194193

195194
/**
196195
* Deletes facts from the Directory service based on a list of fact IDs.

src/main/java/de/samply/directory_sync_service/directory/graphql/DirectoryApiGraphql.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,12 @@ private String wrapValueInHashWithAttribute(String attributeName, String value)
640640
/**
641641
* Retrieves a list of fact IDs from the Directory associated with a specific collection.
642642
*
643-
* @param countryCode The country code, e.g. DE.
644643
* @param collectionId The ID of the collection to retrieve fact IDs for.
645644
* @return A list of fact IDs for the specified collection, or null if there is an issue retrieving the data. An empty list indicates that there are no more facts left to be retrieved.
646645
*/
647646
@Override
648-
protected List<String> getNextPageOfFactIdsForCollection(String countryCode, String collectionId) {
647+
protected List<String> getNextPageOfFactIdsForCollection(String collectionId) {
648+
String countryCode = extractCountryCodeFromBbmriEricId(collectionId);
649649
List<String> factIds = new ArrayList<>();
650650

651651
// Use getFactPageToggle to ensure that this method gets run only once.

src/main/java/de/samply/directory_sync_service/directory/rest/DirectoryApiRest.java

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,20 @@ public Biobank fetchBiobank(BbmriEricId id) {
7070
// Return a fake Biobank if we are mocking
7171
return new Biobank();
7272

73-
Biobank biobank = (Biobank) directoryCallsRest.get(directoryEndpointsRest.getBiobankEndpoint(id.getCountryCode()) + "/" + id, Biobank.class);
73+
Biobank biobank = fetchBiobank(id.getCountryCode(), id);
7474
if (biobank == null) {
75-
logger.warn("fetchBiobank: No Biobank in Directory with id: " + id);
76-
return null;
75+
logger.info("fetchBiobank: biobank is null, trying URL without country code");
76+
biobank = fetchBiobank(null, id);
77+
if (biobank == null)
78+
logger.warn("fetchBiobank: No Biobank in Directory with id: " + id);
7779
}
7880
return biobank;
7981
}
8082

83+
private Biobank fetchBiobank(String countryCode, BbmriEricId id) {
84+
return (Biobank) directoryCallsRest.get(directoryEndpointsRest.getBiobankEndpoint(countryCode) + "/" + id, Biobank.class);
85+
}
86+
8187
/**
8288
* Make API calls to the Directory to fill a DirectoryCollectionGet object containing attributes
8389
* for all of the collections listed in collectionIds. The countryCode is used solely for
@@ -97,17 +103,15 @@ public DirectoryCollectionGet fetchCollectionGetOutcomes(String countryCode, Lis
97103
return directoryCollectionGet;
98104
}
99105

100-
logger.debug("fetchCollectionGetOutcomes: ?????????????????????????????????????????????????????????????????????????????????????????????????????????????");
101106
boolean warnFlag = false;
102107
for (String collectionId: collectionIds) {
103108
logger.debug("fetchCollectionGetOutcomes: collectionId: " + collectionId);
104109
String commandUrl = directoryEndpointsRest.getCollectionEndpoint(countryCode) + "?q=id==%22" + collectionId + "%22";
105110
logger.debug("fetchCollectionGetOutcomes: commandUrl: " + commandUrl);
106111
DirectoryCollectionGet singleDirectoryCollectionGet = (DirectoryCollectionGet) directoryCallsRest.get(commandUrl, DirectoryCollectionGet.class);
107112
if (singleDirectoryCollectionGet == null) {
108-
logger.warn("fetchCollectionGetOutcomes: singleDirectoryCollectionGet is null, trying URL without country code");
113+
logger.info("fetchCollectionGetOutcomes: singleDirectoryCollectionGet is null, trying URL without country code");
109114
commandUrl = directoryEndpointsRest.getCollectionEndpoint(null) + "?q=id==%22" + collectionId + "%22";
110-
logger.debug("fetchCollectionGetOutcomes: new commandUrl: " + commandUrl);
111115
singleDirectoryCollectionGet = (DirectoryCollectionGet) directoryCallsRest.get(commandUrl, DirectoryCollectionGet.class);
112116
if (singleDirectoryCollectionGet == null) {
113117
logger.warn("fetchCollectionGetOutcomes: singleDirectoryCollectionGet is null, does the collection exist in the Directory: " + collectionId);
@@ -123,7 +127,6 @@ public DirectoryCollectionGet fetchCollectionGetOutcomes(String countryCode, Lis
123127
}
124128
directoryCollectionGet.getItems().add(item);
125129
}
126-
logger.debug("fetchCollectionGetOutcomes: ?????????????????????????????????????????????????????????????????????????????????????????????????????????????");
127130

128131
if (warnFlag && directoryCollectionGet.isEmpty()) {
129132
logger.warn("fetchCollectionGetOutcomes: No entities retrieved from Directory");
@@ -145,15 +148,24 @@ public boolean updateEntities(DirectoryCollectionPut directoryCollectionPut) {
145148
return true;
146149
}
147150

148-
String response = directoryCallsRest.put(directoryEndpointsRest.getCollectionEndpoint(directoryCollectionPut.getCountryCode()), directoryCollectionPut);
151+
String response = updateEntities(directoryCollectionPut.getCountryCode(), directoryCollectionPut);
149152
if (response == null) {
150-
logger.warn("entity update, PUT problem");
151-
return false;
153+
logger.info("updateEntities: PUT problem, trying URL without country code");
154+
response = updateEntities(null, directoryCollectionPut);
155+
if (response == null) {
156+
logger.warn("updateEntities: PUT problem even without country code, aborting");
157+
return false;
158+
}
152159
}
153160

154161
return true;
155162
}
156163

164+
private String updateEntities(String countryCode, DirectoryCollectionPut directoryCollectionPut) {
165+
return directoryCallsRest.put(directoryEndpointsRest.getCollectionEndpoint(countryCode), directoryCollectionPut);
166+
}
167+
168+
157169
/**
158170
* Updates the fact tables block for a specific country with the provided data.
159171
*
@@ -168,13 +180,22 @@ protected boolean updateFactTablesBlock(String countryCode, List<Map<String, Str
168180
return true;
169181
}
170182

183+
if (mockDirectory) {
184+
// Dummy return if we're in mock mode
185+
return true;
186+
}
187+
171188
Map<String,Object> body = new HashMap<String,Object>();
172189
body.put("entities", factTablesBlock);
173-
String response = directoryCallsRest.post(directoryEndpointsRest.getFactEndpoint(countryCode), body);
174190

191+
String response = directoryCallsRest.post(directoryEndpointsRest.getFactEndpoint(countryCode), body);
175192
if (response == null) {
176-
logger.warn("updateFactTablesBlock: null response from REST call");
177-
return false;
193+
logger.info("updateFactTablesBlock: null response from REST call, trying URL without country code");
194+
response = directoryCallsRest.post(directoryEndpointsRest.getFactEndpoint(null), body);
195+
if (response == null) {
196+
logger.warn("updateFactTablesBlock: null response from REST call even without country code, aborting");
197+
return false;
198+
}
178199
}
179200

180201
return true;
@@ -183,23 +204,26 @@ protected boolean updateFactTablesBlock(String countryCode, List<Map<String, Str
183204
/**
184205
* Retrieves a list of fact IDs from the Directory associated with a specific collection.
185206
*
186-
* @param countryCode The country code, e.g. DE.
187207
* @param collectionId The ID of the collection to retrieve fact IDs for.
188208
* @return A list of fact IDs for the specified collection, or null if there is an issue retrieving the data. An empty list indicates that there are no more facts left to be retrieved.
189209
*/
190210
@Override
191-
protected List<String> getNextPageOfFactIdsForCollection(String countryCode, String collectionId) {
192-
String apiUrl = directoryEndpointsRest.getFactEndpoint(countryCode);
193-
211+
protected List<String> getNextPageOfFactIdsForCollection(String collectionId) {
194212
// Get a list of fact IDs for this collection
213+
String apiUrl = directoryEndpointsRest.getFactEndpoint(extractCountryCodeFromBbmriEricId(collectionId));
195214
Map factWrapper = (Map) directoryCallsRest.get(apiUrl + "?q=collection==%22" + collectionId + "%22", Map.class);
196-
197215
if (factWrapper == null) {
198-
logger.warn("deleteStarModel: Problem getting facts for collection, factWrapper == null, collectionId=" + collectionId);
199-
return null;
216+
logger.info("getNextPageOfFactIdsForCollection: Problem getting facts for collection, factWrapper == null, collectionId=" + collectionId + ", trying without country code");
217+
apiUrl = directoryEndpointsRest.getFactEndpoint(extractCountryCodeFromBbmriEricId(null));
218+
factWrapper = (Map) directoryCallsRest.get(apiUrl + "?q=collection==%22" + collectionId + "%22", Map.class);
219+
if (factWrapper == null) {
220+
logger.warn("getNextPageOfFactIdsForCollection: Problem getting facts for collection, factWrapper == null, collectionId=" + collectionId + " even without contry code, aborting");
221+
return null;
222+
}
200223
}
224+
201225
if (!factWrapper.containsKey("items")) {
202-
logger.warn("deleteStarModel: Problem getting facts for collection, no item key present: " + collectionId);
226+
logger.warn("getNextPageOfFactIdsForCollection: Problem getting facts for collection, no item key present: " + collectionId);
203227
return null;
204228
}
205229
List<Map<String, String>> facts = (List<Map<String, String>>) factWrapper.get("items");
@@ -227,14 +251,20 @@ protected boolean deleteFactsByIds(String countryCode, List<String> factIds) {
227251
// Nothing to delete
228252
return true;
229253

230-
String apiUrl = directoryEndpointsRest.getFactEndpoint(countryCode);
254+
if (mockDirectory) {
255+
// Dummy return if we're in mock mode
256+
return true;
257+
}
231258

232259
// Directory likes to have its delete data wrapped in a map with key "entityIds".
233-
String result = directoryCallsRest.delete(apiUrl, new HashMap<>(Map.of("entityIds", factIds)));
234-
260+
String result = directoryCallsRest.delete(directoryEndpointsRest.getFactEndpoint(countryCode), new HashMap<>(Map.of("entityIds", factIds)));
235261
if (result == null) {
236-
logger.warn("deleteFactsByIds, Problem during delete of factIds");
237-
return false;
262+
logger.info("deleteFactsByIds, Problem during delete of factIds, trying without country code");
263+
result = directoryCallsRest.delete(directoryEndpointsRest.getFactEndpoint(null), new HashMap<>(Map.of("entityIds", factIds)));
264+
if (result == null) {
265+
logger.warn("deleteFactsByIds, Problem during delete of factIds even without contry code, aborting");
266+
return false;
267+
}
238268
}
239269

240270
return true;

src/main/java/de/samply/directory_sync_service/directory/rest/DirectoryCallsRest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
package de.samply.directory_sync_service.directory.rest;
22

3-
import com.google.gson.Gson;
43
import de.samply.directory_sync_service.Util;
54
import de.samply.directory_sync_service.directory.DirectoryCalls;
65
import de.samply.directory_sync_service.directory.DirectoryCredentials;
7-
import org.apache.http.HttpEntity;
8-
import org.apache.http.HttpResponse;
9-
import org.apache.http.client.methods.CloseableHttpResponse;
106
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
117
import org.apache.http.client.methods.HttpGet;
12-
import org.apache.http.client.methods.HttpOptions;
138
import org.apache.http.client.methods.HttpPost;
149
import org.apache.http.client.methods.HttpPut;
15-
import org.apache.http.client.methods.HttpUriRequest;
1610
import org.apache.http.entity.StringEntity;
17-
import org.apache.http.impl.client.CloseableHttpClient;
18-
import org.apache.http.impl.client.HttpClients;
19-
import org.apache.http.util.EntityUtils;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
2211

2312
import java.io.BufferedReader;
2413
import java.io.IOException;

0 commit comments

Comments
 (0)