Skip to content

Commit a0f8ad5

Browse files
committed
add tests
Signed-off-by: achour94 <[email protected]>
1 parent a570fb9 commit a0f8ad5

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 jakarta.persistence.EntityNotFoundException;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.ControllerAdvice;
13+
import org.springframework.web.bind.annotation.ExceptionHandler;
14+
import org.springframework.web.context.request.WebRequest;
15+
16+
/**
17+
* @author Achour BERRAHMA <achour.berrahma at rte-france.com>
18+
*/
19+
@ControllerAdvice
20+
public class RestResponseEntityExceptionHandler {
21+
22+
@ExceptionHandler(EntityNotFoundException.class)
23+
protected ResponseEntity<Object> handleEntityNotFound(
24+
EntityNotFoundException ex, WebRequest request) {
25+
String bodyOfResponse = ex.getMessage();
26+
return new ResponseEntity<>(bodyOfResponse, HttpStatus.NOT_FOUND);
27+
}
28+
}

src/test/java/org/gridsuite/spreadsheetconfig/server/SpreadsheetConfigControllerTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ void testCreate() throws Exception {
7575
assertThat(createdConfig.getCustomColumns()).allMatch(column -> column.getId() != null);
7676
}
7777

78+
@Test
79+
void testCreateWithInvalidData() throws Exception {
80+
SpreadsheetConfigDto invalidConfig = SpreadsheetConfigDto.builder()
81+
.sheetType(null) // SheetType is required
82+
.customColumns(createCustomColumns())
83+
.build();
84+
85+
String invalidConfigJson = mapper.writeValueAsString(invalidConfig);
86+
87+
mockMvc.perform(post(URI_SPREADSHEET_CONFIG_BASE)
88+
.content(invalidConfigJson)
89+
.contentType(MediaType.APPLICATION_JSON))
90+
.andExpect(status().isBadRequest());
91+
}
92+
7893
@Test
7994
void testRead() throws Exception {
8095
SpreadsheetConfigDto configToRead = SpreadsheetConfigDto.builder()
@@ -94,6 +109,37 @@ void testRead() throws Exception {
94109
assertThat(receivedConfig.getCustomColumns()).allMatch(column -> column.getId() != null);
95110
}
96111

112+
@Test
113+
void testReadNonExistent() throws Exception {
114+
UUID nonExistentUuid = UUID.randomUUID();
115+
116+
mockMvc.perform(get(URI_SPREADSHEET_CONFIG_GET_PUT + nonExistentUuid))
117+
.andExpect(status().isNotFound());
118+
}
119+
120+
@Test
121+
void testUpdateWithInvalidData() throws Exception {
122+
SpreadsheetConfigDto configToUpdate = SpreadsheetConfigDto.builder()
123+
.sheetType(SheetType.BATTERIES)
124+
.customColumns(createCustomColumns())
125+
.build();
126+
127+
UUID configUuid = saveAndReturnId(configToUpdate);
128+
129+
SpreadsheetConfigDto invalidUpdate = SpreadsheetConfigDto.builder()
130+
.id(configUuid)
131+
.sheetType(null) // SheetType is required
132+
.customColumns(createUpdatedCustomColumns())
133+
.build();
134+
135+
String invalidUpdateJson = mapper.writeValueAsString(invalidUpdate);
136+
137+
mockMvc.perform(put(URI_SPREADSHEET_CONFIG_GET_PUT + configUuid)
138+
.content(invalidUpdateJson)
139+
.contentType(MediaType.APPLICATION_JSON))
140+
.andExpect(status().isBadRequest());
141+
}
142+
97143
@Test
98144
void testUpdate() throws Exception {
99145
SpreadsheetConfigDto configToUpdate = SpreadsheetConfigDto.builder()
@@ -141,6 +187,14 @@ void testDelete() throws Exception {
141187
assertThat(storedConfigs).isEmpty();
142188
}
143189

190+
@Test
191+
void testDeleteNonExistent() throws Exception {
192+
UUID nonExistentUuid = UUID.randomUUID();
193+
194+
mockMvc.perform(delete(URI_SPREADSHEET_CONFIG_GET_PUT + nonExistentUuid))
195+
.andExpect(status().isNotFound());
196+
}
197+
144198
@Test
145199
void testGetAll() throws Exception {
146200
SpreadsheetConfigDto config1 = SpreadsheetConfigDto.builder()
@@ -182,6 +236,15 @@ void testDuplicate() throws Exception {
182236
assertThat(duplicatedConfig.getId()).isNotEqualTo(configUuid);
183237
}
184238

239+
@Test
240+
void testDuplicateNonExistent() throws Exception {
241+
UUID nonExistentUuid = UUID.randomUUID();
242+
243+
mockMvc.perform(post(URI_SPREADSHEET_CONFIG_BASE + "/duplicate")
244+
.queryParam("duplicateFrom", nonExistentUuid.toString()))
245+
.andExpect(status().isNotFound());
246+
}
247+
185248
private List<CustomColumnDto> createCustomColumns() {
186249
return Arrays.asList(
187250
new CustomColumnDto(null, "cust_a", "cust_b + cust_c"),

0 commit comments

Comments
 (0)