|
| 1 | +package es.in2.issuer.backend.oidc4vci.domain.service.impl; |
| 2 | + |
| 3 | +import es.in2.issuer.backend.shared.domain.model.dto.CredentialProcedureIdAndTxCode; |
| 4 | +import es.in2.issuer.backend.shared.domain.model.dto.Grants; |
| 5 | +import es.in2.issuer.backend.shared.domain.model.dto.PreAuthorizedCodeResponse; |
| 6 | +import es.in2.issuer.backend.shared.domain.service.TranslationService; |
| 7 | +import es.in2.issuer.backend.shared.infrastructure.repository.CacheStore; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | +import reactor.test.StepVerifier; |
| 15 | + |
| 16 | +import java.lang.reflect.Method; |
| 17 | +import java.security.SecureRandom; |
| 18 | + |
| 19 | +import static es.in2.issuer.backend.oidc4vci.domain.util.Constants.TX_CODE_SIZE; |
| 20 | +import static es.in2.issuer.backend.oidc4vci.domain.util.Constants.TX_INPUT_MODE; |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class PreAuthorizedCodeServiceImplTest { |
| 25 | + |
| 26 | + @Mock |
| 27 | + private SecureRandom random; // Unused in these tests, needed by constructor |
| 28 | + @Mock |
| 29 | + private CacheStore<CredentialProcedureIdAndTxCode> cacheStore; // Unused here |
| 30 | + @Mock |
| 31 | + private TranslationService translationService; // Unused here |
| 32 | + |
| 33 | + private PreAuthorizedCodeServiceImpl service; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setUp() { |
| 37 | + service = new PreAuthorizedCodeServiceImpl(random, cacheStore, translationService); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void buildPreAuthorizedCodeResponse_createsExpectedPayload() throws Exception { |
| 42 | + // Given |
| 43 | + String preAuthorizedCode = "pre-auth-abc123"; |
| 44 | + String txCode = "567890"; |
| 45 | + |
| 46 | + // When |
| 47 | + Mono<PreAuthorizedCodeResponse> mono = invokeBuild(preAuthorizedCode, txCode); |
| 48 | + |
| 49 | + // Then |
| 50 | + StepVerifier.create(mono) |
| 51 | + .assertNext(resp -> { |
| 52 | + assertNotNull(resp, "Response must not be null"); |
| 53 | + // PreAuthorizedCodeResponse(pin=..., grants=...) |
| 54 | + assertEquals(txCode, resp.pin(), "pin in response must match input txCode"); |
| 55 | + |
| 56 | + Grants grants = resp.grants(); |
| 57 | + assertNotNull(grants, "grants must not be null"); |
| 58 | + assertEquals(preAuthorizedCode, grants.preAuthorizedCode(), |
| 59 | + "preAuthorizedCode in grants must match input"); |
| 60 | + |
| 61 | + Grants.TxCode grantTx = grants.txCode(); |
| 62 | + assertNotNull(grantTx, "grants.txCode must not be null"); |
| 63 | + assertEquals(TX_CODE_SIZE, grantTx.length(), |
| 64 | + "TxCode.length must match TX_CODE_SIZE"); |
| 65 | + assertEquals(TX_INPUT_MODE, grantTx.inputMode(), |
| 66 | + "TxCode.inputMode must match TX_INPUT_MODE"); |
| 67 | + // Not set in service; should be null |
| 68 | + assertNull(grantTx.description(), "TxCode.description should be null"); |
| 69 | + }) |
| 70 | + .verifyComplete(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void buildPreAuthorizedCodeResponse_emitsSingleValueAndCompletes() throws Exception { |
| 75 | + // Given |
| 76 | + String preAuthorizedCode = "pre-auth-xyz789"; |
| 77 | + String txCode = "123456"; |
| 78 | + |
| 79 | + // When |
| 80 | + Mono<PreAuthorizedCodeResponse> mono = invokeBuild(preAuthorizedCode, txCode); |
| 81 | + |
| 82 | + // Then |
| 83 | + StepVerifier.create(mono) |
| 84 | + .expectNextCount(1) |
| 85 | + .verifyComplete(); |
| 86 | + } |
| 87 | + |
| 88 | + // --- Helper to call the private method via reflection --- |
| 89 | + |
| 90 | + @SuppressWarnings("unchecked") |
| 91 | + private Mono<PreAuthorizedCodeResponse> invokeBuild(String preAuthorizedCode, String txCode) throws Exception { |
| 92 | + Method m = PreAuthorizedCodeServiceImpl.class |
| 93 | + .getDeclaredMethod("buildPreAuthorizedCodeResponse", String.class, String.class); |
| 94 | + m.setAccessible(true); |
| 95 | + return (Mono<PreAuthorizedCodeResponse>) m.invoke(service, preAuthorizedCode, txCode); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void buildPreAuthorizedCodeResponse_shouldBuildTxCodeProperly() throws Exception { |
| 100 | + // Given |
| 101 | + String preAuthorizedCode = "pre-auth-sonar"; |
| 102 | + String txCode = "999999"; |
| 103 | + |
| 104 | + // When |
| 105 | + Mono<PreAuthorizedCodeResponse> mono = invokeBuild(preAuthorizedCode, txCode); |
| 106 | + PreAuthorizedCodeResponse response = mono.block(); // Forces execution of Mono.just(...) |
| 107 | + |
| 108 | + // Then |
| 109 | + assertNotNull(response, "Response must not be null"); |
| 110 | + Grants.TxCode grantTx = response.grants().txCode(); |
| 111 | + assertNotNull(grantTx, "TxCode must not be null"); |
| 112 | + assertEquals(TX_CODE_SIZE, grantTx.length(), "Should match TX_CODE_SIZE constant"); |
| 113 | + assertEquals(TX_INPUT_MODE, grantTx.inputMode(), "Should match TX_INPUT_MODE constant"); |
| 114 | + } |
| 115 | +} |
0 commit comments