dependency API
This commit is contained in:
parent
ad374677ee
commit
aa3e5339db
4 changed files with 65 additions and 10 deletions
|
@ -48,6 +48,8 @@ public final class API {
|
|||
|
||||
public static final String INACTIVE_PATH_SEGMENT = "/inactive";
|
||||
|
||||
public static final String DEPENDENCY_PATH_SEGMENT = "/dependency";
|
||||
|
||||
public static final String PATH_VAR_ACTIVE = MODEL_ID_VAR_PATH_SEGMENT + ACTIVE_PATH_SEGMENT;
|
||||
public static final String PATH_VAR_INACTIVE = MODEL_ID_VAR_PATH_SEGMENT + INACTIVE_PATH_SEGMENT;
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -52,7 +53,7 @@ public final class BulkAction {
|
|||
this.type = type;
|
||||
this.sourceType = sourceType;
|
||||
this.sources = Utils.immutableSetOf(sources);
|
||||
this.dependencies = new HashSet<>();
|
||||
this.dependencies = new LinkedHashSet<>();
|
||||
this.result = new HashSet<>();
|
||||
|
||||
check();
|
||||
|
@ -66,14 +67,8 @@ public final class BulkAction {
|
|||
this(type, sourceType, (sources != null) ? Arrays.asList(sources) : Collections.emptyList());
|
||||
}
|
||||
|
||||
private void check() {
|
||||
for (final EntityKey source : this.sources) {
|
||||
if (source.entityType != this.sourceType) {
|
||||
throw new IllegalArgumentException(
|
||||
"At least one EntityType in sources list has not the expected EntityType");
|
||||
}
|
||||
}
|
||||
|
||||
public Set<EntityKey> getDependencies() {
|
||||
return Collections.unmodifiableSet(this.dependencies);
|
||||
}
|
||||
|
||||
public Set<EntityKey> extractKeys(final EntityType type) {
|
||||
|
@ -96,4 +91,14 @@ public final class BulkAction {
|
|||
return "BulkAction [type=" + this.type + ", sourceType=" + this.sourceType + ", sources=" + this.sources + "]";
|
||||
}
|
||||
|
||||
private void check() {
|
||||
for (final EntityKey source : this.sources) {
|
||||
if (source.entityType != this.sourceType) {
|
||||
throw new IllegalArgumentException(
|
||||
"At least one EntityType in sources list has not the expected EntityType");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -139,12 +139,38 @@ public abstract class EntityController<T extends GrantEntity, M extends GrantEnt
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// ******************
|
||||
// * GET (dependency)
|
||||
// ******************
|
||||
|
||||
@RequestMapping(
|
||||
path = API.MODEL_ID_VAR_PATH_SEGMENT + API.DEPENDENCY_PATH_SEGMENT,
|
||||
method = RequestMethod.GET,
|
||||
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Collection<EntityKey> getDependencies(
|
||||
@PathVariable final String modelId,
|
||||
@RequestParam final BulkAction.Type type) {
|
||||
|
||||
this.entityDAO
|
||||
.byModelId(modelId)
|
||||
.flatMap(this.authorization::checkReadonly);
|
||||
|
||||
final BulkAction bulkAction = new BulkAction(
|
||||
type,
|
||||
this.entityDAO.entityType(),
|
||||
Arrays.asList(new EntityKey(modelId, this.entityDAO.entityType())));
|
||||
|
||||
this.bulkActionService.collectDependencies(bulkAction);
|
||||
return bulkAction.getDependencies();
|
||||
}
|
||||
|
||||
// ******************
|
||||
// * GET (single)
|
||||
// ******************
|
||||
|
||||
@RequestMapping(
|
||||
path = "/{modelId}",
|
||||
path = API.MODEL_ID_VAR_PATH_SEGMENT,
|
||||
method = RequestMethod.GET,
|
||||
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
|
|
|
@ -25,10 +25,13 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|||
|
||||
import ch.ethz.seb.sebserver.gbl.api.API;
|
||||
import ch.ethz.seb.sebserver.gbl.api.APIMessage;
|
||||
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
|
||||
import ch.ethz.seb.sebserver.gbl.model.EntityName;
|
||||
import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport;
|
||||
import ch.ethz.seb.sebserver.gbl.model.EntityType;
|
||||
import ch.ethz.seb.sebserver.gbl.model.Page;
|
||||
import ch.ethz.seb.sebserver.gbl.model.institution.Institution;
|
||||
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction;
|
||||
|
||||
@Sql(scripts = { "classpath:schema-test.sql", "classpath:data-test.sql" })
|
||||
public class InstitutionAPITest extends AdministrationAPIIntegrationTester {
|
||||
|
@ -401,6 +404,25 @@ public class InstitutionAPITest extends AdministrationAPIIntegrationTester {
|
|||
assertEquals("[]", getOrderedUUIDs(institutions.content));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependency() throws Exception {
|
||||
final Collection<EntityKey> dependencies = new RestAPITestHelper()
|
||||
.withAccessToken(getSebAdminAccess())
|
||||
.withPath(API.INSTITUTION_ENDPOINT)
|
||||
.withPath("1")
|
||||
.withPath(API.DEPENDENCY_PATH_SEGMENT)
|
||||
.withAttribute("type", BulkAction.Type.DEACTIVATE.name())
|
||||
.withExpectedStatus(HttpStatus.OK)
|
||||
.getAsObject(new TypeReference<Collection<EntityKey>>() {
|
||||
});
|
||||
|
||||
assertNotNull(dependencies);
|
||||
assertTrue(dependencies.size() == 3);
|
||||
assertTrue(dependencies.contains(new EntityKey("1", EntityType.USER)));
|
||||
assertTrue(dependencies.contains(new EntityKey("2", EntityType.USER)));
|
||||
assertTrue(dependencies.contains(new EntityKey("5", EntityType.USER)));
|
||||
}
|
||||
|
||||
static void assertContainsInstitution(final String name, final Collection<Institution> institutions) {
|
||||
assert institutions != null;
|
||||
assert institutions.stream()
|
||||
|
|
Loading…
Add table
Reference in a new issue