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

import com.intrasoft.ermis.transit.cargoofficeofdestination.domain.ValidationResult;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ArrivalPresentationServiceImpl implements ArrivalPresentationService {

    private final DossierDataService dossierDataService;

    private static final String RULE_ID = "CARGO_ARRIVAL_PRESENTATION_TIMER_EXPIRATION";

    private static final String ERROR_CODE = "N/A";

    private static final String ERROR_POINTER = "/TB015C/preparationDateAndTime";

    @Override
    public void handleArrivalPresentationTimeout(String messageId) {
        // Persist rejecting Validation Result due to Cargo Arrival Presentation timer expiration:
        List<ValidationResult> validationResultList = new ArrayList<>();
        ValidationResult rejectingValidationResult = ValidationResult.builder()
                                                                     .messageId(messageId)
                                                                     .createdDateTime(LocalDateTime.now(ZoneOffset.UTC))
                                                                     .rejecting(true)
                                                                     .ruleId(RULE_ID)
                                                                     .errorCode(ERROR_CODE)
                                                                     .pointer(ERROR_POINTER)
                                                                     .build();
        validationResultList.add(rejectingValidationResult);
        dossierDataService.saveValidationResultList(validationResultList);
    }
}

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

import static org.mockito.Mockito.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.intrasoft.ermis.transit.cargoofficeofdestination.core.service.ArrivalPresentationServiceImpl;
import com.intrasoft.ermis.transit.cargoofficeofdestination.core.service.DossierDataService;
import com.intrasoft.ermis.transit.cargoofficeofdestination.domain.ValidationResult;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ArrivalPresentationServiceUT {

    private static final String MESSAGE_ID = "TEST_MESSAGE_ID";

    private static final String RULE_ID = "CARGO_ARRIVAL_PRESENTATION_TIMER_EXPIRATION";

    private static final String ERROR_CODE = "N/A";

    private static final String ERROR_POINTER = "/TB015C/preparationDateAndTime";

    @Mock
    private DossierDataService dossierDataService;

    @InjectMocks
    private ArrivalPresentationServiceImpl arrivalPresentationService;

    @Captor
    private ArgumentCaptor<List<ValidationResult>> validationResultListArgumentCaptor;

    @Test
    @DisplayName("Test - Handle Arrival Presentation Timeout")
    void testHandleArrivalPresentationTimeout() {
        // Call ArrivalPresentationService method:
        arrivalPresentationService.handleArrivalPresentationTimeout(MESSAGE_ID);
        // Verification & Assertions:
        verify(dossierDataService).saveValidationResultList(validationResultListArgumentCaptor.capture());
        ValidationResult validationResultActual = validationResultListArgumentCaptor.getValue().get(0);
        assertEquals(MESSAGE_ID, validationResultActual.getMessageId());
        assertEquals(RULE_ID, validationResultActual.getRuleId());
        assertEquals(ERROR_CODE, validationResultActual.getErrorCode());
        assertEquals(ERROR_POINTER, validationResultActual.getPointer());
    }
}
