diff --git a/src/main/java/org/eclipsefoundation/profile/services/impl/DefaultProfileService.java b/src/main/java/org/eclipsefoundation/profile/services/impl/DefaultProfileService.java index 6d42ecb3aecf23baf19772c298e5b99b7b971015..6aa5c288dec0ee2399a8ced60642bf2ca1c678f1 100644 --- a/src/main/java/org/eclipsefoundation/profile/services/impl/DefaultProfileService.java +++ b/src/main/java/org/eclipsefoundation/profile/services/impl/DefaultProfileService.java @@ -117,21 +117,23 @@ public class DefaultProfileService implements ProfileService { @Override public EfUser getProfileByUsername(String username) { try { - // Search in LDAP and accounts asynchronously - CompletableFuture<Optional<LdapResult>> ldapSearch = executor.supplyAsync(() -> ldapService.searchLdapByUsername(username)); - CompletableFuture<Optional<AccountsProfileData>> accountsSearch = executor - .supplyAsync(() -> profileHelper.getAccountsProfileByUsername(username)); - - Optional<LdapResult> ldapResult = ldapSearch.get(); - Optional<AccountsProfileData> accountsResult = accountsSearch.get(); + // Search in LDAP first and then accounts + Optional<LdapResult> ldapSearch = ldapService.searchLdapByUsername(username); + // ldap is really reliable, if it has no results, then its incredibly likely there is no user + if (ldapSearch.isEmpty()) { + return null; + } + + // retrieve accounts results for the given username + Optional<AccountsProfileData> accountsSearch = profileHelper.getAccountsProfileByUsername(username); // #114 - If results are asymmetrical, throw an exception - if (ldapResult.isPresent() ^ accountsResult.isPresent()) { + if (accountsSearch.isEmpty()) { throw new ApplicationException( String.format("Results from services are inconsistent regarding user, cannot return: %s", username)); } // if either result is empty, both are empty with above bitwise check - return ldapResult.isEmpty() ? null : buildEfUser(ldapResult.get(), accountsResult.get()); + return buildEfUser(ldapSearch.get(), accountsSearch.get()); } catch (Exception e) { if (e.getClass().equals(InterruptedException.class)) { // only interrupt on interrupts diff --git a/src/test/java/org/eclipsefoundation/profile/resources/AccountResourceTest.java b/src/test/java/org/eclipsefoundation/profile/resources/AccountResourceTest.java index c6e049f81aa2f971df78ffb4803cba39f7846140..fdee504c69ab73a1d8bca302655b5cf522db8217 100644 --- a/src/test/java/org/eclipsefoundation/profile/resources/AccountResourceTest.java +++ b/src/test/java/org/eclipsefoundation/profile/resources/AccountResourceTest.java @@ -164,7 +164,7 @@ class AccountResourceTest { public static final EndpointTestCase GET_BY_USER_CASE_MISSING_IN_LDAP = TestCaseHelper .prepareTestCase(PROFILE_BY_USER_URL, new String[] { MockAccountsAPI.USERNAME_NO_LDAP_USER }, SchemaNamespaceHelper.ERROR_SCHEMA_PATH) - .setStatusCode(500) + .setStatusCode(404) .setHeaderParams(TestNamespaceHelper.VALID_ANON_AUTH_HEADER) .build(); diff --git a/src/test/java/org/eclipsefoundation/profile/services/impl/DefaultProfileServiceTest.java b/src/test/java/org/eclipsefoundation/profile/services/impl/DefaultProfileServiceTest.java index 68337c40af4b911ec4270ddcfdf8789f7a8b47cb..4aa1bf06cb8bbcae10c929e84cc1baf94c1620bb 100644 --- a/src/test/java/org/eclipsefoundation/profile/services/impl/DefaultProfileServiceTest.java +++ b/src/test/java/org/eclipsefoundation/profile/services/impl/DefaultProfileServiceTest.java @@ -45,8 +45,8 @@ class DefaultProfileServiceTest { * Regression test for #110, check that we can handle 404's w/ unexpected return values properly */ @Test - void getProfileByUsername_failure_handlesServiceDown() { - String username = "username_notfound"; + void getProfileByUsername_failure_handlesAccountServiceDown() { + String username = "username"; // reset the mock between the tests Mockito.reset(apimock); diff --git a/src/test/java/org/eclipsefoundation/profile/test/services/MockLDAPConnectionWrapper.java b/src/test/java/org/eclipsefoundation/profile/test/services/MockLDAPConnectionWrapper.java index 627ff293024ce057e875b99c8011454a4f43b483..050efdcc1b149113e59f837f50e10ee572eb9fb9 100644 --- a/src/test/java/org/eclipsefoundation/profile/test/services/MockLDAPConnectionWrapper.java +++ b/src/test/java/org/eclipsefoundation/profile/test/services/MockLDAPConnectionWrapper.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.eclipsefoundation.http.exception.ApplicationException; import org.eclipsefoundation.profile.namespace.LdapFieldNames; import org.eclipsefoundation.profile.namespace.ProfileAPIParameterNames; import org.eclipsefoundation.profile.services.LDAPConnectionWrapper; @@ -54,6 +55,11 @@ public class MockLDAPConnectionWrapper implements LDAPConnectionWrapper { String searchField = request.getFilter().getAttributeName(); String searchValue = request.getFilter().getAssertionValue(); + if (searchValue.startsWith(TestNamespaceHelper.ERROR_TRIGGER_PREFIX)) { + throw new ApplicationException("Mock error for LDAP", + Integer.parseInt(searchValue.substring(TestNamespaceHelper.ERROR_TRIGGER_PREFIX.length()))); + } + return entries.stream().filter(e -> e.getAttribute(searchField).getValue().equalsIgnoreCase(searchValue)).toList(); }