Search in sources :

Example 6 with RepositoryGroup

use of org.apache.archiva.rest.api.v2.model.RepositoryGroup in project archiva by apache.

the class NativeRepositoryGroupServiceTest method testAddRepositoryToGroupIdempotency.

@Test
void testAddRepositoryToGroupIdempotency() {
    String token = getAdminToken();
    try {
        Map<String, Object> jsonAsMap = new HashMap<>();
        jsonAsMap.put("id", "group_001");
        jsonAsMap.put("name", "group_001");
        Response response = given().spec(getRequestSpec(token)).contentType(JSON).when().body(jsonAsMap).post("").prettyPeek().then().statusCode(201).extract().response();
        assertNotNull(response);
        RepositoryGroup result = response.getBody().jsonPath().getObject("", RepositoryGroup.class);
        assertNotNull(result);
        response = given().spec(getRequestSpec(token)).contentType(JSON).when().get("").then().statusCode(200).extract().response();
        assertNotNull(response);
        PagedResult resultList = response.getBody().jsonPath().getObject("", PagedResult.class);
        assertEquals(1, resultList.getPagination().getTotalCount());
        response = given().spec(getRequestSpec(token)).contentType(JSON).when().body(jsonAsMap).put("group_001/repositories/internal").prettyPeek().then().statusCode(200).extract().response();
        response = given().spec(getRequestSpec(token)).contentType(JSON).when().body(jsonAsMap).put("group_001/repositories/internal").prettyPeek().then().statusCode(200).extract().response();
        assertNotNull(response);
        result = response.getBody().jsonPath().getObject("", RepositoryGroup.class);
        assertNotNull(result);
        assertEquals(1, result.getRepositories().size());
        assertTrue(result.getRepositories().contains("internal"));
    } finally {
        given().spec(getRequestSpec(token)).contentType(JSON).when().delete("group_001").then().statusCode(200);
    }
}
Also used : Response(io.restassured.response.Response) RepositoryGroup(org.apache.archiva.rest.api.v2.model.RepositoryGroup) HashMap(java.util.HashMap) PagedResult(org.apache.archiva.components.rest.model.PagedResult) Test(org.junit.jupiter.api.Test)

Example 7 with RepositoryGroup

use of org.apache.archiva.rest.api.v2.model.RepositoryGroup in project archiva by apache.

the class DefaultRepositoryGroupService method getRepositoriesGroups.

@Override
public PagedResult<RepositoryGroup> getRepositoriesGroups(String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order) throws ArchivaRestServiceException {
    try {
        Predicate<org.apache.archiva.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter(searchTerm);
        Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator(orderBy, QUERY_HELPER.isAscending(order));
        int totalCount = Math.toIntExact(repositoryRegistry.getRepositoryGroups().stream().filter(filter).count());
        List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups().stream().filter(filter).sorted(ordering).skip(offset).limit(limit).map(RepositoryGroup::of).collect(Collectors.toList());
        return new PagedResult<>(totalCount, offset, limit, result);
    } catch (ArithmeticException e) {
        log.error("Could not convert total count: {}", e.getMessage());
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.INVALID_RESULT_SET_ERROR));
    }
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.rest.api.v2.model.RepositoryGroup) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) PagedResult(org.apache.archiva.components.rest.model.PagedResult)

Example 8 with RepositoryGroup

use of org.apache.archiva.rest.api.v2.model.RepositoryGroup in project archiva by apache.

the class DefaultRepositoryGroupService method addRepositoryGroup.

@Override
public RepositoryGroup addRepositoryGroup(RepositoryGroup repositoryGroup) throws ArchivaRestServiceException {
    final String groupId = repositoryGroup.getId();
    if (StringUtils.isEmpty(groupId)) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_INVALID_ID, groupId), 422);
    }
    if (repositoryRegistry.hasRepositoryGroup(groupId)) {
        httpServletResponse.setHeader("Location", uriInfo.getAbsolutePathBuilder().path(groupId).build().toString());
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_ID_EXISTS, groupId), 303);
    }
    try {
        RepositoryGroupConfiguration configuration = toConfig(repositoryGroup);
        CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate(configuration);
        if (validationResult.isValid()) {
            httpServletResponse.setStatus(201);
            return RepositoryGroup.of(validationResult.getRepository());
        } else {
            throw ValidationException.of(validationResult.getResult());
        }
    } catch (RepositoryException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_GROUP_ADD_FAILED));
    }
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.rest.api.v2.model.RepositoryGroup) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) RepositoryException(org.apache.archiva.repository.RepositoryException) ValidationError(org.apache.archiva.repository.validation.ValidationError) Map(java.util.Map)

