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

[v3.x] Migrate all tests from restlet-client to rest-assured #99

Merged
merged 6 commits into from
May 8, 2023
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
81 changes: 7 additions & 74 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
</developers>

<properties>
<restlet.version>2.3.12</restlet.version>
<hadoop.version>2.6.0-cdh5.16.1</hadoop.version>
<node.version>v8.11.4</node.version>
<npm.version>6.4.1</npm.version>
Expand Down Expand Up @@ -66,11 +65,6 @@
</dependencyManagement>

<repositories>
<repository>
<id>maven-restlet</id>
<name>Restlet Framework repository</name>
<url>https://maven.restlet.talend.com</url>
</repository>

<repository>
<id>jfrog-genepi-maven</id>
Expand Down Expand Up @@ -157,64 +151,9 @@
</dependency>

<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.jetty</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.html</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.fileupload</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.slf4j</artifactId>
<version>${restlet.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.freemarker</artifactId>
<version>${restlet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.17</version>
<version>4.0.11</version>
<type>pom</type>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -278,11 +217,6 @@
<version>2.11.5</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
Expand All @@ -302,12 +236,6 @@

<!-- Test Dependencies -->

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
Expand Down Expand Up @@ -405,6 +333,11 @@
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ public ValidatedApiTokenResponse validateApiToken(String token) {
Publisher<Authentication> authentication = validator.validateToken(token, null);

return Mono.<ValidatedApiTokenResponse>create(emitter -> {

authentication.subscribe(new Subscriber<Authentication>() {

private Subscription subscription;



@Override
public void onComplete() {

//handle empty publisher. e.g. when token is invalid
emitter.success(ValidatedApiTokenResponse.error(MESSAGE_INVALID_API_TOKEN));
}

@Override
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/cloudgene/mapred/steps/GroovyStep.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package cloudgene.mapred.steps;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;

import com.google.common.base.Throwables;

import cloudgene.mapred.jobs.CloudgeneContext;
import cloudgene.mapred.jobs.CloudgeneStep;
import cloudgene.mapred.wdl.WdlStep;
import cloudgene.sdk.internal.WorkflowContext;
import genepi.io.FileUtil;
import groovy.util.GroovyScriptEngine;

public class GroovyStep extends CloudgeneStep {

@Override
Expand Down Expand Up @@ -52,13 +51,19 @@ public boolean run(WdlStep step, CloudgeneContext context) {

} catch (Exception e) {
if (e.getCause() != null) {
context.error("Error in script " + script + ":\n" + Throwables.getStackTraceAsString(e.getCause()));
context.error("Error in script " + script + ":\n" + getStackTraceAsString(e.getCause()));
} else {
context.error("Error in script " + script + ":\n" + Throwables.getStackTraceAsString(e));
context.error("Error in script " + script + ":\n" + getStackTraceAsString(e));
}
return false;
}

}

public static String getStackTraceAsString(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}

}
7 changes: 0 additions & 7 deletions src/main/java/cloudgene/mapred/steps/LinkTestStep.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package cloudgene.mapred.steps;

import java.lang.reflect.Method;

import com.google.common.base.Throwables;

import cloudgene.mapred.jobs.CloudgeneContext;
import cloudgene.mapred.jobs.CloudgeneStep;
import cloudgene.mapred.wdl.WdlStep;
import cloudgene.sdk.internal.WorkflowContext;
import genepi.io.FileUtil;
import genepi.io.text.LineWriter;
import groovy.util.GroovyScriptEngine;

public class LinkTestStep extends CloudgeneStep {

Expand Down

This file was deleted.

57 changes: 18 additions & 39 deletions src/test/java/cloudgene/mapred/api/v2/admin/ChangeGroupTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package cloudgene.mapred.api.v2.admin;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.restlet.data.Form;
import org.restlet.resource.ClientResource;

import cloudgene.mapred.TestApplication;
import cloudgene.mapred.core.User;
import cloudgene.mapred.database.UserDao;
import cloudgene.mapred.util.CloudgeneClient;
import cloudgene.mapred.util.CloudgeneClientRestAssured;
import cloudgene.mapred.util.HashUtil;
import cloudgene.mapred.util.LoginToken;
import cloudgene.mapred.util.TestMailServer;
import genepi.db.Database;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.restassured.RestAssured;
import io.restassured.http.Header;
import jakarta.inject.Inject;

@MicronautTest
@MicronautTest()
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ChangeGroupTest {

@Inject
TestApplication application;

@Inject
CloudgeneClient client;
CloudgeneClientRestAssured client;

@BeforeAll
protected void setUp() throws Exception {
Expand All @@ -55,28 +51,18 @@ protected void setUp() throws Exception {
}

@Test
public void testWithWrongCredentials() throws JSONException, IOException {
public void testWithWrongCredentials() {

LoginToken token = client.login("username-group-test", "oldpassword");
Header accessToken = client.login("username-group-test", "oldpassword");

Database database = application.getDatabase();
UserDao userDao = new UserDao(database);

User oldUser = userDao.findByUsername("username-group-test");

// try to update with no credentials
ClientResource resource = client.createClientResource("/api/v2/admin/users/changegroup", token);
Form form = new Form();
form.set("username", "username-group-test");
form.set("role", "user,newgroup,test");

try {
resource.post(form);
} catch (Exception e) {

}
assertEquals(403, resource.getStatus().getCode());
resource.release();
RestAssured.given().header(accessToken).and().formParam("username", "username-group-test").and()
.formParam("role", "user,newgroup,test").when().post("/api/v2/admin/users/changegroup").then()
.statusCode(403);

User newUser = userDao.findByUsername("username-group-test");
// check if user in database is still the same
Expand All @@ -86,7 +72,7 @@ public void testWithWrongCredentials() throws JSONException, IOException {
}

@Test
public void testWithAdminCredentials() throws JSONException, IOException {
public void testWithAdminCredentials() {

Database database = application.getDatabase();
UserDao userDao = new UserDao(database);
Expand All @@ -102,20 +88,13 @@ public void testWithAdminCredentials() throws JSONException, IOException {
assertFalse(oldUser.hasRole("secret-group"));
assertFalse(oldUser.isAdmin());

LoginToken token = client.login("admin", "admin1978");

// try to update invalid password
ClientResource resource = client.createClientResource("/api/v2/admin/users/changegroup", token);
Form form = new Form();
form.set("username", "username-group-test");
form.set("role", "user,newgroup,test");
Header accessToken = client.login("admin", "admin1978");

resource.post(form);
assertEquals(200, resource.getStatus().getCode());
JSONObject object = new JSONObject(resource.getResponseEntity().getText());
assertEquals("username-group-test", object.get("username"));
assertEquals("user,newgroup,test", object.get("role"));
resource.release();
// update group
RestAssured.given().header(accessToken).and().formParam("username", "username-group-test").and()
.formParam("role", "user,newgroup,test").when().post("/api/v2/admin/users/changegroup").then()
.statusCode(200).body("username", equalTo("username-group-test")).and()
.body("role", equalTo("user,newgroup,test"));

User newUser = userDao.findByUsername("username-group-test");
// check update
Expand Down
Loading