Skip to content

Commit

Permalink
[INJIWEB-383] add api call to fetch credential types from well known …
Browse files Browse the repository at this point in the history
…end poing in download VC (#198)

* [INJIWEB-383] add api call to fetch credential types from well known in download VC
Signed-off-by: Challarao <[email protected]>

* [INJIWEB-383] removed reading wellknown from static json
Signed-off-by: Challarao <[email protected]>
  • Loading branch information
challabeehyv committed Apr 23, 2024
1 parent b1b650b commit 37435fb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.mosip.mimoto.exception.ApiNotAccessibleException;
import io.mosip.mimoto.service.IssuersService;
import io.mosip.mimoto.util.DateUtils;
import io.mosip.mimoto.util.RestApiClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -36,6 +37,9 @@ public class IssuersController {
@Autowired
IssuersService issuersService;

@Autowired
private RestApiClient restApiClient;

private static final String defaultLanguageConstant = "en";

private static final String ID = "mosip.mimoto.issuers";
Expand Down Expand Up @@ -124,7 +128,10 @@ public ResponseEntity<?> generatePdfForVC(@RequestHeader("Bearer") String token,

try{
IssuerDTO issuerConfig = issuersService.getIssuerConfig(issuerId);
CredentialIssuerWellKnownResponse credentialIssuerWellKnownResponse = issuersService.getCredentialWellKnownFromJson();
CredentialIssuerWellKnownResponse credentialIssuerWellKnownResponse = restApiClient.getApi(issuerConfig.getWellKnownEndpoint(), CredentialIssuerWellKnownResponse.class);
if (credentialIssuerWellKnownResponse == null) {
throw new ApiNotAccessibleException();
}
Optional<CredentialsSupportedResponse> credentialsSupportedResponse = credentialIssuerWellKnownResponse.getCredentialsSupported().stream()
.filter(credentialsSupported -> credentialsSupported.getId().equals(credentialType))
.findFirst();
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/io/mosip/mimoto/service/IssuersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.mosip.mimoto.dto.IssuerDTO;
import io.mosip.mimoto.dto.IssuersDTO;
import io.mosip.mimoto.dto.mimoto.CredentialIssuerWellKnownResponse;
import io.mosip.mimoto.dto.mimoto.CredentialsSupportedResponse;
import io.mosip.mimoto.dto.mimoto.IssuerSupportedCredentialsResponse;
import io.mosip.mimoto.exception.ApiNotAccessibleException;
Expand All @@ -20,8 +19,6 @@ public interface IssuersService {

IssuerSupportedCredentialsResponse getCredentialsSupported(String issuerId, String search) throws ApiNotAccessibleException, IOException;

CredentialIssuerWellKnownResponse getCredentialWellKnownFromJson() throws IOException, ApiNotAccessibleException;

ByteArrayInputStream generatePdfForVerifiableCredentials(String accessToken, IssuerDTO issuerDTO, CredentialsSupportedResponse credentialsSupportedResponse, String credentialEndPoint) throws Exception;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public IssuerSupportedCredentialsResponse getCredentialsSupported(String issuerI
// Get credential supported types from well known endpoint
CredentialIssuerWellKnownResponse response = restApiClient.getApi(issuerDto.getWellKnownEndpoint(), CredentialIssuerWellKnownResponse.class);
if (response == null) {
response = getCredentialWellKnownFromJson();
throw new ApiNotAccessibleException();
}
List<CredentialsSupportedResponse> issuerCredentialsSupported = response.getCredentialsSupported();
credentialTypesWithAuthorizationEndpoint.setAuthorizationEndPoint(issuerDto.getAuthorization_endpoint());
Expand All @@ -165,15 +165,6 @@ public IssuerSupportedCredentialsResponse getCredentialsSupported(String issuerI
return credentialTypesWithAuthorizationEndpoint;
}

@Override
public CredentialIssuerWellKnownResponse getCredentialWellKnownFromJson() throws IOException, ApiNotAccessibleException {
String credentialsSupportedResponseJson = utilities.getCredentialsSupportedConfigJsonValue();
if (credentialsSupportedResponseJson == null){
throw new ApiNotAccessibleException();
}
return new Gson().fromJson(credentialsSupportedResponseJson, CredentialIssuerWellKnownResponse.class);
}

@Override
public ByteArrayInputStream generatePdfForVerifiableCredentials(String accessToken, IssuerDTO issuerDTO, CredentialsSupportedResponse credentialsSupportedResponse, String credentialEndPoint) throws Exception {
LinkedHashMap<String, String> vcPropertiesFromWellKnown = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand Down Expand Up @@ -79,6 +80,9 @@ public class InjiControllerTest {
@MockBean
private IssuersServiceImpl issuersService;

@MockBean
public RestApiClient restApiClient;

@Autowired
private MockMvc mockMvc;

Expand Down Expand Up @@ -231,7 +235,7 @@ public void generatePdfForVCTest() throws Exception {
.thenReturn(getIssuerDTO("Issuer1"))
.thenThrow(new ApiNotAccessibleException());

Mockito.when(issuersService.getCredentialWellKnownFromJson())
Mockito.when(restApiClient.getApi(ArgumentMatchers.any(String.class), ArgumentMatchers.any()))
.thenReturn(getCredentialIssuerWellKnownResponseDto("Issuer1",
List.of(getCredentialSupportedResponse("CredentialType1"))));

Expand Down
10 changes: 4 additions & 6 deletions src/test/java/io/mosip/mimoto/service/IssuersServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.util.*;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
Expand Down Expand Up @@ -124,9 +124,6 @@ static CredentialIssuerWellKnownResponse getCredentialIssuerWellKnownResponseDto
public void setUp() throws Exception {
IssuersDTO issuers = new IssuersDTO();
issuers.setIssuers(List.of(getIssuerDTO("Issuer1", Collections.emptyList()), getIssuerDTO("Issuer2", Collections.emptyList())));
CredentialIssuerWellKnownResponse credentialIssuerWellKnownResponse = getCredentialIssuerWellKnownResponseDto("Issuer1",
List.of(getCredentialSupportedResponse("CredentialSupported1"), getCredentialSupportedResponse("CredentialSupported2")));
Mockito.when(utilities.getCredentialsSupportedConfigJsonValue()).thenReturn(new Gson().toJson(credentialIssuerWellKnownResponse));
Mockito.when(utilities.getIssuersConfigJsonValue()).thenReturn(new Gson().toJson(issuers));
}

Expand Down Expand Up @@ -204,9 +201,8 @@ public void shouldReturnOnlyEnabledIssuers() throws IOException, ApiNotAccessibl

@Test(expected = ApiNotAccessibleException.class)
public void shouldThrowApiNotAccessibleExceptionWhenCredentialsSupportedJsonStringIsNullForGettingCredentialsSupportedList() throws Exception {
Mockito.when(utilities.getCredentialsSupportedConfigJsonValue()).thenReturn(null);
Mockito.when(restApiClient.getApi(any(String.class), any(Class.class))).thenReturn(null);
issuersService.getCredentialsSupported("Issuer1id", null);
Mockito.when(restApiClient.getApi(Mockito.any(URI.class), Mockito.any(Class.class))).thenReturn(null);
}

@Test
Expand All @@ -219,6 +215,8 @@ public void shouldReturnIssuerCredentialSupportedResponseForTheIssuerIdIfExist()
expectedIssuerCredentialsSupported.setSupportedCredentials(credentialsSupportedResponses);
expectedIssuerCredentialsSupported.setAuthorizationEndPoint(authorization_endpoint);

Mockito.when(restApiClient.getApi(any(String.class), any())).thenReturn(getCredentialIssuerWellKnownResponseDto("Issuer1",
List.of(getCredentialSupportedResponse("CredentialSupported1"), getCredentialSupportedResponse("CredentialSupported2"))));
IssuerSupportedCredentialsResponse issuerSupportedCredentialsResponse = issuersService.getCredentialsSupported("Issuer1id", null);
assertEquals(issuerSupportedCredentialsResponse, expectedIssuerCredentialsSupported);
}
Expand Down

0 comments on commit 37435fb

Please sign in to comment.