Skip to content

Commit 878f41a

Browse files
committed
chore: segmenting unit/integration tests
1 parent 45a37a3 commit 878f41a

File tree

5 files changed

+156
-8
lines changed

5 files changed

+156
-8
lines changed

.github/workflows/unittest.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Run Unit Tests
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
push:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Unit Tests
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Java
19+
uses: actions/setup-java@v4
20+
with:
21+
distribution: 'temurin'
22+
java-version: '17'
23+
- name: Configure Maven for GitHub Packages
24+
run: |
25+
mkdir -p ~/.m2
26+
cat > ~/.m2/settings.xml <<EOF
27+
<settings>
28+
<servers>
29+
<server>
30+
<id>github</id>
31+
<username>${{ github.actor }}</username>
32+
<password>${{ secrets.GITHUB_TOKEN }}</password>
33+
</server>
34+
</servers>
35+
</settings>
36+
EOF
37+
- name: Run Unit Tests
38+
run: mvn clean test

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
<artifactId>hivemq-mqtt-client</artifactId>
5555
<version>1.3.5</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>com.squareup.okhttp3</groupId>
59+
<artifactId>mockwebserver</artifactId>
60+
<version>4.12.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.mockito</groupId>
65+
<artifactId>mockito-junit-jupiter</artifactId>
66+
<version>5.17.0</version>
67+
<scope>test</scope>
68+
</dependency>
5769
</dependencies>
5870
<repositories>
5971
<repository>
@@ -118,12 +130,12 @@
118130
<configuration>
119131
<java>
120132
<googleJavaFormat>
121-
<version>1.17.0</version>
133+
<version>1.27.0</version>
122134
<style>AOSP</style>
123135
<reflowLongStrings>true</reflowLongStrings>
124136
</googleJavaFormat>
125137
<palantirJavaFormat>
126-
<version>2.35.0</version>
138+
<version>2.61.0</version>
127139
</palantirJavaFormat>
128140
<indent>
129141
<tabs>true</tabs>

