hs.admin.partner from API via Controller to Entity
This commit is contained in:
@ -38,18 +38,32 @@ public class ArchTest {
|
||||
|
||||
@com.tngtech.archunit.junit.ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsPackagesRule = classes()
|
||||
public static final ArchRule testPackagesRule = classes()
|
||||
.that().resideInAPackage("..test.(*)..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage("..test.(*)..");
|
||||
|
||||
@com.tngtech.archunit.junit.ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsPackagePackageRule = classes()
|
||||
public static final ArchRule testPackagePackageRule = classes()
|
||||
.that().resideInAPackage("..test.pac..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage("..test.pac..");
|
||||
|
||||
@com.tngtech.archunit.junit.ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsAdminPackagesRule = classes()
|
||||
.that().resideInAPackage("..hs.admin.(*)..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage("..hs.admin.(*)..");
|
||||
|
||||
@com.tngtech.archunit.junit.ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsAdminPartnerPackageRule = classes()
|
||||
.that().resideInAPackage("..hs.admin.partner..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage("..hs.admin.partner..");
|
||||
|
||||
@com.tngtech.archunit.junit.ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule acceptsAnnotationOnMethodsRule = methods()
|
||||
|
@ -0,0 +1,14 @@
|
||||
package net.hostsharing.hsadminng.hs.admin.contact;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
class HsAdminContactRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
void test() throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,195 @@
|
||||
package net.hostsharing.hsadminng.hs.admin.partner;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import net.hostsharing.hsadminng.Accepts;
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
|
||||
import static net.hostsharing.test.JsonBuilder.jsonObject;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = HsadminNgApplication.class
|
||||
)
|
||||
@Transactional
|
||||
class HsAdminPartnerControllerAcceptanceTest {
|
||||
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Autowired
|
||||
Context context;
|
||||
|
||||
@Autowired
|
||||
Context contextMock;
|
||||
@Autowired
|
||||
HsAdminPartnerRepository partnerRepository;
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Partner:F(Find)" })
|
||||
class ListPartners {
|
||||
|
||||
@Test
|
||||
void testHostsharingAdmin_withoutAssumedRoles_canViewAllPartners_ifNoCriteriaGiven() {
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/admin/partners")
|
||||
.then().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("[0].contact.label", is("Ixx AG"))
|
||||
.body("[0].person.tradeName", is("Ixx AG"))
|
||||
.body("[1].contact.label", is("Ypsilon GmbH"))
|
||||
.body("[1].person.tradeName", is("Ypsilon GmbH"))
|
||||
.body("[2].contact.label", is("Zett OHG"))
|
||||
.body("[2].person.tradeName", is("Zett OHG"))
|
||||
.body("size()", greaterThanOrEqualTo(3));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Partner:C(Create)" })
|
||||
class AddPartner {
|
||||
|
||||
private final static String NEW_PARTNER_JSON_WITHOUT_UUID =
|
||||
"""
|
||||
{
|
||||
"person": {
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "Test Corp.",
|
||||
"givenName": null,
|
||||
"familyName": null
|
||||
},
|
||||
"contact": {
|
||||
"label": "Test Corp.",
|
||||
"postalAddress": "Test Corp.\\nTestweg 50\\n20001 Hamburg",
|
||||
"emailAddresses": "office@example.com",
|
||||
"phoneNumbers": "040 12345"
|
||||
},
|
||||
"registrationOffice": "Registergericht Hamburg",
|
||||
"registrationNumber": "123456",
|
||||
"birthName": null,
|
||||
"birthday": null,
|
||||
"dateOfDeath": null
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void hostsharingAdmin_withoutAssumedRole_canAddPartner_withExplicitUuid() {
|
||||
|
||||
final var givenUUID = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6");
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body(jsonObject(NEW_PARTNER_JSON_WITHOUT_UUID)
|
||||
.with("uuid", givenUUID.toString()).toString())
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/admin/partners")
|
||||
.then().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("uuid", is("3fa85f64-5717-4562-b3fc-2c963f66afa6"))
|
||||
.body("registrationNumber", is("123456"))
|
||||
.body("person.tradeName", is("Test Corp."))
|
||||
.header("Location", startsWith("http://localhost"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new partner can be viewed by its own admin
|
||||
final var newUserUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newUserUuid).isEqualTo(givenUUID);
|
||||
// TODO: context.define("partner-admin@ttt.example.com");
|
||||
// assertThat(partnerRepository.findByUuid(newUserUuid))
|
||||
// .hasValueSatisfying(c -> assertThat(c.getPerson().getTradeName()).isEqualTo("Test Corp."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostsharingAdmin_withoutAssumedRole_canAddPartner_withGeneratedUuid() {
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body(NEW_PARTNER_JSON_WITHOUT_UUID)
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/admin/partners")
|
||||
.then().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("uuid", isUuidValid())
|
||||
.body("registrationNumber", is("123456"))
|
||||
.body("person.tradeName", is("Test Corp."))
|
||||
.header("Location", startsWith("http://localhost"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new partner can be viewed by its own admin
|
||||
final var newUserUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
// TODO: context.define("partner-admin@ttt.example.com");
|
||||
// assertThat(partnerRepository.findByUuid(newUserUuid))
|
||||
// .hasValueSatisfying(c -> assertThat(c.getPerson().getTradeName()).isEqualTo("Test Corp."));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Partner:R(Read)" })
|
||||
class GetPartner {
|
||||
|
||||
@Test
|
||||
void hostsharingAdmin_withoutAssumedRole_canGetArbitraryPartner() {
|
||||
// TODO: final var givenPartnerUuid = partnerRepository.findPartnerByOptionalNameLike("Ixx").get(0).getUuid();
|
||||
final var givenPartnerUuid = UUID.randomUUID();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/admin/partners/" + givenPartnerUuid)
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("person.tradeName", is("Ixx AG"))
|
||||
.body("contact.label", is("Ixx AG"));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
@Accepts({ "Partner:X(Access Control)" })
|
||||
void normalUser_canNotGetUnrelatedPartner() {
|
||||
// TODO: final var givenPartnerUuid = partnerRepository.findPartnerByOptionalNameLike("Ixx").get(0).getUuid();
|
||||
final UUID givenPartnerUuid = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6");
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "somebody@example.org")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/admin/partners/" + givenPartnerUuid)
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(404);
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
package net.hostsharing.hsadminng.hs.admin.partner;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.context.ContextBasedTest;
|
||||
import net.hostsharing.hsadminng.hs.admin.person.HsAdminPersonEntity;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.admin.partner.TestHsAdminPartner.testLtd;
|
||||
import static net.hostsharing.test.JpaAttempt.attempt;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@Disabled
|
||||
@DataJpaTest
|
||||
@ComponentScan(basePackageClasses = { Context.class, HsAdminPartnerRepository.class })
|
||||
@DirtiesContext
|
||||
class HsAdminPartnerRepositoryIntegrationTest extends ContextBasedTest {
|
||||
|
||||
@Autowired
|
||||
HsAdminPartnerRepository partnerRepository;
|
||||
|
||||
@Autowired
|
||||
EntityManager em;
|
||||
|
||||
@MockBean
|
||||
HttpServletRequest request;
|
||||
|
||||
@Nested
|
||||
class CreateCustomer {
|
||||
|
||||
@Test
|
||||
public void testHostsharingAdmin_withoutAssumedRole_canCreateNewCustomer() {
|
||||
// given
|
||||
context("mike@example.org", null);
|
||||
final var count = partnerRepository.count();
|
||||
|
||||
// when
|
||||
|
||||
final var result = attempt(em, () -> {
|
||||
return partnerRepository.save(testLtd);
|
||||
});
|
||||
|
||||
// then
|
||||
assertThat(result.wasSuccessful()).isTrue();
|
||||
assertThat(result.returnedValue()).isNotNull().extracting(HsAdminPartnerEntity::getUuid).isNotNull();
|
||||
assertThatPartnerIsPersisted(result.returnedValue());
|
||||
assertThat(partnerRepository.count()).isEqualTo(count + 1);
|
||||
}
|
||||
|
||||
private void assertThatPartnerIsPersisted(final HsAdminPartnerEntity saved) {
|
||||
final var found = partnerRepository.findByUuid(saved.getUuid());
|
||||
assertThat(found).isNotEmpty().get().usingRecursiveComparison().isEqualTo(saved);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class FindAllCustomers {
|
||||
|
||||
@Test
|
||||
public void testGlobalAdmin_withoutAssumedRole_canViewAllCustomers() {
|
||||
// given
|
||||
context("mike@example.org", null);
|
||||
|
||||
// when
|
||||
final var result = partnerRepository.findPartnerByOptionalNameLike(null);
|
||||
|
||||
// then
|
||||
allThesePartnersAreReturned(result, "Ixx AG", "Ypsilon GmbH", "Zett OHG");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
class FindByPrefixLike {
|
||||
|
||||
@Test
|
||||
public void testGlobalAdmin_withoutAssumedRole_canViewAllCustomers() {
|
||||
// given
|
||||
context("mike@example.org", null);
|
||||
|
||||
// when
|
||||
final var result = partnerRepository.findPartnerByOptionalNameLike("Yps");
|
||||
|
||||
// then
|
||||
exactlyTheseCustomersAreReturned(result, "Ypsilon GmbH");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customerAdmin_withoutAssumedRole_canViewOnlyItsOwnCustomer() {
|
||||
// given:
|
||||
context("customer-admin@xxx.example.com", null);
|
||||
|
||||
// when:
|
||||
final var result = partnerRepository.findPartnerByOptionalNameLike("Yps");
|
||||
|
||||
// then:
|
||||
exactlyTheseCustomersAreReturned(result);
|
||||
}
|
||||
}
|
||||
|
||||
void exactlyTheseCustomersAreReturned(final List<HsAdminPartnerEntity> actualResult, final String... partnerTradeNames) {
|
||||
assertThat(actualResult)
|
||||
.hasSize(partnerTradeNames.length)
|
||||
.extracting(HsAdminPartnerEntity::getPerson)
|
||||
.extracting(HsAdminPersonEntity::getTradeName)
|
||||
.containsExactlyInAnyOrder(partnerTradeNames);
|
||||
}
|
||||
|
||||
void allThesePartnersAreReturned(final List<HsAdminPartnerEntity> actualResult, final String... partnerTradeNames) {
|
||||
assertThat(actualResult)
|
||||
.extracting(HsAdminPartnerEntity::getPerson)
|
||||
.extracting(HsAdminPersonEntity::getTradeName)
|
||||
.contains(partnerTradeNames);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package net.hostsharing.hsadminng.hs.admin.partner;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.admin.contact.HsAdminContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.admin.partner.HsAdminPartnerEntity;
|
||||
import net.hostsharing.hsadminng.hs.admin.person.HsAdminPersonEntity;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.admin.person.HsAdminPersonEntity.PersonType.LEGAL;
|
||||
|
||||
public class TestHsAdminPartner {
|
||||
|
||||
public static final HsAdminPartnerEntity testLtd = hsAdminPartnerWithLegalPerson("Test Ltd.");
|
||||
|
||||
static public HsAdminPartnerEntity hsAdminPartnerWithLegalPerson(final String tradeName) {
|
||||
return HsAdminPartnerEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.person(HsAdminPersonEntity.builder()
|
||||
.type(LEGAL)
|
||||
.tradeName(tradeName)
|
||||
.build())
|
||||
.contact(HsAdminContactEntity.builder()
|
||||
.label(tradeName)
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.test.IsValidUuidMatcher.isValidUuid;
|
||||
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@ -63,7 +63,7 @@ class RbacUserControllerRestTest {
|
||||
|
||||
// then
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("uuid", isValidUuid()));
|
||||
.andExpect(jsonPath("uuid", isUuidValid()));
|
||||
|
||||
// then
|
||||
verify(rbacUserRepository).create(argThat(entity -> entity.getUuid() != null));
|
||||
|
@ -5,14 +5,15 @@ import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class IsValidUuidMatcher extends BaseMatcher<CharSequence> {
|
||||
|
||||
public static Matcher<CharSequence> isValidUuid() {
|
||||
public static Matcher<CharSequence> isUuidValid() {
|
||||
return new IsValidUuidMatcher();
|
||||
}
|
||||
|
||||
public static boolean isValidUuid(final String actual) {
|
||||
public static boolean isUuidValid(final String actual) {
|
||||
try {
|
||||
UUID.fromString(actual);
|
||||
} catch (final IllegalArgumentException exc) {
|
||||
@ -21,12 +22,16 @@ public class IsValidUuidMatcher extends BaseMatcher<CharSequence> {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Predicate<String> isValidUuid() {
|
||||
return IsValidUuidMatcher::isUuidValid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final Object actual) {
|
||||
if (actual == null || actual.getClass().isAssignableFrom(CharSequence.class)) {
|
||||
return false;
|
||||
}
|
||||
return isValidUuid(actual.toString());
|
||||
return isUuidValid(actual.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
56
src/test/java/net/hostsharing/test/JsonBuilder.java
Normal file
56
src/test/java/net/hostsharing/test/JsonBuilder.java
Normal file
@ -0,0 +1,56 @@
|
||||
package net.hostsharing.test;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Build JSONObject with little boilerplate code.
|
||||
*/
|
||||
public class JsonBuilder {
|
||||
|
||||
private final JSONObject jsonObject;
|
||||
|
||||
/**
|
||||
* Create a JsonBuilder from a string.
|
||||
*
|
||||
* @param jsonString valid JSON
|
||||
* @return a new JsonBuilder
|
||||
*/
|
||||
public static JsonBuilder jsonObject(final String jsonString) {
|
||||
return new JsonBuilder(jsonString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a property (key/value pair).
|
||||
*
|
||||
* @param key JSON key
|
||||
* @param value JSON value
|
||||
* @return this JsonBuilder
|
||||
*/
|
||||
public JsonBuilder with(final String key, final String value) {
|
||||
try {
|
||||
jsonObject.put(key, value);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return jsonObject.toString(4);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonBuilder(final String jsonString) {
|
||||
try {
|
||||
jsonObject = new JSONObject(jsonString);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user