-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package access.eduid; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.*; | ||
import org.springframework.http.client.support.BasicAuthenticationInterceptor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class EduID { | ||
|
||
private static final Log LOG = LogFactory.getLog(EduID.class); | ||
|
||
private final String uri; | ||
private final RestTemplate restTemplate; | ||
private final HttpHeaders headers; | ||
|
||
public EduID(@Value("${myconext.uri}") String uri, | ||
@Value("${myconext.username}") String userName, | ||
@Value("${myconext.password}") String password) { | ||
this.uri = uri; | ||
this.restTemplate = new RestTemplate(); | ||
this.restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(userName, password)); | ||
this.headers = initHttpHeaders(); | ||
} | ||
|
||
public Optional<String> provisionEduid(EduIDProvision eduIDProvision) { | ||
HttpEntity<EduIDProvision> requestEntity = new HttpEntity<>(eduIDProvision, headers); | ||
try { | ||
ResponseEntity<EduIDProvision> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, EduIDProvision.class); | ||
return Optional.of(responseEntity.getBody().getEduIDValue()); | ||
} catch (RuntimeException e) { | ||
LOG.error("Error in provisionEduid", e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private HttpHeaders initHttpHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); | ||
return headers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package access.eduid; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class EduIDProvision { | ||
|
||
private String eduIDValue; | ||
private String institutionGUID; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package access.eduid; | ||
|
||
import access.WireMockExtension; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class EduIDTest { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private final EduID eduID = new EduID("http://localhost:8081/myconext/api/invite/provision-eduid", "invite", "secret"); | ||
|
||
@RegisterExtension | ||
WireMockExtension mockServer = new WireMockExtension(8081); | ||
|
||
@SneakyThrows | ||
@Test | ||
void provisionEduid() { | ||
String eduIDValue = UUID.randomUUID().toString(); | ||
String institutionGUID = UUID.randomUUID().toString(); | ||
EduIDProvision eduIDProvision = new EduIDProvision(eduIDValue, institutionGUID); | ||
stubFor(post(urlPathMatching("/myconext/api/invite/provision-eduid")).willReturn(aResponse() | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(objectMapper.writeValueAsString(eduIDProvision)))); | ||
Optional<String> eduid = eduID.provisionEduid(eduIDProvision); | ||
assertEquals(eduIDProvision.getEduIDValue(), eduid.get()); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void provisionEduid404() { | ||
EduIDProvision eduIDProvision = new EduIDProvision("eduIDValue", "institutionGUID"); | ||
stubFor(post(urlPathMatching("/myconext/api/invite/provision-eduid")).willReturn(aResponse() | ||
.withHeader("Content-Type", "application/json") | ||
.withStatus(404))); | ||
Optional<String> eduid = eduID.provisionEduid(eduIDProvision); | ||
assertTrue(eduid.isEmpty()); | ||
} | ||
} |