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

import com.intrasoft.ermis.common.logging.ErmisLogger;
import com.intrasoft.ermis.common.xml.parsing.XmlConverter;
import com.intrasoft.ermis.ddntav5152.messages.CD165CType;
import com.intrasoft.ermis.transit.common.exceptions.BaseException;
import com.intrasoft.ermis.transit.officeofexitfortransit.domain.Message;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class InspectionServiceImpl implements InspectionService {

    private final DossierDataService dossierDataService;

    @Override
    public boolean isIE165Positive(String messageId) {

        try {

            Message message = dossierDataService.getMessageById(messageId);

            CD165CType cd165cType = XmlConverter.buildMessageFromXML(message.getPayload(), CD165CType.class);

            if (cd165cType.getTransitOperation().getRequestRejectionReasonCode() == null) {

                return true;
            } else {

                return cd165cType.getTransitOperation().getRequestRejectionReasonCode().isEmpty();
            }
        } catch (Exception e) {

            throw new BaseException("isIE165Positive(): ERROR!", e);
        }
    }
}

package com.intrasoft.ermis.transit.officeofexitfortransit.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.intrasoft.ermis.common.xml.enums.NamespacesEnum;
import com.intrasoft.ermis.common.xml.parsing.XmlConverter;
import com.intrasoft.ermis.ddntav5152.messages.CD165CType;
import com.intrasoft.ermis.ddntav5152.messages.MessageTypes;
import com.intrasoft.ermis.ddntav5152.messages.TransitOperationType41;
import com.intrasoft.ermis.transit.officeofexitfortransit.core.service.DossierDataService;
import com.intrasoft.ermis.transit.officeofexitfortransit.core.service.InspectionServiceImpl;
import com.intrasoft.ermis.transit.officeofexitfortransit.domain.Message;
import java.util.UUID;
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 InspectionServiceUT {

    @Mock
    private DossierDataService dossierDataService;

    @InjectMocks
    private InspectionServiceImpl inquiryService;

    @ParameterizedTest
    @ValueSource(booleans = {true, false})
    @DisplayName("Test - Is IE165 Positive")
    void testIsIE165Positive(boolean requestRejectionCodeExists) {

        System.setProperty("com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");

        Message message = createMessage(requestRejectionCodeExists);

        try {
            // Mocks:
            when(dossierDataService.getMessageById(any())).thenReturn(message);

            // Call InspectionService method:
            boolean expectedResult = inquiryService.isIE165Positive(UUID.randomUUID().toString());

            // Assertion:
            assertEquals(expectedResult, !requestRejectionCodeExists);
        } catch (Exception e) {

            fail();
        }
    }

    private Message createMessage(boolean requestRejectionCodeExists) {
        CD165CType cd165cTypeMessage = new CD165CType();
        cd165cTypeMessage.setTransitOperation(new TransitOperationType41());

        if (requestRejectionCodeExists) {

            cd165cTypeMessage.getTransitOperation().setRequestRejectionReasonCode("TEST");
        }

        Message message = new Message();
        message.setPayload(XmlConverter.marshal(cd165cTypeMessage, NamespacesEnum.NTA.getValue(), MessageTypes.CD_165_C.value()));

        return message;
    }
}
