2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.util;
15 import static org.junit.jupiter.api.Assertions.fail;
16 import static org.mockito.Mockito.*;
18 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.client.api.Request;
24 import org.openhab.binding.mielecloud.internal.webservice.api.json.Device;
25 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceIdentLabel;
26 import org.openhab.binding.mielecloud.internal.webservice.api.json.Ident;
29 * Utility class for creating common mocks.
31 * @author Björn Lange - Initial contribution
34 public final class MockUtil {
38 public static Device mockDevice(String fabNumber) {
39 DeviceIdentLabel deviceIdentLabel = mock(DeviceIdentLabel.class);
40 when(deviceIdentLabel.getFabNumber()).thenReturn(Optional.of(fabNumber));
42 Ident ident = mock(Ident.class);
43 when(ident.getDeviceIdentLabel()).thenReturn(Optional.of(deviceIdentLabel));
45 Device device = mock(Device.class);
46 when(device.getIdent()).thenReturn(Optional.of(ident));
51 public static <T> T requireNonNull(@Nullable T obj) {
53 throw new IllegalArgumentException("Object must not be null");
59 * Creates a mock for {@link HttpClient} circumventing the problem that {@link HttpClient#start()} is {@code final}
60 * and {@link HttpClient#doStart()} {@code protected} and unaccessible when mocking with Mockito.
62 public static HttpClient mockHttpClient() {
63 return new HttpClient() {
65 protected void doStart() throws Exception {
71 * Creates a mock for {@link HttpClient} circumventing the problem that {@link HttpClient#start()} is {@code final}
72 * and {@link HttpClient#doStart()} {@code protected} and unaccessible when mocking with Mockito.
74 * @param newRequestUri {@code uri} parameter of {@link HttpClient#newRequest(String)} to mock.
75 * @param newRequestReturnValue Return value of {@link HttpClient#newRequest(String)} to mock.
77 public static HttpClient mockHttpClient(String newRequestUri, Request newRequestReturnValue) {
78 return new HttpClient() {
80 protected void doStart() throws Exception {
84 public Request newRequest(@Nullable String uri) {
85 if (newRequestUri.equals(uri)) {
86 return newRequestReturnValue;
89 throw new IllegalStateException();