Skip to content

Commit

Permalink
[rest] Fix links to group members in REST response (openhab#2399)
Browse files Browse the repository at this point in the history
* Fix links for to group members in REST response

Signed-off-by: Kai Kreuzer <kai@openhab.org>
GitOrigin-RevId: 3cac330
  • Loading branch information
kaikreuzer authored and splatch committed Jul 11, 2023
1 parent 3166328 commit 953347d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public ItemResource(//
}

private UriBuilder uriBuilder(final UriInfo uriInfo, final HttpHeaders httpHeaders) {
final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder().path(PATH_ITEMS).path("{itemName}");
respectForwarded(uriBuilder, httpHeaders);
return uriBuilder;
}
Expand All @@ -207,7 +207,6 @@ public Response getItems(final @Context UriInfo uriInfo, final @Context HttpHead
final Set<String> namespaces = splitAndFilterNamespaces(namespaceSelector, locale);

final UriBuilder uriBuilder = uriBuilder(uriInfo, httpHeaders);
uriBuilder.path("{itemName}");

Stream<EnrichedItemDTO> itemStream = getItems(type, tags).stream() //
.map(item -> EnrichedItemDTOMapper.map(item, recursive, null, uriBuilder, locale)) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.transform.TransformationException;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -105,23 +106,25 @@ public void beforeEach() {

UriBuilder uriBuilder = mock(UriBuilder.class);
when(uriBuilder.build(any())).thenReturn(URI.create(""));
when(uriBuilder.path(anyString())).thenReturn(uriBuilder);
uriInfo = mock(UriInfo.class);
when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
when(uriInfo.getPath()).thenReturn("");
httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getHeaderString(anyString())).thenReturn(null);
}

@Test
public void shouldReturnUnicodeItems() throws IOException {
public void shouldReturnUnicodeItems() throws IOException, TransformationException {
item4.setLabel(ITEM_LABEL4);

Response response = itemResource.getItems(uriInfo, httpHeaders, null, null, null, null, false, null);
assertThat(readItemLabelsFromResponse(response), hasItems(ITEM_LABEL4));
}

@Test
public void shouldReturnUnicodeItem() throws IOException {
public void shouldReturnUnicodeItem() throws IOException, TransformationException {
item4.setLabel(ITEM_LABEL4);

Response response = itemResource.getItemData(uriInfo, httpHeaders, null, null, ITEM_NAME4);
Expand Down Expand Up @@ -207,7 +210,7 @@ private List<String> readItemNamesFromResponse(Response response) throws IOExcep
return JsonPath.read(jsonResponse, "$..name");
}

private List<String> readItemLabelsFromResponse(Response response) throws IOException {
private List<String> readItemLabelsFromResponse(Response response) throws IOException, TransformationException {
String jsonResponse = new String(((InputStream) response.getEntity()).readAllBytes(), StandardCharsets.UTF_8);
return JsonPath.read(jsonResponse, "$..label");
}
Expand Down

0 comments on commit 953347d

Please sign in to comment.