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

mock multipart response with relation id removed from the arrow part #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions rai-sdk-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>rai-sdk</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>

<build>
Expand All @@ -62,5 +67,11 @@
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
5 changes: 5 additions & 0 deletions rai-sdk-examples/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions rai-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
Expand Down
4 changes: 3 additions & 1 deletion rai-sdk/src/main/java/com/relationalai/MultipartReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ private static String getFieldName(String contentDisposition) {

// parameter parser can handle null input
Map<String,String> params = parser.parse(contentDisposition, ';');
fieldName = (String) params.get("name");
fieldName = params.get("name");
if (fieldName != null) {
fieldName = fieldName.trim();
} else {
fieldName = "";
}
}

Expand Down
93 changes: 93 additions & 0 deletions rai-sdk/src/test/java/com/relationalai/ExecuteAsyncMockedTest.java
Original file line number Diff line number Diff line change
@@ -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<ArrowRelation> () {
{
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<Object>();

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;
}
}
Binary file added rai-sdk/src/test/resources/multipart.data
Binary file not shown.