Skip to content

Commit

Permalink
✨ (142): First iteration of wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Klaus committed Dec 7, 2024
1 parent 9f78399 commit 0915e89
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class CreateUserInKeycloak {

public CreateUserInKeycloak(
Keycloak keycloak,
@ConfigProperty(name = "app.hopps.vereine.auth.realm-name") String realmName,
@ConfigProperty(name = "app.hopps.vereine.auth.default-role") String ownerRoleName) {
@ConfigProperty(name = "app.hopps.org.auth.realm-name") String realmName,
@ConfigProperty(name = "app.hopps.org.auth.default-role") String ownerRoleName) {
this.keycloak = keycloak;
this.realmResource = keycloak.realm(realmName);
this.usersResource = realmResource.users();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import app.hopps.org.jpa.Member;
import app.hopps.org.rest.RestValidator.ValidationResult;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Validator;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
Expand Down Expand Up @@ -32,7 +31,7 @@ public ValidationResult validate(Member member) {
LOG.info("Validating Member {} {}", member.getFirstName(), member.getLastName());

if (result.isValid() == ValidationResult.Validity.INVALID) {
Response response = Response.status(HttpServletResponse.SC_BAD_REQUEST)
Response response = Response.status(Response.Status.BAD_REQUEST)
.entity(result)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,50 @@

import app.hopps.org.jpa.Organization;
import app.hopps.org.rest.RestValidator.ValidationResult;
import app.hopps.org.rest.model.NewOrganizationInput;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.inject.Named;
import jakarta.validation.Validator;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.kie.kogito.Model;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@Path("/organization")
public class OrganizationResource {

private static final Logger LOG = LoggerFactory.getLogger(OrganizationResource.class);

@Inject
Validator validator;

@Inject
@Named("NewOrganization")
Process<? extends Model> process;

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Operation(summary = "Create a new organization", operationId = "createNewOrganization")
@APIResponse(responseCode = "202", description = "Creation started successfully")
@APIResponse(responseCode = "400", description = "Validation of fields failed", content = @Content(mediaType = MediaType.APPLICATION_JSON))
public Response create(NewOrganizationInput input) {
Model model = process.createModel();
Map<String, Object> newOrganizationParameters = input.toModel();
model.fromMap(newOrganizationParameters);
ProcessInstance<? extends Model> instance = process.createInstance(model);
instance.start();

return Response.accepted().entity(instance).entity(instance.id()).build();
}

@POST
@Path(("/validate"))
@Consumes(MediaType.APPLICATION_JSON)
Expand All @@ -32,7 +59,7 @@ public ValidationResult validate(Organization organization) {
LOG.info("Validating Organization {}", organization.getName());

if (result.isValid() == ValidationResult.Validity.INVALID) {
Response response = Response.status(HttpServletResponse.SC_BAD_REQUEST)
Response response = Response.status(Response.Status.BAD_REQUEST)
.entity(result)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app.hopps.org.rest.model;

import app.hopps.org.jpa.Member;
import app.hopps.org.jpa.Organization;
import jakarta.validation.constraints.NotNull;

import java.util.HashMap;
import java.util.Map;

public record NewOrganizationInput(@NotNull OwnerInput owner, @NotNull OrganizationInput organization) {
public Map<String, Object> toModel() {
Map<String, Object> parameters = new HashMap<>();

Organization jpaOrg = new Organization();
jpaOrg.setSlug(organization().slug());
jpaOrg.setName(organization().name());
jpaOrg.setType(organization().type());
jpaOrg.setWebsite(organization().website());
jpaOrg.setAddress(organization().address());
jpaOrg.setProfilePicture(organization().profilePicture());
parameters.put("organization", jpaOrg);

Member member = new Member();
member.setEmail(owner().email());
member.setFirstName(owner().firstName());
member.setLastName(owner().lastName());
parameters.put("owner", member);

return parameters;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package app.hopps.org.rest.model;

import app.hopps.org.jpa.Address;
import app.hopps.org.jpa.Organization.TYPE;

import java.net.URL;

public record OrganizationInput(String name,
String slug,
TYPE type,
URL website,
URL profilePicture,
Address address) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package app.hopps.org.rest.model;

public record OwnerInput(String email, String firstName, String lastName) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ quarkus.flyway.clean-at-start=false
########################################
# Keycloak/auth
########################################
app.hopps.vereine.auth.default-role=Owner
app.hopps.vereine.auth.realm-name=quarkus
app.hopps.org.auth.default-role=Owner
app.hopps.org.auth.realm-name=quarkus
# override in prod
quarkus.keycloak.admin-client.server-url=http://localhost:8554
quarkus.keycloak.admin-client.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
class CreateUserInKeycloakTest {
Expand All @@ -25,11 +26,11 @@ class CreateUserInKeycloakTest {
Keycloak keycloak;

@Inject
@ConfigProperty(name = "app.hopps.vereine.auth.default-role")
@ConfigProperty(name = "app.hopps.org.auth.default-role")
String defaultRole;

@Inject
@ConfigProperty(name = "app.hopps.vereine.auth.realm-name")
@ConfigProperty(name = "app.hopps.org.auth.realm-name")
String realmName;

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package app.hopps.org.rest;

import app.hopps.org.jpa.Organization;
import app.hopps.org.rest.model.NewOrganizationInput;
import app.hopps.org.rest.model.OrganizationInput;
import app.hopps.org.rest.model.OwnerInput;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import jakarta.ws.rs.core.MediaType;
import org.instancio.Instancio;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import java.net.MalformedURLException;
import java.net.URI;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;

@QuarkusTest
@TestHTTPEndpoint(OrganizationResource.class)
class OrganizationResourceTests {

@Test
@DisplayName("should validate valid verein")
void shouldValidateValidVerein() {
Expand All @@ -24,8 +29,8 @@ void shouldValidateValidVerein() {
organization.setSlug("foobar");
organization.setId(null);

RestAssured.given()
.contentType("application/json")
given()
.contentType(MediaType.APPLICATION_JSON)
.body(organization)
.when()
.post("/validate")
Expand All @@ -43,8 +48,8 @@ void shouldInvalidateVereinWithoutName() {
organization.setId(null);
organization.setName("");

RestAssured.given()
.contentType("application/json")
given()
.contentType(MediaType.APPLICATION_JSON)
.body(organization)
.when()
.post("/validate")
Expand All @@ -54,4 +59,22 @@ void shouldInvalidateVereinWithoutName() {
.body("violations[0].propertyPath", equalTo("name"))
.body("violations[0].message", equalTo("must not be blank"));
}

@Test
void shouldStartCreatingOrganization() throws MalformedURLException {
OrganizationInput organizationInput = new OrganizationInput("Schützenverein", "schuetzenverein",
Organization.TYPE.EINGETRAGENER_VEREIN, URI.create("https://hopps.cloud").toURL(),
URI.create("https://hopps.cloud").toURL(), null);
OwnerInput ownerInput = new OwnerInput("[email protected]", "Test", "User");
NewOrganizationInput newOrganizationInput = new NewOrganizationInput(ownerInput, organizationInput);

given()
.contentType(MediaType.APPLICATION_JSON)
.body(newOrganizationInput)
.when()
.post()
.then()
.statusCode(202)
.body(any(String.class));
}
}

0 comments on commit 0915e89

Please sign in to comment.