migrate hs-api to test-api

This commit is contained in:
Michael Hoennig 2022-08-31 15:44:06 +02:00
parent a117258085
commit 8731f4a7b2
17 changed files with 99 additions and 100 deletions

View File

@ -66,18 +66,18 @@ If you have at least Docker, the Java JDK and Gradle installed in appropriate ve
# the following command should return a JSON array with just all customers:
curl \
-H 'current-user: mike@example.org' \
http://localhost:8080/api/customers
http://localhost:8080/api/test-customers
# the following command should return a JSON array with just all packages visible for the admin of the customer yyy:
curl \
-H 'current-user: mike@example.org' -H 'assumed-roles: test_customer#yyy.admin' \
http://localhost:8080/api/packages
http://localhost:8080/api/test-packages
# add a new customer
curl \
-H 'current-user: mike@example.org' -H "Content-Type: application/json" \
-d '{ "prefix":"ttt", "reference":80001, "adminUserName":"admin@ttt.example.com" }' \
-X POST http://localhost:8080/api/customers
-X POST http://localhost:8080/api/test-customers
If you wonder who 'mike@example.org' and 'sven@example.org' are and where the data comes from:
Mike and Sven are just example global admin accounts as part of the example data which is automatically inserted in Testcontainers and Development environments.

View File

