Skip to content

Commit eacf5ae

Browse files
committed
remove exception
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
1 parent 0ca9e39 commit eacf5ae

3 files changed

Lines changed: 29 additions & 60 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.openhab.core.persistence.ModifiablePersistenceService;
6565
import org.openhab.core.persistence.PersistenceItemConfiguration;
6666
import org.openhab.core.persistence.PersistenceItemInfo;
67-
import org.openhab.core.persistence.PersistenceItemNotFoundException;
6867
import org.openhab.core.persistence.PersistenceManager;
6968
import org.openhab.core.persistence.PersistenceService;
7069
import org.openhab.core.persistence.PersistenceServiceProblem;
@@ -646,31 +645,32 @@ private Response getServiceItemListDTO(@Nullable String serviceId, @Nullable Str
646645
}
647646

648647
QueryablePersistenceService qService = (QueryablePersistenceService) service;
649-
Set<PersistenceItemInfoDTO> itemInfo = Set.of();
650648
try {
651-
itemInfo = createDTO(qService, itemName);
652-
} catch (PersistenceItemNotFoundException e) {
653-
return JSONResponse.createErrorResponse(Status.NOT_FOUND, e.getMessage());
649+
Set<PersistenceItemInfoDTO> itemInfo = createDTO(qService, itemName);
650+
if (itemInfo == null) {
651+
return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Item '" + itemName
652+
+ "' could not be found in persistence service '" + effectiveServiceId + "'");
653+
}
654+
655+
return JSONResponse.createResponse(Status.OK, itemInfo, "");
654656
} catch (UnsupportedOperationException e) {
655657
return JSONResponse.createErrorResponse(Status.METHOD_NOT_ALLOWED,
656658
"Not supported for persistence service: " + effectiveServiceId);
657659
}
658-
659-
return JSONResponse.createResponse(Status.OK, itemInfo, "");
660660
}
661661

662-
protected Set<PersistenceItemInfoDTO> createDTO(QueryablePersistenceService qService, @Nullable String itemName)
663-
throws UnsupportedOperationException, PersistenceItemNotFoundException {
662+
protected @Nullable Set<PersistenceItemInfoDTO> createDTO(QueryablePersistenceService qService,
663+
@Nullable String itemName) throws UnsupportedOperationException {
664664
String serviceId = qService.getId();
665665
PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(serviceId);
666666
Map<String, String> itemToAlias = config != null ? config.getAliases() : Map.of();
667667

668-
Set<PersistenceItemInfo> itemInfo = Set.of();
668+
Set<PersistenceItemInfo> itemInfo;
669669
if (itemName != null) {
670670
String alias = itemToAlias.get(itemName);
671671
PersistenceItemInfo singleItemInfo = qService.getItemInfo(itemName, alias);
672672
if (singleItemInfo == null) {
673-
throw new PersistenceItemNotFoundException(serviceId, itemName, alias);
673+
return null;
674674
}
675675
itemInfo = Set.of(singleItemInfo);
676676
} else {

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.openhab.core.persistence.HistoricItem;
5353
import org.openhab.core.persistence.ModifiablePersistenceService;
5454
import org.openhab.core.persistence.PersistenceItemInfo;
55-
import org.openhab.core.persistence.PersistenceItemNotFoundException;
5655
import org.openhab.core.persistence.PersistenceServiceRegistry;
5756
import org.openhab.core.persistence.dto.ItemHistoryDTO;
5857
import org.openhab.core.persistence.dto.ItemHistoryDTO.HistoryDataBean;
@@ -137,6 +136,7 @@ public String getName() {
137136
public void testGetPersistenceItemData() {
138137
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, false, false);
139138

139+
assertNotNull(dto);
140140
assertThat(Integer.parseInt(dto.datapoints), is(5));
141141
assertThat(dto.data, hasSize(5));
142142

@@ -168,6 +168,7 @@ public void testGetPersistenceItemData() {
168168
public void testGetPersistenceItemDataWithBoundery() {
169169
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, true, false);
170170

171+
assertNotNull(dto);
171172
assertThat(Integer.parseInt(dto.datapoints), is(7));
172173
assertThat(dto.data, hasSize(7));
173174
}
@@ -179,6 +180,7 @@ public void testGetPersistenceItemDataWithItemState() throws ItemNotFoundExcepti
179180

180181
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, false, true);
181182

183+
assertNotNull(dto);
182184
assertThat(Integer.parseInt(dto.datapoints), is(6));
183185
assertThat(dto.data, hasSize(6));
184186
assertThat(dto.data.get(dto.data.size() - 1).state, is("0"));
@@ -191,6 +193,7 @@ public void testGetPersistenceItemDataWithItemStateUndefined() throws ItemNotFou
191193

192194
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, false, true);
193195

196+
assertNotNull(dto);
194197
assertThat(Integer.parseInt(dto.datapoints), is(5));
195198
assertThat(dto.data, hasSize(5));
196199
}
@@ -202,6 +205,7 @@ public void testGetPersistenceItemDataWithItemStateNull() throws ItemNotFoundExc
202205

203206
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, false, true);
204207

