The following code is give: User/UserResources/UserService I wrote: Schritt3.java
I have to write a test, but I am completely lost. I googled so much and did not come closer to a Solution.
The first try get´s this error: RESTEASY003320: Failed processing arguments of public java.util.List de.hse.swa.jaxquarkus.step3.UserResource.addUser(java.lang.String,java.lang.String,java.lang.String,java.lang.Boolean)
The second one thise on: Expected status code <200> but was <400>.
As you can see I have tried different encoding types and ways to parse data. But googeling the error messages did not help me at all. I don´t know if I am just on the wrong path or if there is something else wrong.
So the question is: How to correctly pass the data.
User.java
import javax.ws.rs.FormParam;
public class User {
private Long id;
@FormParam("username")
private String username;
@FormParam("password")
private String password;
@FormParam("firstName")
private String fullName;
@FormParam("isAdmin")
private boolean isAdmin = false;
public User() {
}
public User(String username, String password, String fullName, boolean isAdmin) {
this.username = username;
this.password = password;
this.fullName = fullName;
this.isAdmin = isAdmin;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// @JsonIgnore()
public String getPassword() {
return password;
}
// @JsonProperty()
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean admin) {
isAdmin = admin;
}
}
UserResource.java
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.Form;
import io.vertx.core.http.HttpServerRequest;
@RequestScoped
@Path("/step3/users")
public class UserResource {
@Inject
UserService service;
@Context
HttpServerRequest request;
@GET
@Produces("application/json")
public List<User> greeting() {
return service.getUsers();
}
@PUT
@Produces("application/json")
@Consumes("application/json")
public List<User> addUser(
@FormParam("username") String username,
@FormParam("password") String password,
@FormParam("fullName") String fullName,
@FormParam("isAdmin") Boolean isAdmin) {
User user = new User(username, password, fullName, isAdmin);
return service.addUser(user);
}
@POST
@Consumes("application/json")
@Produces("application/json")
public List<User> updateUser(@Form User form) {
User user = new User(form.getUsername(), form.getPassword(),
form.getFullName(), form.isAdmin());
return service.addUser(user);
}
}
User Service.java
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class UserService {
public static List<User> users = new ArrayList<User>();
public static Long id = 1L;
public List<User> getUsers() {
return users;
}
public List<User> addUser(User user) {
user.setId(id++);
users.add(user);
return users;
}
public List<User> updateUser(User user) {
for (int index = 0; index < users.size(); ++index) {
if (users.get(index).getId().equals(user.getId())) {
users.set(index, user);
break;
}
}
return users;
}
public List<User> removeUser(User user) {
for (int index = 0; index < users.size(); ++index) {
if (users.get(index).getId().equals(user.getId())) {
users.remove(index);
break;
}
}
return users;
}
}
Schritt3.java
package de.hse.swa.jaxquarkus;
import de.hse.swa.jaxquarkus.step3.User;
import static org.hamcrest.CoreMatchers.containsString;
import javax.ws.rs.core.MediaType;
import io.quarkus.test.junit.QuarkusTest;
//import io.restassured.RestAssured;
import io.restassured.response.Response;
//import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.given;
@QuarkusTest
public class Schritt3 {
String username = "test";
String password = "123";
String fullName = "testing";
Boolean isAdmin = false;
private static String requestBody = "{\n" +
" \"username\": \"123\",\n" +
" \"password\": \"456\",\n" +
" \"fullName\": \"789\",\n" +
" \"isAdmin\": \"true\" \n}";
@Test
public void postRequest()
{
/* First Try
Response response = given()
.header("Content-type", "application/json")
.contentType(ContentType.JSON)
//.body(requestBody)
.body(username + password + fullName + isAdmin)
.when()
.put("/step3/users")
.then()
.extract().response();
System.out.println("Respone from Step3 is:");
System.out.println(response.asString());
// Assertions.assertEquals("1234", response.jsonPath().getString("username"));
*/
/* Second Try
*/
RestAssured.baseURI = "http://localhost:8080";
given().urlEncodingEnabled(true)
.param("username", "user@site.com")
.param("password", "Pas54321")
.param("fullName", "MAtze")
.param("isAdmin", "true")
//.header("Accept", ContentType.JSON.getAcceptHeader())
// .header("Content-type", "application/json")
.contentType("application/json")
.put("/step3/users")
.then().statusCode(200);
}
}