package com.intrasoft.ermis.transit.cargoofficeofdestination.core.service;

import com.intrasoft.ermis.cargov1.messages.TB015CType;
import com.intrasoft.ermis.common.xml.parsing.XmlConverter;
import com.intrasoft.ermis.transit.cargoofficeofdestination.domain.Message;
import com.intrasoft.ermis.transit.common.enums.AdditionalDeclarationTypeEnum;
import javax.xml.bind.JAXBException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DeclarationRegistrationServiceImpl implements DeclarationRegistrationService {

    private final DossierDataService dossierDataService;

    @Override
    public boolean isDeclarationPrelodged(String messageId) throws JAXBException {
        Message tb015c = dossierDataService.getMessageById(messageId);
        TB015CType tb015cType = XmlConverter.buildMessageFromXML(tb015c.getPayload(), TB015CType.class);
        return AdditionalDeclarationTypeEnum.DECLARATION_TYPE_D.getType().equals(tb015cType.getTransitOperation().getAdditionalDeclarationType());
    }
}

package com.intrasoft.ermis.transit.cargoofficeofdestination.test.service;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import com.intrasoft.ermis.cargov1.messages.MessageTypes;
import com.intrasoft.ermis.cargov1.messages.TB015CType;
import com.intrasoft.ermis.common.xml.parsing.XmlConverter;
import com.intrasoft.ermis.transit.cargoofficeofdestination.core.service.DeclarationRegistrationServiceImpl;
import com.intrasoft.ermis.transit.cargoofficeofdestination.core.service.DossierDataService;
import com.intrasoft.ermis.transit.cargoofficeofdestination.test.util.TestDataUtilities;
import com.intrasoft.ermis.transit.common.enums.AdditionalDeclarationTypeEnum;
import com.intrasoft.ermis.transit.shared.testing.util.FileHelpers;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DeclarationRegistrationServiceUT {

    private static final String MESSAGE_ID = UUID.randomUUID().toString();

    @Mock
    private DossierDataService dossierDataService;

    @InjectMocks
    private DeclarationRegistrationServiceImpl declarationRegistrationService;

    @ParameterizedTest
    @DisplayName("Test if the TB015C declaration is prelodged ")
    @ValueSource(strings = {"A", "D"})
    void test_isDeclarationPrelodged(String additionalDeclarationType) throws JAXBException {

        String tb015cStringXml = FileHelpers.xmlToString(MessageTypes.TB_015_C.value() + ".xml");
        TB015CType tb015CType = XmlConverter.buildMessageFromXML(tb015cStringXml, TB015CType.class);

        tb015CType.getTransitOperation().setAdditionalDeclarationType(additionalDeclarationType);

        String updatedXmlString = XmlConverter.marshal(tb015CType);

        when(dossierDataService.getMessageById(MESSAGE_ID)).thenReturn(
                TestDataUtilities.createMessage(MESSAGE_ID, MessageTypes.TB_015_C.value(), updatedXmlString));

        boolean isPrelodged = declarationRegistrationService.isDeclarationPrelodged(MESSAGE_ID);

        if (AdditionalDeclarationTypeEnum.DECLARATION_TYPE_D.getType().equals(additionalDeclarationType)) {
            assertTrue(isPrelodged);
        } else {
            assertFalse(isPrelodged);
        }
    }
}
