ExternalRestService { public Optional<String> getAccessTokenFromKernel(LocalAuthority localAuthority) { RestTemplate restTemplate = new RestTemplate(); try { String accessToken = restTemplate.getForObject( discoveryUtils.adminServiceUrl() + "/api/admin/local-authority/{localAuthoritySiren}/accessToken", String.class, localAuthority.getSiren()); return Optional.ofNullable(accessToken); } catch (RestClientResponseException e){ LOGGER.error( "[getAccessTokenFromKernel] An error was occured when tried to get kernel access token for organization {} from {} : Status {} Body {} ", discoveryUtils.adminServiceUrl(), e.getRawStatusCode(), e.getResponseBodyAsString()); return Optional.empty(); } }  JsonNode getProfile(String profileUuid); JsonNode getProfileForEmail(String siren, String email); JsonNode getGroupsForLocalAuthority(String uuid); JsonNode getProfiles(String uuid); GenericAccount getGenericAccount(String uuid); GenericAccount authWithCertificate(String serial, String vendor); GenericAccount authWithEmailPassword(String email, String password); JsonNode getPaullConnection(String sessionID); LocalAuthority getLocalAuthorityByCertificate(Certificate certificate); String createGroup(LocalAuthority localAuthority, String name); Optional<String> getAccessTokenFromKernel(LocalAuthority localAuthority); Optional<String> getLocalAuthorityDcId(LocalAuthority localAuthority); JsonNode getProfileByLocalAuthoritySirenAndEmail(String siren, String email);  }
@Test public void getAccessTokenFromKernel() { stubFor(get(urlPathEqualTo("/api/admin/local-authority/123456789/accessToken")) .willReturn(ok("access-token"))); LocalAuthority localAuthority = new LocalAuthority(); ReflectionTestUtils.setField(localAuthority, "uuid", "uuid-local-authority-one-test"); localAuthority.setSiren("123456789"); Optional<String> accessToken = externalRestService.getAccessTokenFromKernel(localAuthority); WireMock.verify(getRequestedFor(urlPathEqualTo("/api/admin/local-authority/123456789/accessToken"))); Assert.assertTrue(accessToken.isPresent()); }
@Test public void getAccessTokenFromKernelLocalAuthorityNotFound() { stubFor(get(urlPathEqualTo("/api/admin/local-authority/987654321/accessToken")) .willReturn(notFound())); LocalAuthority localAuthority = new LocalAuthority(); ReflectionTestUtils.setField(localAuthority, "uuid", "uuid-local-authority-two-test"); localAuthority.setSiren("987654321"); Optional<String> accessToken = externalRestService.getAccessTokenFromKernel(localAuthority); WireMock.verify(getRequestedFor(urlPathEqualTo("/api/admin/local-authority/987654321/accessToken"))); Assert.assertFalse(accessToken.isPresent()); }
@Test public void getAccessTokenFromKernelErrorFromKernel() { stubFor(get(urlPathEqualTo("/api/admin/local-authority/987654321/accessToken")) .willReturn(badRequest())); LocalAuthority localAuthority = new LocalAuthority(); ReflectionTestUtils.setField(localAuthority, "uuid", "uuid-local-authority-two-test"); localAuthority.setSiren("987654321"); Optional<String> accessToken = externalRestService.getAccessTokenFromKernel(localAuthority); WireMock.verify(getRequestedFor(urlPathEqualTo("/api/admin/local-authority/987654321/accessToken"))); Assert.assertFalse(accessToken.isPresent()); }