Changes according to comments (without GetOtaPackageCallback test)

Signed-off-by: Oleksandra Matviienko <al.zzzeebra@gmail.com>
This commit is contained in:
Oleksandra Matviienko 2024-03-26 12:08:42 +01:00
parent d66dff3c6c
commit 0f4591529a
3 changed files with 53 additions and 11 deletions

View File

@ -461,15 +461,15 @@ public class DeviceApiController implements TbTransportService {
String body = null; String body = null;
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) { if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
body = e.getMessage(); body = e.getMessage();
log.debug("Failed to process request: {}", body); log.debug("Failed to process request in DeviceAuthCallback: {}", body);
} else { } else {
log.warn("Failed to process request", e); log.warn("Failed to process request in DeviceAuthCallback", e);
} }
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR)); responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
} }
} }
private static class DeviceProvisionCallback implements TransportServiceCallback<ProvisionDeviceResponseMsg> { static class DeviceProvisionCallback implements TransportServiceCallback<ProvisionDeviceResponseMsg> {
private final DeferredResult<ResponseEntity> responseWriter; private final DeferredResult<ResponseEntity> responseWriter;
DeviceProvisionCallback(DeferredResult<ResponseEntity> responseWriter) { DeviceProvisionCallback(DeferredResult<ResponseEntity> responseWriter) {
@ -486,9 +486,9 @@ public class DeviceApiController implements TbTransportService {
String body = null; String body = null;
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) { if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
body = e.getMessage(); body = e.getMessage();
log.debug("Failed to process request: {}", body); log.debug("Failed to process request in DeviceProvisionCallback: {}", body);
} else { } else {
log.warn("Failed to process request", e); log.warn("Failed to process request in DeviceProvisionCallback", e);
} }
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR)); responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
} }
@ -533,9 +533,9 @@ public class DeviceApiController implements TbTransportService {
String body = null; String body = null;
if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) { if (e instanceof HttpMessageNotReadableException || e instanceof JsonParseException) {
body = e.getMessage(); body = e.getMessage();
log.debug("Failed to process request: {}", body); log.debug("Failed to process request in GetOtaPackageCallback: {}", body);
} else { } else {
log.warn("Failed to process request", e); log.warn("Failed to process request in GetOtaPackageCallback", e);
} }
responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR)); responseWriter.setResult(new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR));
} }

View File

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2024 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.transport.http; package org.thingsboard.server.transport.http;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
@ -18,13 +33,13 @@ import static org.junit.jupiter.api.Assertions.*;
class DeviceApiControllerTest { class DeviceApiControllerTest {
@Test @Test
void callbackOnErrorTest() { void DeviceAuthCallbackTest() {
TransportContext transportContext = Mockito.mock(TransportContext.class); TransportContext transportContext = Mockito.mock(TransportContext.class);
DeferredResult<ResponseEntity> responseWriter = Mockito.mock(DeferredResult.class); DeferredResult<ResponseEntity> responseWriter = Mockito.mock(DeferredResult.class);
Consumer<TransportProtos.SessionInfoProto> onSuccess = x -> {}; Consumer<TransportProtos.SessionInfoProto> onSuccess = x -> {};
var callback = new DeviceApiController.DeviceAuthCallback(transportContext, responseWriter, onSuccess); var callback = new DeviceApiController.DeviceAuthCallback(transportContext, responseWriter, onSuccess);
callback.onError(new HttpMessageNotReadableException("JSON incorect syntax")); callback.onError(new HttpMessageNotReadableException("JSON incorrect syntax"));
callback.onError(new JsonParseException("Json ; expected")); callback.onError(new JsonParseException("Json ; expected"));
@ -32,4 +47,29 @@ class DeviceApiControllerTest {
callback.onError(new RuntimeException("oops it is run time error")); callback.onError(new RuntimeException("oops it is run time error"));
} }
}
@Test
void DeviceProvisionCallbackTest() {
DeferredResult<ResponseEntity> responseWriter = Mockito.mock(DeferredResult.class);
var callback = new DeviceApiController.DeviceProvisionCallback(responseWriter);
callback.onError(new HttpMessageNotReadableException("JSON incorrect syntax"));
callback.onError(new JsonParseException("Json ; expected"));
callback.onError(new IOException("not found"));
callback.onError(new RuntimeException("oops it is run time error"));
}
//@Test
// void GetOtaPackageCallback() {
// DeferredResult<ResponseEntity> responseWriter = Mockito.mock(DeferredResult.class);
// String title = "Title";
// String version = "version";
// int chuckSize = 11;
// int chuck = 3;
//
// var callback = new DeviceApiController.GetOtaPackageCallback(responseWriter, title, version, chuckSize, chuck);
// }
}

View File

@ -7,7 +7,9 @@
</encoder> </encoder>
</appender> </appender>
<root level="TRACE"> logger name="org.thingsboard.server.transport.http.DeviceApiController" level="DEBUG" />
<root level="INFO">
<appender-ref ref="console"/> <appender-ref ref="console"/>
</root> </root>