|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, 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 org.gridsuite.spreadsheetconfig.server; |
| 8 | + |
| 9 | +import org.assertj.core.api.WithAssertions; |
| 10 | +import org.gridsuite.spreadsheetconfig.server.constants.SheetType; |
| 11 | +import org.gridsuite.spreadsheetconfig.server.dto.CustomColumnDto; |
| 12 | +import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigDto; |
| 13 | +import org.gridsuite.spreadsheetconfig.server.entities.CustomColumnEntity; |
| 14 | +import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigEntity; |
| 15 | +import org.gridsuite.spreadsheetconfig.server.mapper.SpreadsheetConfigMapper; |
| 16 | +import org.junit.jupiter.api.Nested; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Achour BERRAHMA <achour.berrahma at rte-france.com> |
| 24 | + */ |
| 25 | +public class DtoConverterTest implements WithAssertions { |
| 26 | + |
| 27 | + private final SpreadsheetConfigMapper mapper = new SpreadsheetConfigMapper(); |
| 28 | + |
| 29 | + @Nested |
| 30 | + class SpreadsheetConfigConverterTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void testConversionToDtoOfSpreadsheetConfig() { |
| 34 | + UUID id = UUID.randomUUID(); |
| 35 | + SpreadsheetConfigEntity entity = SpreadsheetConfigEntity.builder() |
| 36 | + .id(id) |
| 37 | + .sheetType(SheetType.BATTERIES) |
| 38 | + .customColumns(Arrays.asList( |
| 39 | + CustomColumnEntity.builder().id(UUID.randomUUID()).name("Column1").formula("A+B").build(), |
| 40 | + CustomColumnEntity.builder().id(UUID.randomUUID()).name("Column2").formula("C*D").build() |
| 41 | + )) |
| 42 | + .build(); |
| 43 | + |
| 44 | + SpreadsheetConfigDto dto = mapper.toDto(entity); |
| 45 | + |
| 46 | + assertThat(dto) |
| 47 | + .as("DTO conversion result") |
| 48 | + .satisfies(d -> { |
| 49 | + assertThat(d.getId()).isEqualTo(id); |
| 50 | + assertThat(d.getSheetType()).isEqualTo(SheetType.BATTERIES); |
| 51 | + assertThat(d.getCustomColumns()).hasSize(2); |
| 52 | + assertThat(d.getCustomColumns().get(0).getName()).isEqualTo("Column1"); |
| 53 | + assertThat(d.getCustomColumns().get(0).getFormula()).isEqualTo("A+B"); |
| 54 | + assertThat(d.getCustomColumns().get(1).getName()).isEqualTo("Column2"); |
| 55 | + assertThat(d.getCustomColumns().get(1).getFormula()).isEqualTo("C*D"); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testConversionToEntityOfSpreadsheetConfig() { |
| 61 | + UUID id = UUID.randomUUID(); |
| 62 | + SpreadsheetConfigDto dto = SpreadsheetConfigDto.builder() |
| 63 | + .id(id) |
| 64 | + .sheetType(SheetType.BUSES) |
| 65 | + .customColumns(Arrays.asList( |
| 66 | + CustomColumnDto.builder().id(UUID.randomUUID()).name("Column1").formula("X+Y").build(), |
| 67 | + CustomColumnDto.builder().id(UUID.randomUUID()).name("Column2").formula("Z*W").build() |
| 68 | + )) |
| 69 | + .build(); |
| 70 | + |
| 71 | + SpreadsheetConfigEntity entity = mapper.toEntity(dto); |
| 72 | + |
| 73 | + assertThat(entity) |
| 74 | + .as("Entity conversion result") |
| 75 | + .satisfies(e -> { |
| 76 | + assertThat(e.getId()).isEqualTo(id); |
| 77 | + assertThat(e.getSheetType()).isEqualTo(SheetType.BUSES); |
| 78 | + assertThat(e.getCustomColumns()).hasSize(2); |
| 79 | + assertThat(e.getCustomColumns().get(0).getName()).isEqualTo("Column1"); |
| 80 | + assertThat(e.getCustomColumns().get(0).getFormula()).isEqualTo("X+Y"); |
| 81 | + assertThat(e.getCustomColumns().get(1).getName()).isEqualTo("Column2"); |
| 82 | + assertThat(e.getCustomColumns().get(1).getFormula()).isEqualTo("Z*W"); |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Nested |
| 88 | + class CustomColumnConverterTest { |
| 89 | + @Test |
| 90 | + void testConversionToDtoOfCustomColumn() { |
| 91 | + UUID id = UUID.randomUUID(); |
| 92 | + CustomColumnEntity entity = CustomColumnEntity.builder() |
| 93 | + .id(id) |
| 94 | + .name("TestColumn") |
| 95 | + .formula("A+B+C") |
| 96 | + .build(); |
| 97 | + |
| 98 | + CustomColumnDto dto = mapper.toCustomColumnDto(entity); |
| 99 | + |
| 100 | + assertThat(dto) |
| 101 | + .as("DTO conversion result") |
| 102 | + .satisfies(d -> { |
| 103 | + assertThat(d.getId()).isEqualTo(id); |
| 104 | + assertThat(d.getName()).isEqualTo("TestColumn"); |
| 105 | + assertThat(d.getFormula()).isEqualTo("A+B+C"); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testConversionToEntityOfCustomColumn() { |
| 111 | + UUID id = UUID.randomUUID(); |
| 112 | + CustomColumnDto dto = CustomColumnDto.builder() |
| 113 | + .id(id) |
| 114 | + .name("TestColumn") |
| 115 | + .formula("X*Y*Z") |
| 116 | + .build(); |
| 117 | + |
| 118 | + SpreadsheetConfigEntity spreadsheetConfig = new SpreadsheetConfigEntity(); |
| 119 | + CustomColumnEntity entity = mapper.toCustomColumnEntity(dto, spreadsheetConfig); |
| 120 | + |
| 121 | + assertThat(entity) |
| 122 | + .as("Entity conversion result") |
| 123 | + .satisfies(e -> { |
| 124 | + assertThat(e.getId()).isEqualTo(id); |
| 125 | + assertThat(e.getName()).isEqualTo("TestColumn"); |
| 126 | + assertThat(e.getFormula()).isEqualTo("X*Y*Z"); |
| 127 | + assertThat(e.getSpreadsheetConfig()).isEqualTo(spreadsheetConfig); |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments