Skip to content

Commit

Permalink
Add simple test (#18)
Browse files Browse the repository at this point in the history
* create config properties

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* port simple test

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* set JsonProperty annotation

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* fix null pointers when converting json string to object

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* Address some checkstyle errors

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* remove duplicate jacoco skip

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* fix incorrect file name

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* remove file

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* restore file

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* fix compile error

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* temporarily remove query metadata file

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* rename to querymeta

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* use clean package command

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* fix file casing

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* revert change to clean package

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* set config properties

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* Add checks for configs

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* Set to use OSSBUILD env

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

* fix wrong port number

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>

---------

Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>
  • Loading branch information
SanjulaGanepola authored Aug 14, 2024
1 parent bd23897 commit 84eab31
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 232 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ on:
pull_request:
branches: [ "main" ]

env:
CONFIG_PROPERTIES: ./src/test/resources/config.properties
CONFIG_PROPERTIES_SAMPLE: ./src/test/resources/config.properties.sample

jobs:
build:

runs-on: ubuntu-latest

environment: OSSBUILD

steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand All @@ -23,6 +29,14 @@ jobs:
distribution: 'temurin'
cache: maven

- name: Set Test Secrets
run: |
cp ${{ env.CONFIG_PROPERTIES_SAMPLE }} ${{ env.CONFIG_PROPERTIES }}
sed -i 's/^IBMI_HOST=/IBMI_HOST=${{ secrets.IBMI_HOST }}/' ${{ env.CONFIG_PROPERTIES }}
sed -i 's/^IBMI_USER=/IBMI_USER=${{ secrets.IBMI_USER }}/' ${{ env.CONFIG_PROPERTIES }}
sed -i 's/^IBMI_PASSWORD=/IBMI_PASSWORD=${{ secrets.IBMI_PASSWORD }}/' ${{ env.CONFIG_PROPERTIES }}
sed -i 's/^IBMI_PORT=/IBMI_PORT=${{ secrets.IBMI_PORT }}/' ${{ env.CONFIG_PROPERTIES }}
- name: Build with Maven
run: mvn -B package --file pom.xml

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
src/test/resources/config.properties
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/io/github/mapapire/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -144,9 +142,7 @@ public CompletableFuture<SqlJob> addJob(PoolAddOptions options) {
if (newSqlJob.getStatus() == JobStatus.NotStarted) {
try {
newSqlJob.connect(this.options.getCreds()).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/io/github/mapapire/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -35,8 +33,8 @@ public Query(SqlJob job, String query, QueryOptions opts) {
// TODO: Fix constructor
this.isPrepared = opts.getParameters() != null;
this.parameters = opts.getParameters();
this.isCLCommand = opts.isClCommand();
this.isTerseResults = opts.isTerseResults();
this.isCLCommand = opts.getIsClCommand();
this.isTerseResults = opts.getIsTerseResults();

Query.globalQueryList.add(this);
}
Expand Down Expand Up @@ -78,9 +76,7 @@ public void cleanup() {
CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
try {
allOf.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
} catch (Exception e) {
e.printStackTrace();
}

Expand Down Expand Up @@ -113,8 +109,10 @@ public <T> CompletableFuture<QueryResult<T>> execute(int rowsToFetch) {
queryObject.put("sql", this.sql);
queryObject.put("terse", this.isTerseResults);
queryObject.put("rows", rowsToFetch);
JsonNode parameters = objectMapper.valueToTree(this.parameters);
queryObject.set("parameters", parameters);
if (this.parameters != null) {
JsonNode parameters = objectMapper.valueToTree(this.parameters);
queryObject.set("parameters", parameters);
}
}

this.rowsToFetch = rowsToFetch;
Expand All @@ -132,15 +130,15 @@ public <T> CompletableFuture<QueryResult<T>> execute(int rowsToFetch) {
QueryResult<T> queryResult;
try {
queryResult = objectMapper.readValue(result, QueryResult.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

this.state = queryResult.isDone() ? QueryState.RUN_DONE
this.state = queryResult.getIsDone() ? QueryState.RUN_DONE
: QueryState.RUN_MORE_DATA_AVAILABLE;

if (!queryResult.isSuccess() && !this.isCLCommand) {
if (!queryResult.getSuccess() && !this.isCLCommand) {
this.state = QueryState.ERROR;

List<String> errorList = new ArrayList<>();
Expand Down Expand Up @@ -196,10 +194,10 @@ public CompletableFuture<QueryResult<T>> fetchMore(int rowsToFetch) {
// conversion to conform to QueryResult<T>
QueryResult<T> queryResult = objectMapper.readValue(result, QueryResult.class);

this.state = queryResult.isDone() ? QueryState.RUN_DONE
this.state = queryResult.getIsDone() ? QueryState.RUN_DONE
: QueryState.RUN_MORE_DATA_AVAILABLE;

if (!queryResult.isSuccess()) {
if (!queryResult.getSuccess()) {
this.state = QueryState.ERROR;

String error = queryResult.getError();
Expand All @@ -211,12 +209,12 @@ public CompletableFuture<QueryResult<T>> fetchMore(int rowsToFetch) {
}

return CompletableFuture.completedFuture(queryResult);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}
});
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}
Expand All @@ -232,7 +230,7 @@ public CompletableFuture<String> close() {

try {
return job.send(objectMapper.writeValueAsString(queryObject));
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}
Expand Down
83 changes: 46 additions & 37 deletions src/main/java/io/github/mapapire/SqlJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import javax.net.ssl.X509TrustManager;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.github.mapapire.types.ConnectionResult;
import io.github.mapapire.types.DaemonServer;
Expand All @@ -42,17 +42,17 @@
import io.github.mapapire.types.jdbcOptions.Property;
import io.github.mapapire.types.jdbcOptions.TransactionIsolation;

class ReqRespFmt {
String id;
// class ReqRespFmt {
// String id;

public ReqRespFmt(String id) {
this.id = id;
}
// public ReqRespFmt(String id) {
// this.id = id;
// }

String getId() {
return this.id;
}
};
// String getId() {
// return this.id;
// }
// };

class NoAuthTrustManager implements X509TrustManager {
@Override
Expand Down Expand Up @@ -85,9 +85,10 @@ public class SqlJob {
private String traceFile;
private boolean isTracingChannelData = false;
private String id;
private JDBCOptions options;
private JDBCOptions options = new JDBCOptions();
private final Map<String, CompletableFuture<String>> responseMap = new HashMap<>();
private static final ObjectMapper objectMapper = new ObjectMapper();
CompletableFuture<String> openedConnectionFuture;

// currently unused but we will inevitably need a unique ID assigned to each
// instance since server job names can be reused in some circumstances
Expand Down Expand Up @@ -124,10 +125,11 @@ private CompletableFuture<WebSocketClient> getChannel(DaemonServer db2Server) {
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
httpHeaders.put("Authorization", "Basic " + encodedAuth);

WebSocketClient wsc = new WebSocketClient(uri, null, httpHeaders, 5000) {
WebSocketClient wsc = new WebSocketClient(uri, new Draft_6455(), httpHeaders, 5000) {
@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("Opened connection");
openedConnectionFuture.complete("Opened connection");
}

@Override
Expand All @@ -137,12 +139,14 @@ public void onMessage(String message) {
}

try {
ReqRespFmt response = objectMapper.readValue(message, ReqRespFmt.class);
CompletableFuture<String> future = responseMap.get(response.getId());
Map<String, Object> response = objectMapper.readValue(message, Map.class);
String id = (String) response.get("id");

CompletableFuture<String> future = responseMap.get(id);
if (future != null) {
future.complete(message);
}
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -161,7 +165,7 @@ public void onError(Exception ex) {
};
wsc.setSocketFactory(factory);

if (db2Server.isIgnoreUnauthorized()) {
if (db2Server.getIgnoreUnauthorized()) {
// TODO:
}

Expand All @@ -183,18 +187,19 @@ public CompletableFuture<String> send(String content) {
}

try {
ReqRespFmt req = objectMapper.readValue(content, ReqRespFmt.class);
Map<String, Object> req = objectMapper.readValue(content, Map.class);
String id = (String) req.get("id");

CompletableFuture<String> future = new CompletableFuture<>();
responseMap.put(req.getId(), future);
responseMap.put(id, future);
this.socket.send(content);
future.thenApply(message -> {
responseMap.remove(req.getId());
return message;
});
} catch (JsonProcessingException e) {
String message = future.get();
responseMap.remove(id);
return CompletableFuture.completedFuture(message);
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}
return null;
}

public JobStatus getStatus() {
Expand All @@ -212,6 +217,8 @@ public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) {
try {
this.socket = this.getChannel(db2Server).get();
this.socket.connect();
openedConnectionFuture = new CompletableFuture<>();
openedConnectionFuture.get();
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -234,7 +241,9 @@ public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) {
connectionObject.put("type", "connect");
connectionObject.put("technique", "tcp");// TODO: DOVE does not work in cli mode
connectionObject.put("application", "Java client");
connectionObject.put("props", props.length() > 0 ? props : null);
if (props.length() > 0) {
connectionObject.put("props", props);
}

String result;
try {
Expand All @@ -247,12 +256,12 @@ public CompletableFuture<ConnectionResult> connect(DaemonServer db2Server) {
ConnectionResult connectResult;
try {
connectResult = objectMapper.readValue(result, ConnectionResult.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

if (connectResult.isSuccess()) {
if (connectResult.getSuccess()) {
this.status = JobStatus.Ready;
} else {
this.dispose();
Expand Down Expand Up @@ -318,12 +327,12 @@ public CompletableFuture<VersionCheckResult> getVersion() {
VersionCheckResult version;
try {
version = objectMapper.readValue(result, VersionCheckResult.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

if (!version.isSuccess()) {
if (!version.getSuccess()) {
if (version.getError() != null) {
throw new Error(version.getError());
} else {
Expand Down Expand Up @@ -356,12 +365,12 @@ public CompletableFuture<ExplainResults<?>> explain(String statement, ExplainTyp
ExplainResults<?> explainResult;
try {
explainResult = objectMapper.readValue(result, ExplainResults.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

if (!explainResult.isSuccess()) {
if (!explainResult.getSuccess()) {
if (explainResult.getError() != null) {
throw new Error(explainResult.getError());
} else {
Expand Down Expand Up @@ -392,12 +401,12 @@ public CompletableFuture<GetTraceDataResult> getTraceData() {
GetTraceDataResult rpy;
try {
rpy = objectMapper.readValue(result, GetTraceDataResult.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

if (!rpy.isSuccess()) {
if (!rpy.getSuccess()) {
if (rpy.getError() != null) {
throw new Error(rpy.getError());
} else {
Expand Down Expand Up @@ -428,12 +437,12 @@ public CompletableFuture<SetConfigResult> setTraceConfig(ServerTraceDest dest, S
SetConfigResult rpy;
try {
rpy = objectMapper.readValue(result, SetConfigResult.class);
} catch (JsonProcessingException e) {
} catch (Exception e) {
e.printStackTrace();
return CompletableFuture.completedFuture(null);
}

if (!rpy.isSuccess()) {
if (!rpy.getSuccess()) {
if (rpy.getError() != null) {
throw new Error(rpy.getError());
} else {
Expand All @@ -449,7 +458,7 @@ public CompletableFuture<SetConfigResult> setTraceConfig(ServerTraceDest dest, S

public Query<?> clcommand(String cmd) {
QueryOptions options = new QueryOptions();
options.setClCommand(true);
options.setIsClCommand(true);
return new Query(this, cmd, options);
}

Expand Down
Loading

0 comments on commit 84eab31

Please sign in to comment.