diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleActionHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleActionHandler.java index 57ab49f8..56317e1a 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleActionHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleActionHandler.java @@ -12,9 +12,8 @@ */ package org.openhab.automation.jrule.internal.handler; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.io.InputStream; import java.time.Duration; import java.util.Map; import java.util.Properties; @@ -86,12 +85,11 @@ public String sendHttpGetRequest(String url, @Nullable Map heade * @param timeout Time after the request will be canceled * @return Result as String */ - public String sendHttpPutRequest(String url, String contentType, @Nullable String content, + public String sendHttpPutRequest(String url, String contentType, @Nullable InputStream content, Map headers, @Nullable Duration timeout) { try { - return HttpUtil.executeUrl(HttpMethod.PUT, url, mapToProperties(headers), - content != null ? new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)) : null, - contentType, getTimeoutAsInt(timeout)); + return HttpUtil.executeUrl(HttpMethod.PUT, url, mapToProperties(headers), content, contentType, + getTimeoutAsInt(timeout)); } catch (IOException e) { throw new JRuleRuntimeException("Error executing Http action", e); } @@ -109,12 +107,11 @@ public String sendHttpPutRequest(String url, String contentType, @Nullable Strin * @param timeout Time after the request will be canceled * @return Result as String */ - public String sendHttpPostRequest(String url, String contentType, @Nullable String content, + public String sendHttpPostRequest(String url, String contentType, @Nullable InputStream content, Map headers, @Nullable Duration timeout) { try { - return HttpUtil.executeUrl(HttpMethod.POST, url, mapToProperties(headers), - content != null ? new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)) : null, - contentType, getTimeoutAsInt(timeout)); + return HttpUtil.executeUrl(HttpMethod.POST, url, mapToProperties(headers), content, contentType, + getTimeoutAsInt(timeout)); } catch (IOException e) { throw new JRuleRuntimeException("Error executing Http action", e); } diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRule.java b/src/main/java/org/openhab/automation/jrule/rules/JRule.java index 6283dfbb..c0fbf031 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRule.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRule.java @@ -12,6 +12,8 @@ */ package org.openhab.automation.jrule.rules; +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.time.Instant; import java.time.ZoneId; @@ -136,7 +138,26 @@ protected String sendHttpPutRequest(String url, @Nullable Duration timeout) { */ protected String sendHttpPutRequest(String url, String contentType, String content, Map headers, @Nullable Duration timeout) { - return JRuleActionHandler.get().sendHttpPutRequest(url, contentType, content, headers, timeout); + return JRuleActionHandler.get().sendHttpPutRequest(url, contentType, + content != null ? new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)) : null, headers, + timeout); + } + + /** + * Sends a PUT-HTTP request with the given content, request headers, and timeout in ms, and returns the result as a + * String + * + * @param url Target URL + * @param contentType @see javax.ws.rs.core.MediaType + * @param content Request content + * @param headers Header parameters for the request + * @param timeout Time after the request will be canceled, or null then it will never be canceled + * @return Result as String + */ + protected String sendHttpPutRequest(String url, String contentType, byte[] content, Map headers, + @Nullable Duration timeout) { + return JRuleActionHandler.get().sendHttpPutRequest(url, contentType, + content != null ? new ByteArrayInputStream(content) : null, headers, timeout); } /** @@ -164,7 +185,27 @@ protected String sendHttpPostRequest(String url, @Nullable Duration timeout) { */ protected String sendHttpPostRequest(String url, String contentType, String content, Map headers, @Nullable Duration timeout) { - return JRuleActionHandler.get().sendHttpPostRequest(url, contentType, content, headers, timeout); + return JRuleActionHandler.get().sendHttpPostRequest(url, contentType, + content != null ? new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)) : null, headers, + timeout); + } + + /** + * Sends a POST-HTTP request with the given content, request headers, and timeout in ms, and returns the result as a + * String + *
+ * + * @param url Target URL + * @param contentType @see javax.ws.rs.core.MediaType + * @param content Request content + * @param headers Header parameters for the request + * @param timeout Time after the request will be canceled, or null then it will never be canceled + * @return Result as String + */ + protected String sendHttpPostRequest(String url, String contentType, byte[] content, Map headers, + @Nullable Duration timeout) { + return JRuleActionHandler.get().sendHttpPostRequest(url, contentType, + content != null ? new ByteArrayInputStream(content) : null, headers, timeout); } /** diff --git a/src/test/java/org/openhab/automation/jrule/rules/JRuleHttpTest.java b/src/test/java/org/openhab/automation/jrule/rules/JRuleHttpTest.java new file mode 100644 index 00000000..82ed7e18 --- /dev/null +++ b/src/test/java/org/openhab/automation/jrule/rules/JRuleHttpTest.java @@ -0,0 +1,102 @@ +package org.openhab.automation.jrule.rules; + +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.HashMap; + +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.MediaType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.openhab.core.io.net.http.HttpUtil; + +class JRuleHttpTest { + private final JRule jRule = new JRule(); + + @Test + void testSendHttpGetRequest() { + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.GET), Mockito.anyString(), Mockito.isNull(), + Mockito.isNull(), Mockito.isNull(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpGetRequest("http://notexistingurl/service", Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.GET), Mockito.anyString(), Mockito.any(), + Mockito.isNull(), Mockito.isNull(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpGetRequest("http://notexistingurl/service", new HashMap<>(), Duration.ofSeconds(5))); + } + } + + @Test + void testSendHttpPutRequest() { + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.PUT), Mockito.anyString(), Mockito.isNull(), + Mockito.isNull(), Mockito.isNull(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpPutRequest("http://notexistingurl/service", Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.PUT), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", jRule.sendHttpPutRequest("http://notexistingurl/service", + MediaType.APPLICATION_JSON, "string-content", new HashMap<>(), Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.PUT), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpPutRequest("http://notexistingurl/service", MediaType.APPLICATION_OCTET_STREAM, + "binary-content".getBytes(StandardCharsets.UTF_8), new HashMap<>(), Duration.ofSeconds(5))); + } + } + + @Test + void testSendHttpPostRequest() { + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.POST), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpPostRequest("http://notexistingurl/service", Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.POST), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", jRule.sendHttpPostRequest("http://notexistingurl/service", + MediaType.APPLICATION_JSON, "string-content", new HashMap<>(), Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.POST), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpPostRequest("http://notexistingurl/service", MediaType.APPLICATION_OCTET_STREAM, + "binary-content".getBytes(StandardCharsets.UTF_8), new HashMap<>(), Duration.ofSeconds(5))); + } + } + + @Test + void testSendHttpDeleteRequest() { + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.DELETE), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", + jRule.sendHttpDeleteRequest("http://notexistingurl/service", Duration.ofSeconds(5))); + } + + try (MockedStatic mocked = Mockito.mockStatic(HttpUtil.class)) { + mocked.when(() -> HttpUtil.executeUrl(Mockito.eq(HttpMethod.DELETE), Mockito.anyString(), Mockito.any(), + Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn("foo"); + Assertions.assertEquals("foo", jRule.sendHttpDeleteRequest("http://notexistingurl/service", new HashMap<>(), + Duration.ofSeconds(5))); + } + } +}