BJ-847: Fix if-mess

This commit is contained in:
a.romanov 2020-03-31 10:24:31 +03:00
parent f179026fa6
commit 7343ab9172
2 changed files with 110 additions and 32 deletions

View File

@ -9,7 +9,7 @@ import lombok.SneakyThrows;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Optional;
@Service
@RequiredArgsConstructor
@ -24,41 +24,68 @@ public class ClaimCommitterServerHandler implements ClaimCommitterSrv.Iface {
}
@Override
@SneakyThrows
public void commit(String partyId, Claim claim) {
for (ModificationUnit modificationUnit : claim.getChangeset()) {
claim.getChangeset()
.forEach(modificationUnit -> findCashRegister(partyId, modificationUnit)
.ifPresent(cashRegisterRepository::save));
}
private Optional<CashRegister> findCashRegister(String partyId, ModificationUnit modificationUnit) {
CashRegister.CashRegisterBuilder builder = CashRegister.builder();
builder.partyId(partyId);
Modification modification = modificationUnit.getModification();
return findInPartyModification(builder, modification);
}
if (modification.isSetPartyModification()) {
PartyModification partyModification = modification.getPartyModification();
private Optional<CashRegister> findInPartyModification(
CashRegister.CashRegisterBuilder builder,
Modification modification) {
if (!modification.isSetPartyModification()) {
return Optional.empty();
}
return findInShopModification(modification.getPartyModification(), builder);
}
private Optional<CashRegister> findInShopModification(
PartyModification partyModification,
CashRegister.CashRegisterBuilder builder) {
if (!partyModification.isSetShopModification()) {
return Optional.empty();
}
if (partyModification.isSetShopModification()) {
ShopModificationUnit shopModificationUnit = partyModification.getShopModification();
String shopId = shopModificationUnit.getId();
ShopModification shopModification = shopModificationUnit.getModification();
builder.shopId(shopModificationUnit.getId());
return findInCashRegisterModification(shopModificationUnit.getModification(), builder);
}
private Optional<CashRegister> findInCashRegisterModification(
ShopModification shopModification,
CashRegister.CashRegisterBuilder builder) {
if (!shopModification.isSetCashRegisterModificationUnit()) {
return Optional.empty();
}
if (shopModification.isSetCashRegisterModificationUnit()) {
CashRegisterModificationUnit cashRegisterModificationUnit = shopModification.getCashRegisterModificationUnit();
String cashRegisterId = cashRegisterModificationUnit.getId();
CashRegisterModification cashRegisterModification = cashRegisterModificationUnit.getModification();
builder.id(cashRegisterModificationUnit.getId());
return findInCreationModification(cashRegisterModificationUnit.getModification(), builder);
}
@SneakyThrows
private Optional<CashRegister> findInCreationModification(
CashRegisterModification cashRegisterModification,
CashRegister.CashRegisterBuilder builder) {
if (!cashRegisterModification.isSetCreation()) {
return Optional.empty();
}
if (cashRegisterModification.isSetCreation()) {
CashRegisterParams cashRegisterParams = cashRegisterModification.getCreation();
int providerId = cashRegisterParams.getCashRegisterProviderId();
Map<String, String> providerParams = cashRegisterParams.getCashRegisterProviderParams();
CashRegister cashRegister = CashRegister.builder()
.id(cashRegisterId)
.partyId(partyId)
.shopId(shopId)
.providerId(providerId)
.providerParams(objectMapper.writeValueAsString(providerParams))
.build();
builder.providerId(cashRegisterParams.getCashRegisterProviderId());
builder.providerParams(objectMapper.writeValueAsString(cashRegisterParams.getCashRegisterProviderParams()));
cashRegisterRepository.save(cashRegister);
}
}
}
}
}
return Optional.ofNullable(builder.build());
}
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.rbkmoney.cashier.domain.CashRegister;
import com.rbkmoney.cashier.repository.CashRegisterRepository;
import com.rbkmoney.damsel.claim_management.*;
import com.rbkmoney.damsel.domain.CategoryRef;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@ -63,4 +64,54 @@ public class ClaimCommitterServerHandlerTest {
assertThat(cashRegister).isEqualTo(expected);
}
@Test
public void shouldHandleClaimWithNoPartyModification() {
// Given
Claim claim = new Claim()
.setChangeset(List.of(new ModificationUnit()
.setModification(Modification.claim_modification(new ClaimModification()))));
// When
claimCommitterServerHandler.commit("party-id", claim);
// Then
verify(cashRegisterRepository, never())
.save(any());
}
@Test
public void shouldHandleClaimWithNoShopModification() {
// Given
Claim claim = new Claim()
.setChangeset(List.of(new ModificationUnit()
.setModification(Modification.party_modification(
PartyModification.contract_modification(new ContractModificationUnit())))));
// When
claimCommitterServerHandler.commit("party-id", claim);
// Then
verify(cashRegisterRepository, never())
.save(any());
}
@Test
public void shouldHandleClaimWithNoCashRegisterModification() {
// Given
Claim claim = new Claim()
.setChangeset(List.of(new ModificationUnit()
.setModification(Modification.party_modification(
PartyModification.shop_modification(new ShopModificationUnit()
.setId("shop-id")
.setModification(
ShopModification.category_modification(new CategoryRef())))))));
// When
claimCommitterServerHandler.commit("party-id", claim);
// Then
verify(cashRegisterRepository, never())
.save(any());
}
}