@ -1,8 +1,8 @@
package net.hostsharing.hsadminng.test.cust;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.generated.api.v1.api.CustomersApi;
import net.hostsharing.hsadminng.generated.api.v1.model.CustomerResource;
import net.hostsharing.hsadminng.generated.api.v1.api.TestCustomersApi;
import net.hostsharing.hsadminng.generated.api.v1.model.TestCustomerResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
@ -17,7 +17,7 @@ import static net.hostsharing.hsadminng.Mapper.mapList;
@RestController
public class TestCustomerController implements CustomersApi {
public class TestCustomerController implements TestCustomersApi {
@Autowired
private Context context;
@ -27,7 +27,7 @@ public class TestCustomerController implements CustomersApi {
@Override
@Transactional(readOnly = true)
public ResponseEntity<List<CustomerResource>> listCustomers(
public ResponseEntity<List<TestCustomerResource>> listCustomers(
String currentUser,
String assumedRoles,
String prefix
@ -36,15 +36,15 @@ public class TestCustomerController implements CustomersApi {
final var result = testCustomerRepository.findCustomerByOptionalPrefixLike(prefix);
return ResponseEntity.ok(mapList(result, CustomerResource.class));
return ResponseEntity.ok(mapList(result, TestCustomerResource.class));
}
@Override
@Transactional
public ResponseEntity<CustomerResource> addCustomer(
public ResponseEntity<TestCustomerResource> addCustomer(
final String currentUser,
final String assumedRoles,
final CustomerResource customer) {
final TestCustomerResource customer) {
context.define(currentUser, assumedRoles);
@ -56,10 +56,10 @@ public class TestCustomerController implements CustomersApi {
final var uri =
MvcUriComponentsBuilder.fromController(getClass())
.path("/api/customers/{id}")
.path("/api/test-customers/{id}")
.buildAndExpand(customer.getUuid())
.toUri();
return ResponseEntity.created(uri).body(map(saved, CustomerResource.class));
return ResponseEntity.created(uri).body(map(saved, TestCustomerResource.class));
}
}

View File

@ -1,17 +0,0 @@
package net.hostsharing.hsadminng.test.pac;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import java.util.List;
import java.util.UUID;
public interface PackageRepository extends Repository<PackageEntity, UUID> {
@Query("SELECT p FROM PackageEntity p WHERE :name is null or p.name like concat(:name, '%')")
List<PackageEntity> findAllByOptionalNameLike(final String name);
PackageEntity findByUuid(UUID packageUuid);
PackageEntity save(PackageEntity current);
}

View File

@ -2,9 +2,9 @@ package net.hostsharing.hsadminng.test.pac;
import net.hostsharing.hsadminng.OptionalFromJson;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.generated.api.v1.api.PackagesApi;
import net.hostsharing.hsadminng.generated.api.v1.model.PackageResource;
import net.hostsharing.hsadminng.generated.api.v1.model.PackageUpdateResource;
import net.hostsharing.hsadminng.generated.api.v1.api.TestPackagesApi;
import net.hostsharing.hsadminng.generated.api.v1.model.TestPackageResource;
import net.hostsharing.hsadminng.generated.api.v1.model.TestPackageUpdateResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
@ -17,41 +17,41 @@ import static net.hostsharing.hsadminng.Mapper.map;
import static net.hostsharing.hsadminng.Mapper.mapList;
@RestController
public class PackageController implements PackagesApi {
public class TestPackageController implements TestPackagesApi {
@Autowired
private Context context;
@Autowired
private PackageRepository packageRepository;
private TestPackageRepository testPackageRepository;
@Override
@Transactional(readOnly = true)
public ResponseEntity<List<PackageResource>> listPackages(
public ResponseEntity<List<TestPackageResource>> listPackages(
String currentUser,
String assumedRoles,
String name
) {
context.define(currentUser, assumedRoles);
final var result = packageRepository.findAllByOptionalNameLike(name);
return ResponseEntity.ok(mapList(result, PackageResource.class));
final var result = testPackageRepository.findAllByOptionalNameLike(name);
return ResponseEntity.ok(mapList(result, TestPackageResource.class));
}
@Override
@Transactional
public ResponseEntity<PackageResource> updatePackage(
public ResponseEntity<TestPackageResource> updatePackage(
final String currentUser,
final String assumedRoles,
final UUID packageUuid,
final PackageUpdateResource body) {
final TestPackageUpdateResource body) {
context.define(currentUser, assumedRoles);
final var current = packageRepository.findByUuid(packageUuid);
final var current = testPackageRepository.findByUuid(packageUuid);
OptionalFromJson.of(body.getDescription()).ifPresent(current::setDescription);
final var saved = packageRepository.save(current);
final var mapped = map(saved, PackageResource.class);
final var saved = testPackageRepository.save(current);
final var mapped = map(saved, TestPackageResource.class);
return ResponseEntity.ok(mapped);
}
}

View File

@ -15,7 +15,7 @@ import java.util.UUID;
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PackageEntity {
public class TestPackageEntity {
private @Id UUID uuid;

View File

@ -0,0 +1,17 @@
package net.hostsharing.hsadminng.test.pac;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import java.util.List;
import java.util.UUID;
public interface TestPackageRepository extends Repository<TestPackageEntity, UUID> {
@Query("SELECT p FROM TestPackageEntity p WHERE :name is null or p.name like concat(:name, '%')")
List<TestPackageEntity> findAllByOptionalNameLike(final String name);
TestPackageEntity findByUuid(UUID packageUuid);
TestPackageEntity save(TestPackageEntity current);
}

View File

@ -30,14 +30,14 @@ paths:
# HS
/api/customers:
$ref: "./api-definition/hs-customers.yaml"
/api/test-customers:
$ref: "./api-definition/test-customers.yaml"
/api/packages:
$ref: "./api-definition/hs-packages.yaml"
/api/test-packages:
$ref: "./api-definition/test-packages.yaml"
/api/packages/{packageUUID}:
$ref: "./api-definition/hs-packages-uuid.yaml"
/api/test-packages/{packageUUID}:
$ref: "./api-definition/test-packages-uuid.yaml"
# Other

View File

@ -3,7 +3,7 @@ components:
schemas:
Customer:
TestCustomer:
type: object
properties:
uuid:

View File

@ -2,7 +2,7 @@ get:
summary: Returns a list of (optionally filtered) customers.
description: Returns the list of (optionally filtered) customers which are visible to the current user or any of it's assumed roles.
tags:
- customers
- testCustomers
operationId: listCustomers
parameters:
- $ref: './api-definition/auth.yaml#/components/parameters/currentUser'
@ -21,7 +21,7 @@ get:
schema:
type: array
items:
$ref: './api-definition/hs-customer-schemas.yaml#/components/schemas/Customer'
$ref: './api-definition/test-customer-schemas.yaml#/components/schemas/TestCustomer'
"401":
$ref: './api-definition/error-responses.yaml#/components/responses/Unauthorized'
"403":
@ -30,7 +30,7 @@ get:
post:
summary: Adds a new customer.
tags:
- customers
- testCustomers
operationId: addCustomer
parameters:
- $ref: './api-definition/auth.yaml#/components/parameters/currentUser'
@ -39,7 +39,7 @@ post:
content:
'application/json':
schema:
$ref: './api-definition/api-definition/hs-customer-schemas.yaml#/components/schemas/Customer'
$ref: './api-definition/api-definition/test-customer-schemas.yaml#/components/schemas/TestCustomer'
required: true
responses:
"201":
@ -47,7 +47,7 @@ post:
content:
'application/json':
schema:
$ref: './api-definition/hs-customer-schemas.yaml#/components/schemas/Customer'
$ref: './api-definition/test-customer-schemas.yaml#/components/schemas/TestCustomer'
"401":
$ref: './api-definition/error-responses.yaml#/components/responses/Unauthorized'
"403":

View File

@ -3,20 +3,20 @@ components:
schemas:
Package:
TestPackage:
type: object
properties:
uuid:
type: string
format: uuid
customer:
$ref: './api-definition/hs-customer-schemas.yaml#/components/schemas/Customer'
$ref: './api-definition/test-customer-schemas.yaml#/components/schemas/TestCustomer'
name:
type: string
description:
type: string
maxLength: 80
PackageUpdate:
TestPackageUpdate:
type: object
properties:
description:

View File

@ -1,6 +1,6 @@
patch:
tags:
- packages
- testPackages
operationId: updatePackage
parameters:
- $ref: './api-definition/auth.yaml#/components/parameters/currentUser'
@ -15,14 +15,14 @@ patch:
content:
'application/json':
schema:
$ref: './api-definition/hs-package-schemas.yaml#/components/schemas/PackageUpdate'
$ref: './api-definition/test-package-schemas.yaml#/components/schemas/TestPackageUpdate'
responses:
"200":
description: OK
content:
'application/json':
schema:
$ref: './api-definition/hs-package-schemas.yaml#/components/schemas/Package'
$ref: './api-definition/test-package-schemas.yaml#/components/schemas/TestPackage'
"401":
$ref: './api-definition/error-responses.yaml#/components/responses/Unauthorized'
"403":

View File

@ -1,6 +1,6 @@
get:
tags:
- packages
- testPackages
operationId: listPackages
parameters:
- $ref: './api-definition/auth.yaml#/components/parameters/currentUser'
@ -18,7 +18,7 @@ get:
schema:
type: array
items:
$ref: './api-definition/hs-package-schemas.yaml#/components/schemas/Package'
$ref: './api-definition/test-package-schemas.yaml#/components/schemas/TestPackage'
"401":
$ref: './api-definition/error-responses.yaml#/components/responses/Unauthorized'
"403":

View File

@ -7,11 +7,10 @@ options:
map:
result: org.springframework.http.ResponseEntity
types:
- type: array => java.util.List
- type: string:uuid => java.util.UUID
paths:
/api/packages/{packageUUID}:
/api/test-packages/{packageUUID}:
null: org.openapitools.jackson.nullable.JsonNullable

View File

@ -45,7 +45,7 @@ class TestCustomerControllerAcceptanceTest {
.header("current-user", "mike@example.org")
.port(port)
.when()
.get("http://localhost/api/customers")
.get("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -63,7 +63,7 @@ class TestCustomerControllerAcceptanceTest {
.header("current-user", "mike@example.org")
.port(port)
.when()
.get("http://localhost/api/customers?prefix=y")
.get("http://localhost/api/test-customers?prefix=y")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -80,7 +80,7 @@ class TestCustomerControllerAcceptanceTest {
.header("assumed-roles", "test_customer#yyy.admin")
.port(port)
.when()
.get("http://localhost/api/customers")
.get("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -96,7 +96,7 @@ class TestCustomerControllerAcceptanceTest {
.header("current-user", "customer-admin@yyy.example.com")
.port(port)
.when()
.get("http://localhost/api/customers")
.get("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -125,7 +125,7 @@ class TestCustomerControllerAcceptanceTest {
""")
.port(port)
.when()
.post("http://localhost/api/customers")
.post("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(201)
.contentType(ContentType.JSON)
@ -160,7 +160,7 @@ class TestCustomerControllerAcceptanceTest {
""".formatted(givenUuid))
.port(port)
.when()
.post("http://localhost/api/customers")
.post("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(201)
.contentType(ContentType.JSON)
@ -196,7 +196,7 @@ class TestCustomerControllerAcceptanceTest {
""")
.port(port)
.when()
.post("http://localhost/api/customers")
.post("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(403)
.contentType(ContentType.JSON)
@ -225,7 +225,7 @@ class TestCustomerControllerAcceptanceTest {
""")
.port(port)
.when()
.post("http://localhost/api/customers")
.post("http://localhost/api/test-customers")
.then().assertThat()
.statusCode(403)
.contentType(ContentType.JSON)

View File

@ -7,11 +7,11 @@ import static java.util.UUID.randomUUID;
public class TestPackage {
public static final PackageEntity xxx00 = hsPackage(TestCustomer.xxx, "xxx00");
public static final PackageEntity xxx01 = hsPackage(TestCustomer.xxx, "xxx01");
public static final PackageEntity xxx02 = hsPackage(TestCustomer.xxx, "xxx02");
public static final TestPackageEntity xxx00 = hsPackage(TestCustomer.xxx, "xxx00");
public static final TestPackageEntity xxx01 = hsPackage(TestCustomer.xxx, "xxx01");
public static final TestPackageEntity xxx02 = hsPackage(TestCustomer.xxx, "xxx02");
public static PackageEntity hsPackage(final TestCustomerEntity customer, final String name) {
return new PackageEntity(randomUUID(), 0, customer, name, "initial description of package " + name);
public static TestPackageEntity hsPackage(final TestCustomerEntity customer, final String name) {
return new TestPackageEntity(randomUUID(), 0, customer, name, "initial description of package " + name);
}
}

View File

@ -25,7 +25,7 @@ import static org.hamcrest.Matchers.is;
classes = HsadminNgApplication.class
)
@Transactional
class PackageControllerAcceptanceTest {
class TestPackageControllerAcceptanceTest {
@LocalServerPort
Integer port;
@ -33,7 +33,7 @@ class PackageControllerAcceptanceTest {
Context context;
@Autowired
PackageRepository packageRepository;
TestPackageRepository testPackageRepository;
@Nested
class ListPackages {
@ -47,7 +47,7 @@ class PackageControllerAcceptanceTest {
.header("assumed-roles", "test_customer#xxx.admin")
.port(port)
.when()
.get("http://localhost/api/packages")
.get("http://localhost/api/test-packages")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -69,7 +69,7 @@ class PackageControllerAcceptanceTest {
.header("assumed-roles", "test_customer#xxx.admin")
.port(port)
.when()
.get("http://localhost/api/packages?name=xxx01")
.get("http://localhost/api/test-packages?name=xxx01")
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -103,7 +103,7 @@ class PackageControllerAcceptanceTest {
""", randomDescription))
.port(port)
.when()
.patch("http://localhost/api/packages/{uuidOfPackage}", getUuidOfPackage("xxx00"))
.patch("http://localhost/api/test-packages/{uuidOfPackage}", getUuidOfPackage("xxx00"))
.then()
.assertThat()
.statusCode(200)
@ -133,7 +133,7 @@ class PackageControllerAcceptanceTest {
""")
.port(port)
.when()
.patch("http://localhost/api/packages/{uuidOfPackage}", getUuidOfPackage("xxx01"))
.patch("http://localhost/api/test-packages/{uuidOfPackage}", getUuidOfPackage("xxx01"))
.then()
.assertThat()
.statusCode(200)
@ -158,7 +158,7 @@ class PackageControllerAcceptanceTest {
.body("{}")
.port(port)
.when()
.patch("http://localhost/api/packages/{uuidOfPackage}", getUuidOfPackage("xxx02"))
.patch("http://localhost/api/test-packages/{uuidOfPackage}", getUuidOfPackage("xxx02"))
.then().assertThat()
.statusCode(200)
.contentType("application/json")
@ -176,7 +176,7 @@ class PackageControllerAcceptanceTest {
.header("assumed-roles", "test_customer#xxx.admin")
.port(port)
.when()
.get("http://localhost/api/packages?name={packageName}", packageName)
.get("http://localhost/api/test-packages?name={packageName}", packageName)
.then()
.statusCode(200)
.contentType("application/json")
@ -186,6 +186,6 @@ class PackageControllerAcceptanceTest {
String getDescriptionOfPackage(final String packageName) {
context.define("mike@example.org","test_customer#xxx.admin");
return packageRepository.findAllByOptionalNameLike(packageName).get(0).getDescription();
return testPackageRepository.findAllByOptionalNameLike(packageName).get(0).getDescription();
}
}

View File

@ -21,13 +21,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, TestCustomerRepository.class, JpaAttempt.class })
@DirtiesContext
class PackageRepositoryIntegrationTest {
class TestPackageRepositoryIntegrationTest {
@Autowired
Context context;
@Autowired
PackageRepository packageRepository;
TestPackageRepository testPackageRepository;
@Autowired
EntityManager em;
@ -47,7 +47,7 @@ class PackageRepositoryIntegrationTest {
context.define("mike@example.org");
// when
final var result = packageRepository.findAllByOptionalNameLike(null);
final var result = testPackageRepository.findAllByOptionalNameLike(null);
// then
noPackagesAreReturned(result);
@ -59,7 +59,7 @@ class PackageRepositoryIntegrationTest {
context.define("mike@example.org", "global#test-global.admin");
// when
final var result = packageRepository.findAllByOptionalNameLike(null);
final var result = testPackageRepository.findAllByOptionalNameLike(null);
then:
noPackagesAreReturned(result);
@ -71,7 +71,7 @@ class PackageRepositoryIntegrationTest {
context.define("customer-admin@xxx.example.com");
// when:
final var result = packageRepository.findAllByOptionalNameLike(null);
final var result = testPackageRepository.findAllByOptionalNameLike(null);
// then:
exactlyThesePackagesAreReturned(result, "xxx00", "xxx01", "xxx02");
@ -81,7 +81,7 @@ class PackageRepositoryIntegrationTest {
public void customerAdmin_withAssumedOwnedPackageAdminRole_canViewOnlyItsOwnPackages() {
context.define("customer-admin@xxx.example.com", "test_package#xxx00.admin");
final var result = packageRepository.findAllByOptionalNameLike(null);
final var result = testPackageRepository.findAllByOptionalNameLike(null);
exactlyThesePackagesAreReturned(result, "xxx00");
}
@ -94,18 +94,18 @@ class PackageRepositoryIntegrationTest {
public void supportsOptimisticLocking() throws InterruptedException {
// given
testGlobalAdminWithAssumedRole("test_package#xxx00.admin");
final var pac = packageRepository.findAllByOptionalNameLike("%").get(0);
final var pac = testPackageRepository.findAllByOptionalNameLike("%").get(0);
// when
final var result1 = jpaAttempt.transacted(() -> {
testGlobalAdminWithAssumedRole("test_package#xxx00.admin");
pac.setDescription("description set by thread 1");
packageRepository.save(pac);
testPackageRepository.save(pac);
});
final var result2 = jpaAttempt.transacted(() -> {
testGlobalAdminWithAssumedRole("test_package#xxx00.admin");
pac.setDescription("description set by thread 2");
packageRepository.save(pac);
testPackageRepository.save(pac);
sleep(1500);
});
@ -129,15 +129,15 @@ class PackageRepositoryIntegrationTest {
context.define("mike@example.org", assumedRoles);
}
void noPackagesAreReturned(final List<PackageEntity> actualResult) {
void noPackagesAreReturned(final List<TestPackageEntity> actualResult) {
assertThat(actualResult)
.extracting(PackageEntity::getName)
.extracting(TestPackageEntity::getName)
.isEmpty();
}
void exactlyThesePackagesAreReturned(final List<PackageEntity> actualResult, final String... packageNames) {
void exactlyThesePackagesAreReturned(final List<TestPackageEntity> actualResult, final String... packageNames) {
assertThat(actualResult)
.extracting(PackageEntity::getName)
.extracting(TestPackageEntity::getName)
.containsExactlyInAnyOrder(packageNames);
}