Skip to content

Commit c661668

Browse files
committed
specific exception
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
1 parent 49e9857 commit c661668

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.openhab.core.io.rest.RESTConstants;
5353
import org.openhab.core.io.rest.RESTResource;
5454
import org.openhab.core.io.rest.core.config.ConfigurationService;
55+
import org.openhab.core.io.rest.core.persistence.PersistenceItemNotFoundException;
5556
import org.openhab.core.items.Item;
5657
import org.openhab.core.items.ItemNotFoundException;
5758
import org.openhab.core.items.ItemRegistry;
@@ -459,7 +460,8 @@ private Response getItemHistoryDTO(@Nullable String serviceId, String itemName,
459460
protected @Nullable ItemHistoryDTO createDTO(QueryablePersistenceService qService, String itemName,
460461
@Nullable String timeBegin, @Nullable String timeEnd, int pageNumber, int pageLength, boolean boundary,
461462
boolean itemState) {
462-
PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(qService.getId());
463+
String serviceId = qService.getId();
464+
PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(serviceId);
463465
String alias = config != null ? config.getAliases().get(itemName) : null;
464466

465467
ZonedDateTime dateTimeBegin = ZonedDateTime.now();
@@ -643,7 +645,7 @@ private Response getServiceItemListDTO(@Nullable String serviceId, @Nullable Str
643645
Set<PersistenceItemInfoDTO> itemInfo = Set.of();
644646
try {
645647
itemInfo = createDTO(qService, itemName);
646-
} catch (ItemNotFoundException e) {
648+
} catch (PersistenceItemNotFoundException e) {
647649
return JSONResponse.createErrorResponse(Status.NOT_FOUND, e.getMessage());
648650
} catch (UnsupportedOperationException e) {
649651
return JSONResponse.createErrorResponse(Status.METHOD_NOT_ALLOWED,
@@ -654,7 +656,7 @@ private Response getServiceItemListDTO(@Nullable String serviceId, @Nullable Str
654656
}
655657

656658
protected Set<PersistenceItemInfoDTO> createDTO(QueryablePersistenceService qService, @Nullable String itemName)
657-
throws ItemNotFoundException, UnsupportedOperationException {
659+
throws UnsupportedOperationException, PersistenceItemNotFoundException {
658660
String serviceId = qService.getId();
659661
PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(serviceId);
660662
Map<String, String> itemToAlias = config != null ? config.getAliases() : Map.of();
@@ -664,8 +666,7 @@ protected Set<PersistenceItemInfoDTO> createDTO(QueryablePersistenceService qSer
664666
String alias = itemToAlias.get(itemName);
665667
PersistenceItemInfo singleItemInfo = qService.getItemInfo(itemName, alias);
666668
if (singleItemInfo == null) {
667-
throw new ItemNotFoundException(
668-
"Item '" + itemName + "' not found in persistence service: " + serviceId);
669+
throw new PersistenceItemNotFoundException(itemName, serviceId);
669670
}
670671
itemInfo = Set.of(singleItemInfo);
671672
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2010-2026 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.persistence;
14+
15+
import java.io.Serial;
16+
17+
import org.eclipse.jdt.annotation.NonNullByDefault;
18+
19+
/**
20+
* This exception is thrown by the {@link PersistenceResource} if an item could not be found in a persistence service.
21+
*
22+
* @author Mark Herwege - Initial contribution
23+
*/
24+
@NonNullByDefault
25+
public class PersistenceItemNotFoundException extends Exception {
26+
27+
public PersistenceItemNotFoundException(String name, String serviceId) {
28+
super("Item '" + name + "' could not be found in persistence service '" + serviceId + "'");
29+
}
30+
31+
@Serial
32+
private static final long serialVersionUID = 360588429692588595L;
33+
}

bundles/org.openhab.core.io.rest.core/src/test/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResourceTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.openhab.core.io.rest.LocaleService;
4545
import org.openhab.core.io.rest.core.config.ConfigurationService;
4646
import org.openhab.core.io.rest.core.internal.persistence.PersistenceResource.PersistenceItemInfoDTO;
47+
import org.openhab.core.io.rest.core.persistence.PersistenceItemNotFoundException;
4748
import org.openhab.core.items.Item;
4849
import org.openhab.core.items.ItemNotFoundException;
4950
import org.openhab.core.items.ItemRegistry;
@@ -126,9 +127,9 @@ public String getName() {
126127
});
127128
}
128129

129-
when(pServiceMock.query(any(), any())).thenReturn(items);
130-
131130
when(persistenceServiceRegistryMock.get(PERSISTENCE_SERVICE_ID)).thenReturn(pServiceMock);
131+
when(pServiceMock.getId()).thenReturn(PERSISTENCE_SERVICE_ID);
132+
when(pServiceMock.query(any(), any())).thenReturn(items);
132133
when(timeZoneProviderMock.getTimeZone()).thenReturn(ZoneId.systemDefault());
133134
}
134135

@@ -238,7 +239,7 @@ public void testGetPersistenceItemInfoNotImplemented() throws ItemNotFoundExcept
238239
}
239240

240241
@Test
241-
public void testGetPersistenceItemInfo() throws ItemNotFoundException, UnsupportedOperationException {
242+
public void testGetPersistenceItemInfo() throws PersistenceItemNotFoundException, UnsupportedOperationException {
242243
when(pServiceMock.getItemInfo()).thenReturn(Set.of(new PersistenceItemInfo() {
243244

244245
@Override
@@ -275,7 +276,7 @@ public String getName() {
275276

276277
@Test
277278
public void testGetPersistenceItemInfoWithItemDefault()
278-
throws ItemNotFoundException, UnsupportedOperationException, IOException {
279+
throws PersistenceItemNotFoundException, UnsupportedOperationException, IOException {
279280
when(pServiceMock.getItemInfo(any(), any())).thenReturn(new PersistenceItemInfo() {
280281

281282
@Override
@@ -312,7 +313,7 @@ public String getName() {
312313

313314
@Test
314315
public void testGetPersistenceItemInfoWithItem()
315-
throws ItemNotFoundException, UnsupportedOperationException, IOException {
316+
throws PersistenceItemNotFoundException, UnsupportedOperationException, IOException {
316317
when(pServiceMock.getItemInfo(any(), any())).thenAnswer(invocation -> {
317318
String firstArg = invocation.getArgument(0);
318319
String secondArg = invocation.getArgument(1);
@@ -345,7 +346,7 @@ public String getName() {
345346
});
346347

347348
// Testing when item does not exist and getItemInfo returns null
348-
assertThrows(ItemNotFoundException.class, () -> pResource.createDTO(pServiceMock, "NotFoundTest"));
349+
assertThrows(PersistenceItemNotFoundException.class, () -> pResource.createDTO(pServiceMock, "NotFoundTest"));
349350

350351
// Test when specific implementation exists and no alias is used
351352
Set<PersistenceItemInfoDTO> dto = pResource.createDTO(pServiceMock, item);

0 commit comments

Comments
 (0)