use-case-test for create partner
This commit is contained in:
parent
c181500a1d
commit
1516332f9f
@ -0,0 +1,62 @@
|
||||
package net.hostsharing.hsadminng.hs.office.usecases;
|
||||
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static io.restassured.http.Method.POST;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = { HsadminNgApplication.class, JpaAttempt.class }
|
||||
)
|
||||
// @Tag("useCaseTest") FIXME
|
||||
@Order(1)
|
||||
class HsOfficePartnerUseCaseTest extends UseCaseTest {
|
||||
|
||||
@Test
|
||||
void shouldCreatePartner() {
|
||||
|
||||
http(POST, "/api/hs/office/persons", usingJsonBody("""
|
||||
{
|
||||
"personType": "NATURAL_PERSON",
|
||||
"familyName": "Tester",
|
||||
"givenName": "Temp Testi"
|
||||
}
|
||||
"""))
|
||||
.expecting(HttpStatus.CREATED)
|
||||
.keepingAs("partnerPerson.uuid");
|
||||
|
||||
http(POST, "/api/hs/office/contacts", usingJsonBody("""
|
||||
{
|
||||
"caption": "Temp Contact",
|
||||
"emailAddresses": {
|
||||
"main": "test@example.org"
|
||||
}
|
||||
}
|
||||
"""))
|
||||
.expecting(HttpStatus.CREATED)
|
||||
.keepingAs("contact.uuid");
|
||||
|
||||
http(POST, "/api/hs/office/partners", usingJsonBody("""
|
||||
{
|
||||
"partnerNumber": "20002",
|
||||
"partnerRel": {
|
||||
"anchorUuid": "${hostsharingPerson.uuid}",
|
||||
"holderUuid": "${partnerPerson.uuid}",
|
||||
"contactUuid": "${contact.uuid}"
|
||||
},
|
||||
"details": {
|
||||
"registrationOffice": "Registergericht Hamburg",
|
||||
"registrationNumber": "1234567"
|
||||
}
|
||||
}
|
||||
"""))
|
||||
.expecting(HttpStatus.CREATED)
|
||||
.keepingAs("partner20002.uuid");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
package net.hostsharing.hsadminng.hs.office.usecases;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.http.Method;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
|
||||
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity;
|
||||
import net.hostsharing.hsadminng.lambda.Reducer;
|
||||
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
|
||||
import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
public abstract class UseCaseTest extends ContextBasedTestWithCleanup {
|
||||
|
||||
final static String RUN_AS_USER = "superuser-alex@hostsharing.net"; // TODO.test: use global:AGENT when implemented
|
||||
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Autowired
|
||||
HsOfficePersonRepository personRepo;
|
||||
|
||||
@Autowired
|
||||
JpaAttempt jpaAttempt;
|
||||
|
||||
Map<String, String> aliases = new HashMap<>();
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
jpaAttempt.transacted(() ->
|
||||
{
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
aliases.put(
|
||||
"hostsharingPerson.uuid",
|
||||
personRepo.findPersonByOptionalNameLike("Hostsharing eG")
|
||||
.stream()
|
||||
.map(HsOfficePersonEntity::getUuid)
|
||||
.map(Object::toString)
|
||||
.reduce(Reducer::toSingleElement).orElseThrow());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
cleanupAllNew(HsOfficePartnerEntity.class);
|
||||
|
||||
// TODO: should not be necessary anymore, once it's deleted via after delete trigger
|
||||
cleanupAllNew(HsOfficeRelationRealEntity.class);
|
||||
}
|
||||
|
||||
JsonTemplate usingJsonBody(final String jsonTemplate) {
|
||||
return new JsonTemplate(jsonTemplate);
|
||||
}
|
||||
|
||||
HttpResponse http(final Method method, final String uriPath, final JsonTemplate bodyJsonTemplate) {
|
||||
final var request = RestAssured.given()
|
||||
.header("current-subject", RUN_AS_USER)
|
||||
.contentType(ContentType.JSON)
|
||||
.body(bodyJsonTemplate.with(aliases))
|
||||
.port(port);
|
||||
final var response =
|
||||
switch (method) {
|
||||
case POST -> request.when().post("http://localhost" + uriPath);
|
||||
default -> throw new IllegalStateException("HTTP method not implemented yet: " + method);
|
||||
};
|
||||
return new HttpResponse(response);
|
||||
}
|
||||
|
||||
static class JsonTemplate {
|
||||
|
||||
private final String template;
|
||||
|
||||
private JsonTemplate(final String jsonTemplate) {
|
||||
this.template = jsonTemplate;
|
||||
}
|
||||
|
||||
String with(final Map<String, String> aliases) {
|
||||
var resolved = new AtomicReference<String>(template);
|
||||
aliases.forEach((k, v) ->
|
||||
resolved.set(resolved.get().replace("${" + k + "}", v)));
|
||||
return resolved.get();
|
||||
}
|
||||
}
|
||||
|
||||
class HttpResponse {
|
||||
|
||||
private final ValidatableResponse response;
|
||||
|
||||
public HttpResponse(final Response response) {
|
||||
this.response = response.then().log().all().assertThat();
|
||||
}
|
||||
|
||||
HttpResponse expecting(final HttpStatus httpStatus) {
|
||||
response.statusCode(httpStatus.value())
|
||||
.contentType(ContentType.JSON);
|
||||
return this;
|
||||
}
|
||||
|
||||
void keepingAs(final String uuidAliasName) {
|
||||
final var location = response.header("Location", startsWith("http://localhost"))
|
||||
.extract().header("Location");
|
||||
final var newSubjectUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newSubjectUuid).isNotNull();
|
||||
aliases.put(uuidAliasName, newSubjectUuid.toString());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user