Skip to content

Commit

Permalink
openhab#62 Correctly check if long poll response is valid
Browse files Browse the repository at this point in the history
GSON will not return null if there is no "result" field, but will just set the "result" member to null.

Signed-off-by: Christian Oeing <christian.oeing@slashgames.org>
  • Loading branch information
coeing committed Jan 10, 2021
1 parent dc03646 commit a29068d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,17 @@ private void onLongPollResponse(BoschHttpClient httpClient, String subscriptionI
String nextSubscriptionId = subscriptionId;

LongPollResult longPollResult = gson.fromJson(content, LongPollResult.class);
if (longPollResult != null) {
if (longPollResult != null && longPollResult.result != null) {
this.handleResult.accept(longPollResult);
} else {
logger.warn("Could not parse long poll response: {}", content);
logger.warn("Long poll response contained no results: {}", content);

// Check if we got a proper result from the SHC
LongPollError longPollError = gson.fromJson(content, LongPollError.class);

if (longPollError != null) {
logger.warn("Got error from SHC: {} (code: {})", longPollError.error.message, longPollError.error.code);
if (longPollError != null && longPollError.error != null) {
logger.warn("Got long poll error: {} (code: {})", longPollError.error.message,
longPollError.error.code);

if (longPollError.error.code == LongPollError.SUBSCRIPTION_INVALID) {
logger.warn("Subscription {} became invalid, subscribing again", subscriptionId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.boschshc.internal.devices.bridge.dto;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.google.gson.Gson;

/**
* Unit tests for LongPollResult
*
* @author Christian Oeing - Initial contribution
*/
public class LongPollResultTest {
private final Gson gson = new Gson();

@Test
public void NoResultsForErrorResult() {
LongPollResult longPollResult = gson.fromJson(
"{\"jsonrpc\":\"2.0\", \"error\": { \"code\":-32001, \"message\":\"No subscription with id: e8fei62b0-0\" } }",
LongPollResult.class);
assertEquals(null, longPollResult.result);
}
}

0 comments on commit a29068d

Please sign in to comment.