src/main/java/com/bigboxer23/govee/GoveeApi.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Map;
1919
import java.util.Optional;
2020
import java.util.stream.Collectors;
21+
import lombok.Setter;
2122
import okhttp3.RequestBody;
2223
import okhttp3.Response;
2324
import org.slf4j.Logger;
@@ -26,7 +27,9 @@
2627
/** */
2728
public class GoveeApi {
2829
private static final Logger logger = LoggerFactory.getLogger(GoveeApi.class);
29-
private static final String API_URL = "https://openapi.api.govee.com/router/api/v1/";
30+
31+
@Setter
32+
private static String apiUrl = "https://openapi.api.govee.com/router/api/v1/";
3033

3134
private static final String MQTT_URL = "mqtt.openapi.govee.com";
3235

@@ -100,14 +103,14 @@ private boolean checkForError(GoveeAPIResponse apiResponse) {
100103
}
101104

102105
public GoveeGetDevicesResponse getDevices() throws IOException {
103-
try (Response response = OkHttpUtil.getSynchronous(API_URL + "user/devices", addAuth())) {
106+
try (Response response = OkHttpUtil.getSynchronous(apiUrl + "user/devices", addAuth())) {
104107
return parseResponse(response, GoveeGetDevicesResponse.class);
105108
}
106109
}
107110

108111
public GoveeDeviceStatusResponse getDeviceStatus(String model, String deviceId) throws IOException {
109112
try (Response response = OkHttpUtil.postSynchronous(
110-
API_URL + "device/state",
113+
apiUrl + "device/state",
111114
RequestBody.create(URLDecoder.decode(
112115
getMoshi()
113116
.adapter(GoveeDeviceStatusRequest.class)
@@ -121,7 +124,7 @@ public GoveeDeviceStatusResponse getDeviceStatus(String model, String deviceId)
121124

122125
public GoveeDeviceCommandResponse sendDeviceCommand(GoveeDeviceStatusRequest command) throws IOException {
123126
try (Response response = OkHttpUtil.postSynchronous(
124-
API_URL + "device/control",
127+
apiUrl + "device/control",
125128
RequestBody.create(URLDecoder.decode(
126129
getMoshi()
127130
.adapter(GoveeDeviceStatusRequest.class)

src/test/java/com/bigboxer23/govee/GoveeApiTest.java renamed to src/test/java/com/bigboxer23/govee/GoveeApiIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import com.bigboxer23.utils.properties.PropertyUtils;
1010
import java.io.IOException;
1111
import java.util.List;
12+
import org.junit.jupiter.api.Tag;
1213
import org.junit.jupiter.api.Test;
1314

14-
/** */
15-
public class GoveeApiTest {
15+
@Tag("integration")
16+
public class GoveeApiIntegrationTest {
1617
private static final String API_KEY = PropertyUtils.getProperty("govee_api_key");
1718

1819
private static final String TEST_MODEL = PropertyUtils.getProperty("govee_device_model");
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.bigboxer23.govee;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import com.bigboxer23.govee.data.*;
7+
import com.bigboxer23.utils.http.OkHttpUtil;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import okhttp3.mockwebserver.MockResponse;
11+
import okhttp3.mockwebserver.MockWebServer;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.mockito.Mock;
15+
import org.mockito.MockitoAnnotations;
16+
17+
public class GoveeApiUnitTest {
18+
private GoveeApi goveeApi;
19+
20+
@Mock
21+
private OkHttpUtil mockHttpUtil;
22+
23+
private static final String TEST_API_KEY = "mock-api-key";
24+
private static final String TEST_MODEL = "H7142";
25+
private static final String TEST_DEVICEID = "18:12:24:A4:F8:40:79:6C";
26+
27+
@BeforeEach
28+
public void setup() {
29+
MockitoAnnotations.openMocks(this);
30+
}
31+
32+
@Test
33+
public void testGetDevices_Success() throws IOException {
34+
try (MockWebServer server = new MockWebServer()) {
35+
GoveeApi.setApiUrl(server.url("/").toString());
36+
goveeApi = GoveeApi.getInstance(TEST_API_KEY);
37+
38+
server.enqueue(new MockResponse()
39+
.setResponseCode(200)
40+
.setBody("{\"code\":200,\"data\":[{\"device\":\"18:12:24:A4:F8:40:79:6C\",\"sku\":\"H7142\"}]}"));
41+
List<GoveeDevice> devices = goveeApi.getDevices().getData();
42+
assertNotNull(devices);
43+
assertFalse(devices.isEmpty());
44+
assertEquals(TEST_MODEL, devices.get(0).getModel());
45+
assertEquals(TEST_DEVICEID, devices.get(0).getDeviceId());
46+
}
47+
}
48+
49+
@Test
50+
public void testGetDeviceStatus_Error() throws IOException {
51+
try (MockWebServer server = new MockWebServer()) {
52+
GoveeApi.setApiUrl(server.url("/").toString());
53+
goveeApi = GoveeApi.getInstance(TEST_API_KEY);
54+
55+
server.enqueue(new MockResponse().setResponseCode(404));
56+
57+
assertThrows(IOException.class, () -> goveeApi.getDeviceStatus(TEST_MODEL, TEST_DEVICEID));
58+
}
59+
}
60+
61+
@Test
62+
public void testSendDeviceCommand_Success() throws IOException {
63+
try (MockWebServer server = new MockWebServer()) {
64+
GoveeApi.setApiUrl(server.url("/").toString());
65+
goveeApi = GoveeApi.getInstance(TEST_API_KEY);
66+
67+
server.enqueue(new MockResponse()
68+
.setResponseCode(200)
69+
.setBody("{\"code\":200,\"capability\":{\"state\":{\"status\":\"success\"}}}"));
70+
71+
GoveeDeviceCommandResponse response =
72+
goveeApi.sendDeviceCommand(IHumidifierCommands.turnOn(TEST_MODEL, TEST_DEVICEID));
73+
assertNotNull(response);
74+
assertEquals("success", response.getCapability().getState().getStatus());
75+
}
76+
}
77+
78+
@Test
79+
public void testSubscribeToGoveeEvents_Mock() throws IOException, InterruptedException {
80+
GoveeEventSubscriber mockSubscriber = mock(GoveeEventSubscriber.class);
81+
82+
// No need for MockWebServer here, since this is event-based
83+
goveeApi = GoveeApi.getInstance(TEST_API_KEY);
84+
goveeApi.subscribeToGoveeEvents(mockSubscriber);
85+
86+
// Simulate receiving a message
87+
GoveeEvent event = new GoveeEvent();
88+
event.setDeviceId(TEST_DEVICEID);
89+
event.setModel(TEST_MODEL);
90+
91+
mockSubscriber.messageReceived(event);
92+
verify(mockSubscriber).messageReceived(any(GoveeEvent.class));
93+
}
94+
}

0 commit comments

Comments
 (0)