Example 9 with RepositoryGroup

use of org.apache.archiva.rest.api.v2.model.RepositoryGroup in project archiva by apache.

the class DefaultRepositoryGroupService method deleteRepositoryFromGroup.

@Override
public RepositoryGroup deleteRepositoryFromGroup(final String repositoryGroupId, final String repositoryId) throws ArchivaRestServiceException {
    if (StringUtils.isEmpty(repositoryGroupId)) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId), 404);
    }
    if (StringUtils.isEmpty(repositoryId)) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId), 404);
    }
    try {
        org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
        if (repositoryGroup == null) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, ""), 404);
        }
        if (repositoryGroup.getRepositories().stream().noneMatch(r -> repositoryId.equals(r.getId()))) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId), 404);
        }
        if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
            log.error("This group instance is not editable: {}", repositoryGroupId);
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, ""), 500);
        }
        EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
        editableRepositoryGroup.removeRepository(repositoryId);
        org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
        return RepositoryGroup.of(newGroup);
    } catch (RepositoryException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage()), 500);
    }
}
Also used : ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) RepositoryException(org.apache.archiva.repository.RepositoryException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup)

Example 10 with RepositoryGroup

use of org.apache.archiva.rest.api.v2.model.RepositoryGroup in project archiva by apache.

the class NativeRepositoryGroupServiceTest method testRemoveRepositoryFromGroup404.

@Test
void testRemoveRepositoryFromGroup404() {
    String token = getAdminToken();
    try {
        Map<String, Object> jsonAsMap = new HashMap<>();
        jsonAsMap.put("id", "group_001");
        jsonAsMap.put("name", "group_001");
        jsonAsMap.put("repositories", Arrays.asList("internal"));
        Response response = given().spec(getRequestSpec(token)).contentType(JSON).when().body(jsonAsMap).post("").prettyPeek().then().statusCode(201).extract().response();
        assertNotNull(response);
        RepositoryGroup result = response.getBody().jsonPath().getObject("", RepositoryGroup.class);
        assertNotNull(result);
        response = given().spec(getRequestSpec(token)).contentType(JSON).when().get("").then().statusCode(200).extract().response();
        assertNotNull(response);
        PagedResult resultList = response.getBody().jsonPath().getObject("", PagedResult.class);
        assertEquals(1, resultList.getPagination().getTotalCount());
        assertNotNull(result.getRepositories());
        assertEquals(1, result.getRepositories().size());
        assertTrue(result.getRepositories().contains("internal"));
        response = given().spec(getRequestSpec(token)).contentType(JSON).when().body(jsonAsMap).delete("group_001/repositories/internalxx").prettyPeek().then().statusCode(404).extract().response();
        assertNotNull(response);
        ArchivaRestError error = response.getBody().jsonPath().getObject("", ArchivaRestError.class);
        assertNotNull(error);
    } finally {
        given().spec(getRequestSpec(token)).contentType(JSON).when().delete("group_001");
    }
}
Also used : Response(io.restassured.response.Response) RepositoryGroup(org.apache.archiva.rest.api.v2.model.RepositoryGroup) HashMap(java.util.HashMap) ArchivaRestError(org.apache.archiva.rest.api.v2.svc.ArchivaRestError) PagedResult(org.apache.archiva.components.rest.model.PagedResult) Test(org.junit.jupiter.api.Test)

Aggregations

RepositoryGroup (org.apache.archiva.rest.api.v2.model.RepositoryGroup)10 PagedResult (org.apache.archiva.components.rest.model.PagedResult)8 Response (io.restassured.response.Response)7 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 EditableRepositoryGroup (org.apache.archiva.repository.EditableRepositoryGroup)5 ArchivaRestServiceException (org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException)5 RepositoryException (org.apache.archiva.repository.RepositoryException)4 RepositoryGroupConfiguration (org.apache.archiva.configuration.model.RepositoryGroupConfiguration)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ValidationError (org.apache.archiva.repository.validation.ValidationError)2 MergeConfiguration (org.apache.archiva.rest.api.v2.model.MergeConfiguration)1 ArchivaRestError (org.apache.archiva.rest.api.v2.svc.ArchivaRestError)1