Merge salary signing fix
commit
5af0e857b8
@ -0,0 +1,90 @@
|
||||
package com.yau.digitalrmb.corporatewallet.application;
|
||||
|
||||
import com.yau.digitalrmb.corporatewallet.domain.CorporateWalletApplication;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.CorporateWalletApplicationRepository;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.SalaryBatch;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.SalaryBatchRepository;
|
||||
import com.yau.digitalrmb.institutionidentity.application.InstitutionKeySubject;
|
||||
import com.yau.digitalrmb.institutionidentity.domain.InstitutionIdentityCryptography;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SalaryBatchServiceTest {
|
||||
private static final InstitutionKeySubject SUBJECT =
|
||||
new InstitutionKeySubject("salary-user", 1001L, 2001L);
|
||||
private static final String PRIVATE_KEY = "OPERATOR_PRIVATE_KEY";
|
||||
|
||||
@Mock private SalaryBatchRepository repository;
|
||||
@Mock private CorporateWalletApplicationRepository corporateWalletRepository;
|
||||
@Mock private InstitutionIdentityCryptography cryptography;
|
||||
@Mock private SalaryScoreService scoreService;
|
||||
@Mock private CorporateWalletKeyService keyService;
|
||||
@InjectMocks private SalaryBatchService service;
|
||||
|
||||
@Test
|
||||
void startsFreshBatchAndSignsCurrentDigestWhenWorkflowRunsAgain() {
|
||||
AtomicReference<SalaryBatch> latest = new AtomicReference<>();
|
||||
AtomicLong ids = new AtomicLong(10L);
|
||||
when(repository.save(any(SalaryBatch.class))).thenAnswer(invocation -> {
|
||||
SalaryBatch batch = invocation.getArgument(0);
|
||||
batch.setId(ids.incrementAndGet());
|
||||
latest.set(batch);
|
||||
return batch;
|
||||
});
|
||||
when(repository.findLatest(SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenAnswer(invocation -> Optional.ofNullable(latest.get()));
|
||||
|
||||
CorporateWalletApplication wallet = CorporateWalletApplication.create(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId());
|
||||
wallet.setWalletId("CORP_WALLET_001");
|
||||
when(corporateWalletRepository.findLatest(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.of(wallet));
|
||||
when(cryptography.sm3(any(String.class))).thenReturn("DIGEST_ONE", "DIGEST_TWO");
|
||||
when(keyService.getOrCreatePrivateKey(
|
||||
any(InstitutionKeySubject.class), eq("数币操作员私钥"), eq("王五")))
|
||||
.thenReturn(PRIVATE_KEY);
|
||||
when(cryptography.sign(PRIVATE_KEY, "DIGEST_ONE")).thenReturn("SIGNATURE_ONE");
|
||||
when(cryptography.sign(PRIVATE_KEY, "DIGEST_TWO")).thenReturn("SIGNATURE_TWO");
|
||||
|
||||
service.fetchBlockchain(SUBJECT, "tester");
|
||||
service.concatenate(SUBJECT);
|
||||
SalaryDigestResult firstDigest = service.computeDigest(SUBJECT);
|
||||
SalaryBatch firstBatch = latest.get();
|
||||
assertThat(firstBatch.getStatus()).isEqualTo(SalaryBatch.Status.DIGESTED);
|
||||
SalarySignatureResult firstSignature = service.sign(SUBJECT, PRIVATE_KEY);
|
||||
|
||||
service.fetchBlockchain(SUBJECT, "tester");
|
||||
service.concatenate(SUBJECT);
|
||||
SalaryDigestResult secondDigest = service.computeDigest(SUBJECT);
|
||||
SalaryBatch secondBatch = latest.get();
|
||||
assertThat(secondBatch.getStatus()).isEqualTo(SalaryBatch.Status.DIGESTED);
|
||||
SalarySignatureResult secondSignature = service.sign(SUBJECT, PRIVATE_KEY);
|
||||
|
||||
assertThat(secondBatch).isNotSameAs(firstBatch);
|
||||
assertThat(secondBatch.getId()).isNotEqualTo(firstBatch.getId());
|
||||
assertThat(firstDigest.getDigestValue()).isEqualTo("DIGEST_ONE");
|
||||
assertThat(secondDigest.getDigestValue()).isEqualTo("DIGEST_TWO");
|
||||
assertThat(firstSignature.getSignature()).isEqualTo("SIGNATURE_ONE");
|
||||
assertThat(secondSignature.getSignature()).isEqualTo("SIGNATURE_TWO");
|
||||
|
||||
verify(repository, times(2)).save(any(SalaryBatch.class));
|
||||
verify(cryptography).sign(PRIVATE_KEY, "DIGEST_ONE");
|
||||
verify(cryptography).sign(PRIVATE_KEY, "DIGEST_TWO");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue