]> git.basschouten.com Git - openhab-addons.git/blob
1ecfcb11c711766f70a8ec01e1d4a861eb76996c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.util;
14
15 import static org.junit.jupiter.api.Assertions.fail;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Optional;
19
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;
27
28 /**
29  * Utility class for creating common mocks.
30  *
31  * @author Björn Lange - Initial contribution
32  */
33 @NonNullByDefault
34 public final class MockUtil {
35     private MockUtil() {
36     }
37
38     public static Device mockDevice(String fabNumber) {
39         DeviceIdentLabel deviceIdentLabel = mock(DeviceIdentLabel.class);
40         when(deviceIdentLabel.getFabNumber()).thenReturn(Optional.of(fabNumber));
41
42         Ident ident = mock(Ident.class);
43         when(ident.getDeviceIdentLabel()).thenReturn(Optional.of(deviceIdentLabel));
44
45         Device device = mock(Device.class);
46         when(device.getIdent()).thenReturn(Optional.of(ident));
47
48         return device;
49     }
50
51     public static <T> T requireNonNull(@Nullable T obj) {
52         if (obj == null) {
53             throw new IllegalArgumentException("Object must not be null");
54         }
55         return obj;
56     }
57
58     /**
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.
61      */
62     public static HttpClient mockHttpClient() {
63         return new HttpClient() {
64             @Override
65             protected void doStart() throws Exception {
66             }
67         };
68     }
69
70     /**
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.
73      *
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.
76      */
77     public static HttpClient mockHttpClient(String newRequestUri, Request newRequestReturnValue) {
78         return new HttpClient() {
79             @Override
80             protected void doStart() throws Exception {
81             }
82
83             @Override
84             public Request newRequest(@Nullable String uri) {
85                 if (newRequestUri.equals(uri)) {
86                     return newRequestReturnValue;
87                 } else {
88                     fail();
89                     throw new IllegalStateException();
90                 }
91             }
92         };
93     }
94 }