Skip to content

Commit

Permalink
feat: unwrap logging transport
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Sep 9, 2024
1 parent 268b9c8 commit 6fcf6f3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions coap-core/src/main/java/com/mbed/coap/server/CoapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.packet.SeparateResponse;
import com.mbed.coap.transport.CoapTransport;
import com.mbed.coap.transport.LoggingCoapTransport;
import com.mbed.coap.utils.Service;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -33,7 +34,7 @@
public class CoapServer {
private static final Logger LOGGER = LoggerFactory.getLogger(CoapServer.class);
private final AtomicBoolean isRunning = new AtomicBoolean(false);
final CoapTransport transport;
private final CoapTransport transport;
private final Consumer<CoapPacket> dispatcher;
private final Service<CoapRequest, CoapResponse> outboundService;
private final Service<SeparateResponse, Boolean> outboundResponseService;
Expand Down Expand Up @@ -131,6 +132,6 @@ public Service<SeparateResponse, Boolean> outboundResponseService() {
}

CoapTransport getTransport() {
return transport;
return LoggingCoapTransport.unwrap(transport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public LoggingCoapTransport(CoapTransport transport) {
this.transport = transport;
}

public static CoapTransport unwrap(CoapTransport transport) {
if (transport instanceof LoggingCoapTransport) {
return ((LoggingCoapTransport) transport).getTransport();
}
return transport;
}

public CoapTransport getTransport() {
return transport;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* SPDX-License-Identifier: Apache-2.0
* 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 com.mbed.coap.transport;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class LoggingCoapTransportTest {

@Test
void shouldUnwrapTransport() {
CoapTransport transport = Mockito.mock(CoapTransport.class);
LoggingCoapTransport loggingCoapTransport = new LoggingCoapTransport(transport);

assertEquals(transport, LoggingCoapTransport.unwrap(loggingCoapTransport));
assertEquals(transport, LoggingCoapTransport.unwrap(transport));
}

}

0 comments on commit 6fcf6f3

Please sign in to comment.