Skip to content

Commit

Permalink
[irods#42] Small fixups to Response, queryOperations, and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-i-am012 committed Aug 2, 2024
1 parent 5fb5d8d commit 7c8f46f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
<version>2.23.1</version>
</dependency>

</dependencies>
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/org/example/Operations/QueryOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,14 @@ public Response executeSpecificQuery(String token, String name, QueryExecuteSpec
return new Response(response.statusCode(), response.body());
}

public Response addSpecificQuery(String token, String name, String sql) {
public Response addSpecificQuery(String token, String name, String sql) throws UnsupportedEncodingException {
Map<Object, Object> formData = new HashMap<>();
formData.put("op", "add_specific_query");
formData.put("name", name);

// need to URL encode the spaces in the query to prevent illegal characters in the query
try {
String encodedSql = URLEncoder.encode(sql, StandardCharsets.UTF_8.toString());
formData.put("sql", encodedSql);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedSql = URLEncoder.encode(sql, StandardCharsets.UTF_8.toString());
formData.put("sql", encodedSql);

HttpResponse<String> response = HttpRequestUtil.sendAndParsePOST(formData, baseUrl, token, client.getClient());
return new Response(response.statusCode(), response.body());
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/example/Util/HttpRequestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ public static HttpResponse<String> sendAndParsePOST(Map<Object, Object> formData
.build();

// sending request
HttpResponse<String> response;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
return client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
return response;
}

/**
Expand Down Expand Up @@ -73,12 +71,10 @@ public static HttpResponse<String> sendAndParseGET(Map<Object, Object> formData
.build();

// sending request
HttpResponse<String> response;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
return client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
return response;
}
}
7 changes: 1 addition & 6 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="${layout}"/>
</Console>
<File name="LogFile" fileName="app.log">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="LogFile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
8 changes: 2 additions & 6 deletions src/test/java/org/example/Operations/QueryOperationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.example.Wrapper;
import org.junit.Before;
import org.junit.Test;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -24,9 +23,6 @@ public class QueryOperationsTest {
private Wrapper alice;
private String aliceToken;

private Wrapper bob;
private String bobToken;

private String host;
private final ObjectMapper mapper = new ObjectMapper();
private static final Logger logger = LogManager.getLogger(QueryOperationsTest.class);
Expand Down Expand Up @@ -88,7 +84,7 @@ public void testAddingAndRemovingSpecificQueries() {
String sql = "select token_id from R_TOKN_MAIN where token_name = 'rodsgroup'";

// Show that rodsusers are NOT allowed to add specific queries.
res = rods.queryOperations().addSpecificQuery(aliceToken, specificQueryName, sql);
res = assertDoesNotThrow(() -> rods.queryOperations().addSpecificQuery(aliceToken, specificQueryName, sql));
logger.debug(res.getBody());
assertEquals("Adding specific query request failed", 200, res.getHttpStatusCode());
// ierror code of -13000: SYS_NO_API_PRIV
Expand All @@ -97,7 +93,7 @@ public void testAddingAndRemovingSpecificQueries() {

try {
// Show that only rodsadmin are allowed to add specific queries.
res = rods.queryOperations().addSpecificQuery(rodsToken, specificQueryName, sql);
res = assertDoesNotThrow(() -> rods.queryOperations().addSpecificQuery(rodsToken, specificQueryName, sql));
logger.debug(res.getBody());
assertEquals("Adding specific query request failed", 200, res.getHttpStatusCode());
assertEquals("Adding specific query failed",0,
Expand Down

0 comments on commit 7c8f46f

Please sign in to comment.