Skip to content

Commit 0dda661

Browse files
author
Dennis Labordus
committed
Fixed bug regarding no data found errors.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent d71d695 commit 0dda661

File tree

7 files changed

+167
-16
lines changed

7 files changed

+167
-16
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.rest.exception;
5+
6+
import org.lfenergy.compas.core.rest.model.ErrorResponse;
7+
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
8+
9+
import javax.ws.rs.core.Response;
10+
import javax.ws.rs.ext.ExceptionMapper;
11+
import javax.ws.rs.ext.Provider;
12+
13+
@Provider
14+
public class CompasNoDataFoundExceptionHandler implements ExceptionMapper<CompasNoDataFoundException> {
15+
@Override
16+
public Response toResponse(CompasNoDataFoundException exception) {
17+
var response = new ErrorResponse();
18+
response.addErrorMessage(exception.getErrorCode(), exception.getMessage());
19+
return Response.status(Response.Status.NOT_FOUND).entity(response).build();
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.rest.exception;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.lfenergy.compas.core.rest.model.ErrorResponse;
8+
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
9+
10+
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNull;
13+
14+
class CompasNoDataFoundExceptionHandlerTest {
15+
@Test
16+
void toResponse_WhenCalledWithException_ThenNotFoundReturnedWithBody() {
17+
var exception = new CompasNoDataFoundException("Some message that will only be logged");
18+
var handler = new CompasNoDataFoundExceptionHandler();
19+
20+
var result = handler.toResponse(exception);
21+
assertEquals(NOT_FOUND.getStatusCode(), result.getStatus());
22+
var errorMessage = ((ErrorResponse) result.getEntity()).getErrorMessages().get(0);
23+
assertEquals(exception.getErrorCode(), errorMessage.getCode());
24+
assertEquals(exception.getMessage(), errorMessage.getMessage());
25+
assertNull(errorMessage.getProperty());
26+
}
27+
}

repository-basex/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataBaseXRepository.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.lfenergy.compas.core.commons.ElementConverter;
77
import org.lfenergy.compas.scl.data.basex.BaseXClient;
88
import org.lfenergy.compas.scl.data.basex.BaseXClientFactory;
9+
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
910
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
1011
import org.lfenergy.compas.scl.data.model.Item;
1112
import org.lfenergy.compas.scl.data.model.SclType;
@@ -46,7 +47,7 @@ public class CompasSclDataBaseXRepository implements CompasSclDataRepository {
4647
private static final String DECLARE_COMPAS_NAMESPACE = "declare namespace compas=\"" + COMPAS_EXTENSION_NS_URI + "\";\n";
4748
private static final String DECLARE_LATEST_VERSION_FUNC =
4849
"declare function local:latest-version($db as xs:string, $id as xs:string)\n" +
49-
" as document-node() { \n" +
50+
" as document-node()? { \n" +
5051
" let $doc :=\n" +
5152
" (for $resource in db:open($db, $id)\n" +
5253
" let $parts := tokenize($resource/scl:SCL/scl:Header/@version, '\\.')\n" +
@@ -60,6 +61,7 @@ public class CompasSclDataBaseXRepository implements CompasSclDataRepository {
6061
"};\n";
6162
private static final String DECLARE_DB_VARIABLE = "declare variable $db := '%s';\n";
6263
private static final String DECLARE_ID_VARIABLE = "declare variable $id := '%s';\n";
64+
private static final String DECLARE_PATH_VARIABLE = "declare variable $path := '%s';\n";
6365

6466
private final BaseXClientFactory baseXClientFactory;
6567
private final SclDataModelMarshaller sclDataMarshaller;
@@ -97,13 +99,12 @@ public List<Item> list(SclType type) {
9799
" let $version := $latestScl//scl:SCL/scl:Header/@version\n" +
98100
" let $name := $latestScl//scl:SCL/scl:Private[@type='" + COMPAS_SCL_EXTENSION_TYPE + "']/compas:" + COMPAS_SCL_NAME_EXTENSION + "\n" +
99101
" return '<Item xmlns=\"" + SCL_DATA_SERVICE_V1_NS_URI + "\"><Id>' || $id || '</Id><Name>' || $name || '</Name><Version>' || $version || '</Version></Item>'",
100-
sclDataMarshaller::unmarshal
101-
);
102+
sclDataMarshaller::unmarshal);
102103
}
103104

104105
@Override
105106
public List<Item> listVersionsByUUID(SclType type, UUID id) {
106-
return executeQuery(type, DECLARE_SCL_NS_URI +
107+
var items = executeQuery(type, DECLARE_SCL_NS_URI +
107108
DECLARE_COMPAS_NAMESPACE +
108109
format(DECLARE_DB_VARIABLE, type) +
109110
format(DECLARE_ID_VARIABLE, id) +
@@ -117,8 +118,12 @@ public List<Item> listVersionsByUUID(SclType type, UUID id) {
117118
" let $patchVersion := xs:int($parts[3])\n" +
118119
" order by $majorVersion, $minorVersion, $patchVersion\n" +
119120
" return '<Item xmlns=\"" + SCL_DATA_SERVICE_V1_NS_URI + "\"><Id>' || $id || '</Id><Name>' || $name || '</Name><Version>' || $version || '</Version></Item>' ",
120-
sclDataMarshaller::unmarshal
121-
);
121+
sclDataMarshaller::unmarshal);
122+
123+
if (items.isEmpty()) {
124+
throw new CompasNoDataFoundException("No versions found for type '" + type + "' with ID '" + id + "'");
125+
}
126+
return items;
122127
}
123128

124129
@Override
@@ -133,15 +138,24 @@ public Element findByUUID(SclType type, UUID id) {
133138
format(DECLARE_ID_VARIABLE, id) +
134139
"local:latest-version($db, $id)",
135140
xmlString -> elementConverter.convertToElement(xmlString, SCL_ELEMENT_NAME, SCL_NS_URI));
141+
142+
if (result.isEmpty()) {
143+
throw new CompasNoDataFoundException("No record found for type '" + type + "' with ID '" + id + "'");
144+
}
136145
return result.get(0);
137146
}
138147

139148
@Override
140149
public Element findByUUID(SclType type, UUID id, Version version) {
141150
// This find method searches for a specific version.
142-
var result = executeQuery(type,
143-
"doc('" + type + "/" + createDocumentPath(id, version) + "')",
151+
var result = executeQuery(type, format(DECLARE_DB_VARIABLE, type) +
152+
format(DECLARE_PATH_VARIABLE, createDocumentPath(id, version)) +
153+
"db:open($db, $path)",
144154
xmlString -> elementConverter.convertToElement(xmlString, SCL_ELEMENT_NAME, SCL_NS_URI));
155+
156+
if (result.isEmpty()) {
157+
throw new CompasNoDataFoundException("No record found for type '" + type + "' with ID '" + id + "' and version '" + version + "'");
158+
}
145159
return result.get(0);
146160
}
147161

repository-basex/src/test/java/org/lfenergy/compas/scl/data/repository/CompasSclDataBaseXRepositoryTest.java

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.lfenergy.compas.core.commons.ElementConverter;
1111
import org.lfenergy.compas.scl.data.basex.BaseXClientFactory;
1212
import org.lfenergy.compas.scl.data.basex.BaseXServerJUnitExtension;
13+
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
1314
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
1415
import org.lfenergy.compas.scl.data.model.ChangeSetType;
1516
import org.lfenergy.compas.scl.data.model.SclType;
@@ -24,8 +25,8 @@
2425
import static org.junit.jupiter.api.Assertions.*;
2526
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.*;
2627
import static org.lfenergy.compas.scl.data.basex.BaseXServerUtil.createClientFactory;
27-
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.BASEX_QUERY_ERROR_CODE;
2828
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.HEADER_NOT_FOUND_ERROR_CODE;
29+
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_DATA_FOUND_ERROR_CODE;
2930

3031
@ExtendWith({MockitoExtension.class, BaseXServerJUnitExtension.class})
3132
class CompasSclDataBaseXRepositoryTest {
@@ -106,6 +107,16 @@ void list_WhenTwoVersionsOfARecordAdded_ThenLatestRecordFound() {
106107
assertEquals(version.toString(), items.get(0).getVersion());
107108
}
108109

110+
@Test
111+
void listVersionsByUUID_WhenCalledWithUnknownID_ThenExceptionIsThrown() {
112+
var uuid = UUID.randomUUID();
113+
114+
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
115+
repository.listVersionsByUUID(TYPE, uuid);
116+
});
117+
assertEquals(NO_DATA_FOUND_ERROR_CODE, exception.getErrorCode());
118+
}
119+
109120
@Test
110121
void listVersionsByUUID_WhenTwoVersionsOfARecordAdded_ThenAllRecordAreFound() {
111122
var version = new Version(1, 0, 0);
@@ -125,13 +136,60 @@ void listVersionsByUUID_WhenTwoVersionsOfARecordAdded_ThenAllRecordAreFound() {
125136
}
126137

127138
@Test
128-
void find_WhenCalledWithUnknownUUID_ThenExceptionIsThrown() {
139+
void findByUUID_WhenCalledWithUnknownUUID_ThenExceptionIsThrown() {
129140
var uuid = UUID.randomUUID();
130141

131-
var exception = assertThrows(CompasSclDataServiceException.class, () -> {
142+
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
132143
repository.findByUUID(TYPE, uuid);
133144
});
134-
assertEquals(BASEX_QUERY_ERROR_CODE, exception.getErrorCode());
145+
assertEquals(NO_DATA_FOUND_ERROR_CODE, exception.getErrorCode());
146+
}
147+
148+
@Test
149+
void findByUUID_WhenTwoVersionsOfARecordAdded_ThenLastRecordIsFound() {
150+
var version = new Version(1, 0, 0);
151+
var uuid = UUID.randomUUID();
152+
var scl = readSCL(uuid, version);
153+
repository.create(TYPE, uuid, scl, version);
154+
version = new Version(1, 1, 0);
155+
scl = readSCL(uuid, version);
156+
repository.create(TYPE, uuid, scl, version);
157+
158+
var foundScl = repository.findByUUID(TYPE, uuid);
159+
160+
assertNotNull(foundScl);
161+
assertEquals(uuid.toString(), getIdFromHeader(foundScl));
162+
assertEquals(version.toString(), getVersionFromHeader(foundScl));
163+
}
164+
165+
@Test
166+
void findByUUIDWithVersion_WhenCalledWithUnknownVersion_ThenExceptionIsThrown() {
167+
var version = new Version(1, 0, 0);
168+
var uuid = UUID.randomUUID();
169+
var scl = readSCL(uuid, version);
170+
repository.create(TYPE, uuid, scl, version);
171+
172+
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
173+
repository.findByUUID(TYPE, uuid, new Version(1, 1, 1));
174+
});
175+
assertEquals(NO_DATA_FOUND_ERROR_CODE, exception.getErrorCode());
176+
}
177+
178+
@Test
179+
void findByUUIDWithVersion_WhenTwoVersionsOfARecordAdded_ThenCorrectRecordIsFound() {
180+
var expectedVersion = new Version(1, 0, 0);
181+
var uuid = UUID.randomUUID();
182+
var scl = readSCL(uuid, expectedVersion);
183+
repository.create(TYPE, uuid, scl, expectedVersion);
184+
var version = new Version(1, 1, 0);
185+
scl = readSCL(uuid, version);
186+
repository.create(TYPE, uuid, scl, version);
187+
188+
var foundScl = repository.findByUUID(TYPE, uuid, expectedVersion);
189+
190+
assertNotNull(foundScl);
191+
assertEquals(uuid.toString(), getIdFromHeader(foundScl));
192+
assertEquals(expectedVersion.toString(), getVersionFromHeader(foundScl));
135193
}
136194

137195
@Test
@@ -196,10 +254,10 @@ void createAndDelete_WhenSclAddedAndDelete_ThenScLStoredAndRemoved() {
196254
assertEquals(getIdFromHeader(scl), getIdFromHeader(foundScl));
197255

198256
repository.delete(TYPE, uuid, version);
199-
var exception = assertThrows(CompasSclDataServiceException.class, () -> {
257+
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
200258
repository.findByUUID(TYPE, uuid);
201259
});
202-
assertEquals(BASEX_QUERY_ERROR_CODE, exception.getErrorCode());
260+
assertEquals(NO_DATA_FOUND_ERROR_CODE, exception.getErrorCode());
203261
}
204262

205263
@Test
@@ -222,10 +280,10 @@ void createAndDeleteAll_WhenSclAddedAndDelete_ThenScLStoredAndRemoved() {
222280
assertEquals(getVersionFromHeader(scl), getVersionFromHeader(foundScl));
223281

224282
repository.delete(TYPE, uuid);
225-
var exception = assertThrows(CompasSclDataServiceException.class, () -> {
283+
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
226284
repository.findByUUID(TYPE, uuid);
227285
});
228-
assertEquals(BASEX_QUERY_ERROR_CODE, exception.getErrorCode());
286+
assertEquals(NO_DATA_FOUND_ERROR_CODE, exception.getErrorCode());
229287
}
230288

231289
private String getIdFromHeader(Element scl) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.exception;
5+
6+
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_DATA_FOUND_ERROR_CODE;
7+
8+
public class CompasNoDataFoundException extends CompasSclDataServiceException {
9+
public CompasNoDataFoundException(String message) {
10+
super(NO_DATA_FOUND_ERROR_CODE, message);
11+
}
12+
}

repository/src/main/java/org/lfenergy/compas/scl/data/exception/CompasSclDataServiceErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public class CompasSclDataServiceErrorCode {
1616
public static final String BASEX_CLIENT_CREATION_ERROR_CODE = "SDS-1000";
1717
public static final String BASEX_QUERY_ERROR_CODE = "SDS-1001";
1818
public static final String BASEX_COMMAND_ERROR_CODE = "SDS-1002";
19+
public static final String NO_DATA_FOUND_ERROR_CODE = "SDS-1003";
1920
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.exception;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
class CompasNoDataFoundExceptionTest {
11+
@Test
12+
void constructor_WhenCalledWithOnlyMessage_ThenMessageCanBeRetrieved() {
13+
String expectedMessage = "The message";
14+
var exception = new CompasNoDataFoundException(expectedMessage);
15+
16+
assertEquals(expectedMessage, exception.getMessage());
17+
}
18+
}

0 commit comments

Comments
 (0)