Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
fix: sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Sep 5, 2024
1 parent c0c202f commit 643493d
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
@DirtiesContext
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = { ManagedIdentityWalletsApplication.class })
@ContextConfiguration(initializers = { TestContextInitializer.class })
public class TokenRequestTest {
class TokenRequestTest {

private static final ObjectMapper MAPPER = new ObjectMapper();

Expand Down Expand Up @@ -125,7 +125,7 @@ public void initWallets() {

@Test
@SneakyThrows
public void testPresentationQueryWithToken() {
void testPresentationQueryWithToken() {
// when
String body = "audience=%s&client_id=%s&client_secret=%s&grant_type=client_credentials&bearer_access_scope=org.eclipse.tractusx.vc.type:MembershipCredential:read";
String requestBody = String.format(body, bpn, clientId, clientSecret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public static ResponseEntity<String> createWallet(String bpn, String name, TestR

HttpEntity<CreateWalletRequest> entity = new HttpEntity<>(request, headers);

ResponseEntity<String> exchange = testTemplate.exchange(RestURI.WALLETS, HttpMethod.POST, entity, String.class);
return exchange;
return testTemplate.exchange(RestURI.WALLETS, HttpMethod.POST, entity, String.class);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public String extractBpnFromURL(String url) {
if (matcher.find()) {
return matcher.group(1).toUpperCase();
} else {
throw new Exception("No match found");
throw new IllegalArgumentException("No match found");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private boolean validateCredentialSubject(
for (Map<String, Object> subject : credentialSubjects) {
// Extract the 'id' of the credential subject if it exists
Object subjectId = subject.get("id");
if (subjectId == null || !(subjectId instanceof String)) {
if (!(subjectId instanceof String)) {
addConstraintViolation(context, "credentialSubject.id must be a valid String");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void testMIWSettingsWithNullVCContexts() {

@Test
void testMIWSettingsWithEmptyVCContexts() {
assertThrows(IllegalArgumentException.class, () -> new MIWSettings(List.of()));
List<URI> list = List.of();
assertThrows(IllegalArgumentException.class, () -> new MIWSettings(list));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@

class CustomAuthenticationConverterTest {

private static final String VALID =
"eyJhbGciOiJSUzI1NiIsImFscGhhIjoiZzB1ZjNycjlycnN2cHlhcTVuamg4In0.eyJpc3MiOiJEaW5vQ2hpZXNhLmdpdGh1Yi5pbyIsInN1YiI6Im1heGluZSIsImF1ZCI6ImlkcmlzIiwiaWF0IjoxNzAyNjUwMTc2LCJleHAiOjE3MDI2NTA3NzYsInJlc291cmNlX2FjY2VzcyI6eyJyZXNvdXJjZUlkIjp7InJvbGVzIjpbImRlaV9tdWRhIl19fX0.wTv9GBX3AuRO8UIsAuu2YJU77ai-wchDyxRn-_yX9PeHt23vCmp_JAbkkdMdyLAWWOKncjgNeG-4lB9RCBsjmbdb1imujUrAocp3VZQqNg6OVaNV58kdsIpNNF9S8XlFI4hr1BANrw2rWJDkTRu1id-Fu-BVE1BF7ySCKHS_NaY3e7yXQM-jtU63z5FBpPvfMF-La3blPle93rgut7V3LlG-tNOp93TgFzGrQQXuJUsew34T0u4OlQa3TjQuMdZMTy0SVSLSpIzAqDsAkHv34W6SdY1p6FVQ14TfawRLkrI2QY-YM_dCFAEE7KqqnUrVVyw6XG1ydeFDuX8SJuQX7g";

private static final String MISSING_RESOURCE_ID =
"{\n" + " \"resource_access\": {\n" + " }\n" + "}";

private static final String MISSING_ROLES =
"{\n"
+ " \"resource_access\": {\n"
+ " \"resourceId\": {\n"
+ " }\n"
+ " }\n"
+ "}";

@Test
void shouldConvertSuccessfullyWithAuthorities() {
Expand Down Expand Up @@ -92,7 +79,6 @@ void shouldConvertSuccessfullyWithoutAuthoritiesWhenRolesMissing() {

@Test
void shouldConvertSuccessfullyWithoutAuthoritiesWhenResourceAccessMissing() {
Map<String, Object> resourceId = Map.of("resourceId", Map.of());
Map<String, Object> resourceAccess = Map.of("resource_access", Map.of());

Jwt jwt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.tractusx.managedidentitywallets.revocation.constant.RevocationApiEndpoints;
import org.eclipse.tractusx.managedidentitywallets.revocation.dto.CredentialStatusDto;
import org.eclipse.tractusx.managedidentitywallets.revocation.dto.StatusEntryDto;
import org.eclipse.tractusx.managedidentitywallets.revocation.dto.StatusListCredentialSubject;
import org.eclipse.tractusx.managedidentitywallets.revocation.services.RevocationService;
import org.eclipse.tractusx.managedidentitywallets.revocation.utils.BitSetManager;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
Expand Down Expand Up @@ -99,13 +100,12 @@ void whenPostCreateStatusListVC_thenReturnStatus() throws Exception {
RevocationPurpose.REVOCATION.name(),
validIndex, // this value is within the range [0, BitSetManager.BITSET_SIZE - 1]
"https://example.com/revocations/credentials/" + BPN + "/revocation/1",
"BitstringStatusListEntry");
StatusListCredentialSubject.TYPE_ENTRY);
given(revocationService.createStatusList(statusEntryDto, "token"))
.willReturn(credentialStatusDto);
when(revocationService.extractBpnFromDid(DID)).thenReturn(BPN);

Principal mockPrincipal = mockPrincipal(BPN);
var name = mockPrincipal.getName();
// When & Then
mockMvc
.perform(
Expand Down Expand Up @@ -142,7 +142,7 @@ void whenPostRevokeCredential_thenReturnOkStatus() throws Exception {
"revocation",
validIndex, // this value is within the range [0, BitSetManager.BITSET_SIZE - 1]
"http://example.com/credentials/" + BPN + "/revocation/1",
"BitstringStatusListEntry");
StatusListCredentialSubject.TYPE_ENTRY);
doNothing().when(revocationService).revoke(credentialStatusDto, "token");
when(revocationService.extractBpnFromURL(any())).thenReturn(BPN);

Expand Down Expand Up @@ -201,23 +201,4 @@ private VerifiableCredential createVerifiableCredentialTestData() {
VerifiableCredential credential = new VerifiableCredential(credentialData);
return credential;
}

private VerifiableCredential createVerifiableCredentialTestDataInvalidDID() {
Map<String, Object> credentialData = new HashMap<>();
credentialData.put("id", UUID.randomUUID().toString());
credentialData.put("issuer", "https://issuer.example.com");
credentialData.put("issuanceDate", Instant.now().toString());
// Include 'type' field as a list because VerifiableCredential expects it to be non-null and a
// list
credentialData.put("type", List.of("VerifiableCredential", "StatusListCredential"));
Map<String, Object> subjectData = new HashMap<>();
subjectData.put("id", "subjectId");
subjectData.put("type", "StatusList2021Credential");
// 'credentialSubject' can be either a List or a single Map according to the code, so I'm
// keeping it as a single Map
credentialData.put("credentialSubject", subjectData);
credentialData.put("@context", VerifiableCredential.DEFAULT_CONTEXT.toString());
VerifiableCredential credential = new VerifiableCredential(credentialData);
return credential;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ void validCredentialStatusDto_CreatesSuccessfully() {
"revocation",
validIndex, // this value is within the range [0, BitSetManager.BITSET_SIZE - 1]
"statusListCredential",
"BitstringStatusListEntry");
StatusListCredentialSubject.TYPE_ENTRY);

// Assert
assertNotNull(dto);
assertEquals("id", dto.id());
assertEquals("revocation", dto.statusPurpose());
assertEquals(validIndex, dto.statusListIndex());
assertEquals("statusListCredential", dto.statusListCredential());
assertEquals("BitstringStatusListEntry", dto.type());
assertEquals(StatusListCredentialSubject.TYPE_ENTRY, dto.type());
}

@ParameterizedTest
Expand Down Expand Up @@ -89,7 +89,7 @@ void anyParameterIsBlank_ThrowsValidationException() {
"revocation",
"0",
"statusListCredential",
"BitstringStatusListEntry"))
StatusListCredentialSubject.TYPE_ENTRY))
.isEmpty());

assertFalse(
Expand All @@ -100,7 +100,7 @@ void anyParameterIsBlank_ThrowsValidationException() {
"revocation",
"0",
"", // statusListCredential is blank
"BitstringStatusListEntry"))
StatusListCredentialSubject.TYPE_ENTRY))
.isEmpty());
}

Expand All @@ -113,7 +113,7 @@ void invalidStatusPurpose_ThrowsIllegalArgumentException() {
IllegalArgumentException.class,
() -> {
new CredentialStatusDto(
"id", invalidPurpose, "0", "statusListCredential", "BitstringStatusListEntry");
"id", invalidPurpose, "0", "statusListCredential", StatusListCredentialSubject.TYPE_ENTRY);
});
}

Expand All @@ -137,14 +137,14 @@ void validStatusPurpose_DoesNotThrowException() {
assertDoesNotThrow(
() -> {
new CredentialStatusDto(
"id", validPurpose, "0", "statusListCredential", "BitstringStatusListEntry");
"id", validPurpose, "0", "statusListCredential", StatusListCredentialSubject.TYPE_ENTRY);
});
}

@Test
@DisplayName("type is valid")
void validType_DoesNotThrowException() {
String validType = "BitstringStatusListEntry";
String validType = StatusListCredentialSubject.TYPE_ENTRY;

assertDoesNotThrow(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void builderCreatesObjectWithCorrectValues() {
@Test
void defaultConstantsAreCorrect() {
// Assert
assertEquals("BitstringStatusListEntry", StatusListCredentialSubject.TYPE_ENTRY);
assertEquals("BitstringStatusList", StatusListCredentialSubject.TYPE_LIST);
assertEquals("StatusList2021Entry", StatusListCredentialSubject.TYPE_ENTRY);
assertEquals("StatusList2021Credential", StatusListCredentialSubject.TYPE_LIST);
assertEquals("id", StatusListCredentialSubject.SUBJECT_ID);
assertEquals("type", StatusListCredentialSubject.SUBJECT_TYPE);
assertEquals("statusPurpose", StatusListCredentialSubject.SUBJECT_STATUS_PURPOSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class TokenResponeTest {
class TokenResponseTest {
@Test
void getAndSetAccessToken() {
// Arrange
Expand All @@ -38,7 +38,7 @@ void getAndSetAccessToken() {

// Assert
String actualToken = tokenResponse.getAccessToken();
assertEquals(expectedToken, actualToken, "someAccessToken123");
assertEquals(expectedToken, "someAccessToken123", actualToken);
}

@Test
Expand All @@ -65,6 +65,6 @@ void setAccessTokenWithEmptyString() {

// Assert
String actualToken = tokenResponse.getAccessToken();
assertEquals(expectedToken, actualToken, "");
assertEquals(expectedToken, "", actualToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.tractusx.managedidentitywallets.revocation.dto.TokenResponse;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -91,10 +90,6 @@ public static void beforeAll() {
ReflectionTestUtils.setField(httpClientService, "miwUrl", wm1.baseUrl());
}

@BeforeEach
void setUp() {
}

@Test
void testGetBearerToken_Success() {
String expectedToken = "mockToken";
Expand Down Expand Up @@ -131,7 +126,7 @@ void testSignStatusListVC_Success() {
tokenResponse.setAccessToken("123456");
wm1.stubFor(post("/token").willReturn(jsonResponse(tokenResponse, 200)));
wm1.stubFor(
post("/api/credentials?isRevocable=false")
post("/api/credentials?revocable=false")
.willReturn(jsonResponse(statusListCredential.getCredential(), 200)));
VerifiableCredential signedCredential =
assertDoesNotThrow(
Expand Down
Loading

0 comments on commit 643493d

Please sign in to comment.