Skip to content

Commit

Permalink
[iotawatt] check httpsStatus
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Rosenberg <prosenb.dev@gmail.com>
  • Loading branch information
PRosenb committed May 7, 2024
1 parent c81cf01 commit b7a6564
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.openhab.binding.iotawatt.internal.exception.ThingStatusOfflineException;
import org.openhab.binding.iotawatt.internal.model.StatusResponse;
import org.openhab.core.thing.ThingStatusDetail;
Expand Down Expand Up @@ -80,6 +81,10 @@ public Optional<StatusResponse> fetchStatus() throws ThingStatusOfflineException
final Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(requestTimeout,
TimeUnit.SECONDS);
final ContentResponse response = request.send();
if (response.getStatus() != HttpStatus.OK_200) {
throw new ThingStatusOfflineException(ThingStatusDetail.COMMUNICATION_ERROR,
"HttpStatus " + response.getStatus());
}
final String content = response.getContentAsString();
@Nullable
final StatusResponse statusResponse = gson.fromJson(content, StatusResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -68,6 +69,7 @@ void fetchStatus_whenValidJson_returnObject() throws ThingStatusOfflineException
when(request.timeout(anyLong(), any(TimeUnit.class))).thenReturn(request);
ContentResponse contentResponse = mock(ContentResponse.class);
when(request.send()).thenReturn(contentResponse);
when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
when(contentResponse.getContentAsString()).thenReturn(readFile(DEVICE_STATUS_RESPONSE_FILE));

// when
Expand Down Expand Up @@ -109,7 +111,7 @@ void fetchStatus_whenInputsAndOutputsEmpty_returnEmpty()
ContentResponse contentResponse = mock(ContentResponse.class);
when(request.send()).thenReturn(contentResponse);
when(contentResponse.getContentAsString()).thenReturn("{}");
final StatusResponse statusResponse = new StatusResponse(null, null);
when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);

// when
Optional<StatusResponse> resultOptional = client.fetchStatus();
Expand All @@ -121,6 +123,23 @@ void fetchStatus_whenInputsAndOutputsEmpty_returnEmpty()
assertNull(result.outputs());
}

@Test
void fetchStatus_whenNot200Response_throwsException()
throws ThingStatusOfflineException, ExecutionException, InterruptedException, TimeoutException {
// given
final IoTaWattClient client = new IoTaWattClient("hostname", 10, httpClient, gson);
Request request = mock(Request.class);
when(httpClient.newRequest(any(URI.class))).thenReturn(request);
when(request.method(any(HttpMethod.class))).thenReturn(request);
when(request.timeout(anyLong(), any(TimeUnit.class))).thenReturn(request);
ContentResponse contentResponse = mock(ContentResponse.class);
when(request.send()).thenReturn(contentResponse);
when(contentResponse.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);

// when/then
assertThrows(ThingStatusOfflineException.class, client::fetchStatus);
}

private String readFile(String filename) throws IOException {
final Path workingDir = Path.of("", "src/test/resources");
final Path file = workingDir.resolve(filename);
Expand Down

0 comments on commit b7a6564

Please sign in to comment.