aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolderTest.java
blob: 10014c5f3cbc443c66d6f9353b9f51a2361303a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.onap.a1pesimulator.service.fileready;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;

import java.util.List;
import java.util.UUID;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.onap.a1pesimulator.data.fileready.EventMemoryHolder;
import org.onap.a1pesimulator.data.fileready.FileData;
import org.onap.a1pesimulator.data.fileready.FileReadyEvent;
import org.onap.a1pesimulator.exception.VesBrokerException;
import org.onap.a1pesimulator.service.ves.RanVesSender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import reactor.core.publisher.Mono;

class RanFileReadyHolderTest extends CommonFileReady {

    private RanFileReadyHolder ranFileReadyHolder;

    @Mock
    PMBulkFileService pmBulkFileService;

    @Mock
    RanVesSender ranVesSender;

    @Mock
    FtpServerService ftpServerService;

    @Mock
    FileReadyEventService fileReadyEventService;

    @BeforeEach
    void setUp() {
        super.setUp();
        ranFileReadyHolder = spy(new RanFileReadyHolder(ranVesSender, ftpServerService, pmBulkFileService, fileReadyEventService));
    }

    @Test
    void createPMBulkFileAndSendFileReadyMessage() {
        ListAppender<ILoggingEvent> appender = createCommonLogAndMock();

        ranFileReadyHolder.createPMBulkFileAndSendFileReadyMessage();
        assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage)
                .containsExactly("PM Bulk file was generated, uploaded to FTP and File ready event was send to VES Collector");
    }

    @Test
    void errorCreatePMBulkFileAndSendFileReadyMessage() {
        ListAppender<ILoggingEvent> appender = createCommonLogAndMock();
        doReturn(Mono.error(new Exception("error"))).when(fileReadyEventService).createFileReadyEventAndDeleteTmpFile(any());

        ranFileReadyHolder.createPMBulkFileAndSendFileReadyMessage();
        assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage).containsExactly("File ready event was unsuccessful: error");
    }

    @Test
    void saveEventToMemory() {
        ranFileReadyHolder = spy(new RanFileReadyHolder(ranVesSender, ftpServerService, pmBulkFileService, fileReadyEventService));
        try {
            ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), "Cell1", UUID.randomUUID().toString(), 30);
        } catch (VesBrokerException e) {
            e.printStackTrace();
        }
        assertThat(ranFileReadyHolder.getCollectedEvents()).hasSize(1);
    }

    @Test
    void errorSaveEventToMemory() throws VesBrokerException {
        doThrow(new VesBrokerException("error")).when(ranFileReadyHolder).saveEventToMemory(any(), any(), any(), any());

        Throwable exception = assertThrows(VesBrokerException.class,
                () -> ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), "Cell1", UUID.randomUUID().toString(), 30));
        assertThat(exception.getMessage()).contains("error");
        assertThat(ranFileReadyHolder.getCollectedEvents()).isEmpty();
    }

    @Test
    void getCollectedEvents() {
        List<EventMemoryHolder> collectedEvents = ranFileReadyHolder.getCollectedEvents();
        assertNotNull(collectedEvents);
    }

    /**
     * Creates common Log and Mocks
     *
     * @return ListAppender<ILoggingEvent>
     */
    private ListAppender<ILoggingEvent> createCommonLogAndMock() {
        ListAppender<ILoggingEvent> appender = createCommonLog(RanFileReadyHolder.class);

        List<EventMemoryHolder> collectedEvents = getTestEvents();
        FileData testFileData = FileData.builder().pmBulkFile(createTempFile(PM_BULK_FILE)).build();

        doReturn(collectedEvents).when(ranFileReadyHolder).getCollectedEvents();
        doReturn(Mono.just(testFileData)).when(pmBulkFileService).generatePMBulkFileXml(collectedEvents);
        testFileData.setArchivedPmBulkFile(createTempFile(ARCHIVED_PM_BULK_FILE));
        doReturn(Mono.just(testFileData)).when(ftpServerService).uploadFileToFtp(any());
        testFileData.setFileReadyEvent(new FileReadyEvent());
        doReturn(Mono.just(testFileData)).when(fileReadyEventService).createFileReadyEventAndDeleteTmpFile(any());
        doReturn(Mono.just(HttpStatus.SC_ACCEPTED)).when(ranVesSender).send(any());
        return appender;
    }
}