Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow byte[] for http requests #196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
119 changes: 119 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,119 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
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;

/**
* The {@link JRuleHttpTest} for testing the available http methods
*
* @author Robert Delbrück - Initial contribution
*/
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)));
}
}
}
Loading