Skip to content

Commit

Permalink
allow byte[] for http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
querdenker2k committed Mar 1, 2024
1 parent 7d1afa2 commit e17d831
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,12 +85,11 @@ public String sendHttpGetRequest(String url, @Nullable Map<String, String> 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<String, String> 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);
}
Expand All @@ -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<String, String> 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);
}
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/org/openhab/automation/jrule/rules/JRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,7 +138,26 @@ protected String sendHttpPutRequest(String url, @Nullable Duration timeout) {
*/
protected String sendHttpPutRequest(String url, String contentType, String content, Map<String, String> 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<String, String> headers,
@Nullable Duration timeout) {
return JRuleActionHandler.get().sendHttpPutRequest(url, contentType,
content != null ? new ByteArrayInputStream(content) : null, headers, timeout);
}

/**
Expand Down Expand Up @@ -164,7 +185,27 @@ protected String sendHttpPostRequest(String url, @Nullable Duration timeout) {
*/
protected String sendHttpPostRequest(String url, String contentType, String content, Map<String, String> 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
* <br/>
*
* @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<String, String> headers,
@Nullable Duration timeout) {
return JRuleActionHandler.get().sendHttpPostRequest(url, contentType,
content != null ? new ByteArrayInputStream(content) : null, headers, timeout);
}

/**
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/org/openhab/automation/jrule/rules/JRuleHttpTest.java
Original file line number Diff line number Diff line change
@@ -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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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<HttpUtil> 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)));
}
}
}

0 comments on commit e17d831

Please sign in to comment.