diff --git a/rai-sdk-examples/pom.xml b/rai-sdk-examples/pom.xml index 17e46d6..ac1a78d 100644 --- a/rai-sdk-examples/pom.xml +++ b/rai-sdk-examples/pom.xml @@ -39,6 +39,11 @@ rai-sdk ${project.parent.version} + + org.slf4j + slf4j-reload4j + 2.0.6 + @@ -62,5 +67,11 @@ maven-dependency-plugin + + + src/main/resources + true + + diff --git a/rai-sdk-examples/src/main/resources/log4j.properties b/rai-sdk-examples/src/main/resources/log4j.properties new file mode 100644 index 0000000..1a67939 --- /dev/null +++ b/rai-sdk-examples/src/main/resources/log4j.properties @@ -0,0 +1,5 @@ +log4j.rootLogger=DEBUG, stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %p : %m%n diff --git a/rai-sdk/pom.xml b/rai-sdk/pom.xml index 224b969..406f7c7 100644 --- a/rai-sdk/pom.xml +++ b/rai-sdk/pom.xml @@ -40,6 +40,18 @@ 5.8.2 test + + org.mockito + mockito-core + 4.1.0 + test + + + org.mockito + mockito-junit-jupiter + 4.1.0 + test + commons-fileupload commons-fileupload diff --git a/rai-sdk/src/main/java/com/relationalai/MultipartReader.java b/rai-sdk/src/main/java/com/relationalai/MultipartReader.java index 12ecc6e..a8abb36 100644 --- a/rai-sdk/src/main/java/com/relationalai/MultipartReader.java +++ b/rai-sdk/src/main/java/com/relationalai/MultipartReader.java @@ -136,9 +136,11 @@ private static String getFieldName(String contentDisposition) { // parameter parser can handle null input Map params = parser.parse(contentDisposition, ';'); - fieldName = (String) params.get("name"); + fieldName = params.get("name"); if (fieldName != null) { fieldName = fieldName.trim(); + } else { + fieldName = ""; } } diff --git a/rai-sdk/src/test/java/com/relationalai/ExecuteAsyncMockedTest.java b/rai-sdk/src/test/java/com/relationalai/ExecuteAsyncMockedTest.java new file mode 100644 index 0000000..e4c1b54 --- /dev/null +++ b/rai-sdk/src/test/java/com/relationalai/ExecuteAsyncMockedTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 2022 RelationalAI, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"): you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.relationalai; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import relationalai.protocol.Message; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@ExtendWith({TestExtension.class}) +public class ExecuteAsyncMockedTest extends UnitTest { + + @Test + void testExecuteAsyncMocked() throws HttpError, InterruptedException, IOException, URISyntaxException { + var client = createClient(); + client.setHttpClient(getMockedHttpClient()); + + var query = "x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}"; + + var rsp = client.executeAsync("mockedDB", "mockedEngine", query, true); + + var results = new ArrayList () { + { + add(new ArrowRelation("", Arrays.asList(new Object[] {1L, 2L, 3L, 4L, 5L}) )); + add(new ArrowRelation("", Arrays.asList(new Object[] {1L, 4L, 9L, 16L, 25L}) )); + add(new ArrowRelation("", Arrays.asList(new Object[] {1L, 8L, 27L, 64L, 125L}) )); + add(new ArrowRelation("", Arrays.asList(new Object[] {1L, 16L, 81L, 256L, 625L}) )); + } + }; + + var problems = new ArrayList(); + + var metadata = Message.MetadataInfo.parseFrom( + Files.readAllBytes( + Path.of(Paths.get(getClass().getResource("/metadata.pb").toURI()).toString()) + ) + ); + + assertEquals(rsp.metadata.toString(), metadata.toString()); + assertEquals(rsp.results, results); + assertEquals(rsp.problems, problems); + } + + HttpClient getMockedHttpClient() throws IOException, InterruptedException, URISyntaxException { + var httpRequest = HttpRequest.newBuilder() + .headers("User-Agent", "rai-sdk-java") + .uri(new URI("https://mocked/path/to/transactions")).build(); + var httpClient = Mockito.mock(HttpClient.class); + var txnhttpResponse = Mockito.mock(HttpResponse.class); + + Mockito.when(txnhttpResponse.statusCode()).thenReturn(200); + Mockito.when(txnhttpResponse.request()).thenReturn(httpRequest); + Mockito.when(txnhttpResponse.headers()).thenReturn( + HttpHeaders.of(new HashMap<>() {{put("Content-Type", new ArrayList<>() {{ add ("multipart/form-data; boundary=b11385ead6144ee0a9550db3672a7ccf"); }}); + }}, (k, v) -> true)); + Mockito.when(txnhttpResponse.body()).thenReturn(Files.readAllBytes(Path.of(Paths.get(getClass().getResource("/multipart.data").toURI()).toString()))); + + Mockito.when(httpClient.send(any(), any())).thenReturn(txnhttpResponse); + + return httpClient; + } +} diff --git a/rai-sdk/src/test/resources/multipart.data b/rai-sdk/src/test/resources/multipart.data new file mode 100644 index 0000000..4bd88bc Binary files /dev/null and b/rai-sdk/src/test/resources/multipart.data differ