|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +package com.powsybl.caseserver.service; |
| 8 | + |
| 9 | +import com.powsybl.caseserver.dto.CaseInfos; |
| 10 | +import com.powsybl.caseserver.elasticsearch.DisableElasticsearch; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | + |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 18 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 19 | +import org.springframework.data.elasticsearch.core.IndexOperations; |
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.test.context.junit4.SpringRunner; |
| 22 | +import org.springframework.web.server.ResponseStatusException; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Antoine Bouhours <antoine.bouhours at rte-france.com> |
| 31 | + */ |
| 32 | +@RunWith(SpringRunner.class) |
| 33 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 34 | +@DisableElasticsearch |
| 35 | +class SupervisionServiceTest { |
| 36 | + |
| 37 | + @MockBean |
| 38 | + ElasticsearchOperations elasticsearchOperations; |
| 39 | + |
| 40 | + @MockBean |
| 41 | + IndexOperations indexOperations; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + SupervisionService supervisionService; |
| 45 | + |
| 46 | + @Test |
| 47 | + void recreateIndexThrowsExceptionWhenDeleteFails() { |
| 48 | + when(elasticsearchOperations.indexOps(CaseInfos.class)).thenReturn(indexOperations); |
| 49 | + when(indexOperations.delete()).thenReturn(false); |
| 50 | + |
| 51 | + ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> supervisionService.recreateIndex()); |
| 52 | + |
| 53 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exception.getStatusCode()); |
| 54 | + assertEquals("Failed to delete cases ElasticSearch index", exception.getReason()); |
| 55 | + verify(elasticsearchOperations, times(1)).indexOps(CaseInfos.class); |
| 56 | + verify(indexOperations, times(1)).delete(); |
| 57 | + verify(indexOperations, never()).createWithMapping(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void recreateIndexThrowsExceptionWhenCreateFails() { |
| 62 | + when(elasticsearchOperations.indexOps(CaseInfos.class)).thenReturn(indexOperations); |
| 63 | + when(indexOperations.delete()).thenReturn(true); |
| 64 | + when(indexOperations.createWithMapping()).thenReturn(false); |
| 65 | + |
| 66 | + ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> supervisionService.recreateIndex()); |
| 67 | + |
| 68 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exception.getStatusCode()); |
| 69 | + assertEquals("Failed to create cases ElasticSearch index", exception.getReason()); |
| 70 | + verify(elasticsearchOperations, times(1)).indexOps(CaseInfos.class); |
| 71 | + verify(indexOperations, times(1)).delete(); |
| 72 | + verify(indexOperations, times(1)).createWithMapping(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void recreateIndexSuccess() { |
| 77 | + when(elasticsearchOperations.indexOps(CaseInfos.class)).thenReturn(indexOperations); |
| 78 | + when(indexOperations.delete()).thenReturn(true); |
| 79 | + when(indexOperations.createWithMapping()).thenReturn(true); |
| 80 | + |
| 81 | + supervisionService.recreateIndex(); |
| 82 | + |
| 83 | + verify(elasticsearchOperations, times(1)).indexOps(CaseInfos.class); |
| 84 | + verify(indexOperations, times(1)).delete(); |
| 85 | + verify(indexOperations, times(1)).createWithMapping(); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterEach |
| 89 | + void verifyNoMoreInteractionsMocks() { |
| 90 | + verifyNoMoreInteractions(elasticsearchOperations); |
| 91 | + verifyNoMoreInteractions(indexOperations); |
| 92 | + } |
| 93 | +} |
0 commit comments