Skip to content

Commit

Permalink
notfound-test also OK
Browse files Browse the repository at this point in the history
  • Loading branch information
bratne committed Sep 17, 2024
1 parent a8e0ebc commit 570d93c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
31 changes: 12 additions & 19 deletions src/main/java/no/fintlabs/AzureClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,32 +563,25 @@ private CompletableFuture<Void> processPageAsync(AzureGroup azureGroup, Director

public boolean doesGroupExist(String resourceGroupId) {
// TODO: Attributes should not be hard-coded [FKS-210]
String selectionCriteria = String.format("id,displayName,description,%s", configGroup.getFintkontrollidattribute());
String[] selectionCriteria = new String[]{String.format("id,displayName,description,%s", configGroup.getFintkontrollidattribute())};
String filterCriteria = String.format(configGroup.getFintkontrollidattribute() + " eq '%s'", resourceGroupId);

GroupCollectionResponse groupCollectionPage = graphServiceClient.groups()
.get(requestConfiguration -> {
requestConfiguration.queryParameters.select = new String[]{String.format("id,displayName,description,%s", configGroup.getFintkontrollidattribute())};
requestConfiguration.queryParameters.filter = String.format(configGroup.getFintkontrollidattribute() + " eq '%s'", resourceGroupId);
requestConfiguration.queryParameters.select = selectionCriteria;
requestConfiguration.queryParameters.filter = filterCriteria;
});

while (groupCollectionPage != null) {
for (Group group : groupCollectionPage.getValue()) {
String attributeValue = group.getAdditionalData().get(configGroup.getFintkontrollidattribute()).toString();
if (groupCollectionPage.getOdataNextLink() != null) {
log.error("doesGroupExist should only return a single group!");
}

if (attributeValue != null && attributeValue.equals(resourceGroupId)) {
return true; // Group with the specified ResourceID found
}
}
for (Group group : groupCollectionPage.getValue()) {
String attributeValue = group.getAdditionalData().get(configGroup.getFintkontrollidattribute()).toString();

// Move to the next page if available
//groupCollectionPage = groupCollectionPage.getOdataNextLink() == null ? null :
// graphServiceClient.groups().get(requestconfiguration ->
// requestconfiguration.queryParameters.toQueryParameters(groupCollectionPage.getOdataNextLink())
// );
//groupCollectionPage = groupCollectionPage.getNextPage() == null ? null :
// groupCollectionPage.getNextPage()
// .buildRequest()
// .get();
if (attributeValue != null && attributeValue.equals(resourceGroupId)) {
return true; // Group with the specified ResourceID found
}
}

return false; // Group with resourceID not found
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/no/fintlabs/AzureClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,11 @@ void doesGroupExist_found() {
void doesGroupExist_notfound() {
String resourceGroupID = "234";

when(graphServiceClient.groups().byGroupId(anyString()).get()).thenReturn(groupItemRequestBuilder.get());
//when(graphServiceClient.groups().get()).thenReturn(groupCollectionRequest);
when(graphServiceClient.groups().get(requestConfiguration -> {
requestConfiguration.queryParameters.select = new String[]{String.format("id,%s", configGroup.getFintkontrollidattribute())};
requestConfiguration.queryParameters.filter = String.format(configGroup.getFintkontrollidattribute() + " eq '%s'", resourceGroupID);
})).thenReturn(groupRequestConfiguration.get());
when(graphServiceClient.groups()).thenReturn(groupsRequestBuilder);
when(groupsRequestBuilder.get(any())).thenReturn(groupCollectionResponse);

List<Group> groupList = getTestGrouplist(3);
when(groupCollectionPage.getValue()).thenReturn(groupList);
when(groupCollectionResponse.getValue()).thenReturn(groupList);

assertFalse(azureClient.doesGroupExist(resourceGroupID));
}
Expand Down

0 comments on commit 570d93c

Please sign in to comment.