208+
assertNotNull(dto);
205209
assertThat(Integer.parseInt(dto.datapoints), is(5));
206210
assertThat(dto.data, hasSize(5));
207211
}
@@ -214,6 +218,7 @@ public void testGetPersistenceItemDataWithBoundaryAndItemStateButNoItemStateRequ
214218

215219
ItemHistoryDTO dto = pResource.createDTO(pServiceMock, "testItem", null, null, 1, 10, true, true);
216220

221+
assertNotNull(dto);
217222
assertThat(Integer.parseInt(dto.datapoints), is(7));
218223
assertThat(dto.data, hasSize(7));
219224
assertThat(dto.data.get(dto.data.size() - 1).state, not("0"));
@@ -232,14 +237,14 @@ public void testPutPersistenceItemData() throws ItemNotFoundException {
232237
}
233238

234239
@Test
235-
public void testGetPersistenceItemInfoNotImplemented() throws ItemNotFoundException, UnsupportedOperationException {
240+
public void testGetPersistenceItemInfoNotImplemented() throws UnsupportedOperationException {
236241
// Test method not supported
237242
when(pServiceMock.getItemInfo()).thenThrow(UnsupportedOperationException.class);
238243
assertThrows(UnsupportedOperationException.class, () -> pResource.createDTO(pServiceMock, null));
239244
}
240245

241246
@Test
242-
public void testGetPersistenceItemInfo() throws PersistenceItemNotFoundException, UnsupportedOperationException {
247+
public void testGetPersistenceItemInfo() throws UnsupportedOperationException {
243248
when(pServiceMock.getItemInfo()).thenReturn(Set.of(new PersistenceItemInfo() {
244249

245250
@Override
@@ -265,6 +270,7 @@ public String getName() {
265270

266271
// Testing with a specific implementation
267272
Set<PersistenceItemInfoDTO> dto = pResource.createDTO(pServiceMock, null);
273+
assertNotNull(dto);
268274
PersistenceItemInfoDTO itemInfo = dto.iterator().next();
269275
assertThat(itemInfo.name(), is(ITEM));
270276
assertThat(itemInfo.earliest(),
@@ -275,8 +281,7 @@ public String getName() {
275281
}
276282

277283
@Test
278-
public void testGetPersistenceItemInfoWithItemDefault()
279-
throws PersistenceItemNotFoundException, UnsupportedOperationException {
284+
public void testGetPersistenceItemInfoWithItemDefault() throws UnsupportedOperationException {
280285
when(pServiceMock.getItemInfo(any(), any())).thenReturn(new PersistenceItemInfo() {
281286

282287
@Override
@@ -302,6 +307,7 @@ public String getName() {
302307

303308
// This is testing the default behavior when no specific implementation exists in the service
304309
Set<PersistenceItemInfoDTO> dto = pResource.createDTO(pServiceMock, ITEM);
310+
assertNotNull(dto);
305311
assertThat(dto.size(), is(1));
306312
PersistenceItemInfoDTO itemInfo = dto.iterator().next();
307313
assertThat(itemInfo.name(), is(ITEM));
@@ -312,8 +318,7 @@ public String getName() {
312318
}
313319

314320
@Test
315-
public void testGetPersistenceItemInfoWithItem()
316-
throws PersistenceItemNotFoundException, UnsupportedOperationException {
321+
public void testGetPersistenceItemInfoWithItem() throws UnsupportedOperationException {
317322
when(pServiceMock.getItemInfo(any(), any())).thenAnswer(invocation -> {
318323
String firstArg = invocation.getArgument(0);
319324
String secondArg = invocation.getArgument(1);
@@ -345,11 +350,13 @@ public String getName() {
345350
};
346351
});
347352

348-
// Testing when ITEM does not exist and getItemInfo returns null
349-
assertThrows(PersistenceItemNotFoundException.class, () -> pResource.createDTO(pServiceMock, "NotFoundTest"));
353+
// Testing when ITEM does not exist
354+
Set<PersistenceItemInfoDTO> dto = pResource.createDTO(pServiceMock, "NotFoundTest");
355+
assertNull(dto);
350356

351357
// Test when specific implementation exists and no alias is used
352-
Set<PersistenceItemInfoDTO> dto = pResource.createDTO(pServiceMock, ITEM);
358+
dto = pResource.createDTO(pServiceMock, ITEM);
359+
assertNotNull(dto);
353360
assertThat(dto.size(), is(1));
354361
PersistenceItemInfoDTO itemInfo = dto.iterator().next();
355362
assertThat(itemInfo.name(), is(ITEM));
@@ -363,6 +370,7 @@ public String getName() {
363370
when(persistenceServiceConfigurationRegistryMock.get(any())).thenReturn(persistenceServiceConfigurationMock);
364371
when(persistenceServiceConfigurationMock.getAliases()).thenReturn(Map.of(ITEM, "TestAlias"));
365372
dto = pResource.createDTO(pServiceMock, ITEM);
373+
assertNotNull(dto);
366374
assertThat(dto.size(), is(1));
367375
itemInfo = dto.iterator().next();
368376
assertThat(itemInfo.name(), is("TestAlias"));

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/PersistenceItemNotFoundException.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)