Search in sources :

Example 6 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiResourceTest method init.

@Before
public void init() {
    reset(apiService);
    mockApi = new ApiEntity();
    mockApi.setId(API);
    mockApi.setName(API);
    Proxy proxy = new Proxy();
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/test")));
    mockApi.setProxy(proxy);
    mockApi.setUpdatedAt(new Date());
    doReturn(mockApi).when(apiService).findById(API);
    doThrow(ApiNotFoundException.class).when(apiService).findById(UNKNOWN_API);
    updateApiEntity = new UpdateApiEntity();
    updateApiEntity.setDescription("toto");
    updateApiEntity.setVisibility(Visibility.PUBLIC);
    updateApiEntity.setName(API);
    updateApiEntity.setVersion("v1");
    proxy = new Proxy();
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/test")));
    updateApiEntity.setProxy(proxy);
    updateApiEntity.setLifecycleState(ApiLifecycleState.CREATED);
    doReturn(mockApi).when(apiService).update(eq(API), any(), eq(true));
}
Also used : Proxy(io.gravitee.definition.model.Proxy) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Date(java.util.Date) Before(org.junit.Before)

Example 7 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method duplicate.

@Override
public ApiEntity duplicate(final ApiEntity apiEntity, final DuplicateApiEntity duplicateApiEntity, String organizationId, String environmentId) {
    requireNonNull(apiEntity, "Missing ApiEntity");
    final String apiId = apiEntity.getId();
    LOGGER.debug("Duplicate API {}", apiId);
    final UpdateApiEntity newApiEntity = convert(apiEntity);
    final Proxy proxy = apiEntity.getProxy();
    proxy.setVirtualHosts(singletonList(new VirtualHost(duplicateApiEntity.getContextPath())));
    newApiEntity.setProxy(proxy);
    newApiEntity.setVersion(duplicateApiEntity.getVersion() == null ? apiEntity.getVersion() : duplicateApiEntity.getVersion());
    if (duplicateApiEntity.getFilteredFields().contains("groups")) {
        newApiEntity.setGroups(null);
    } else {
        newApiEntity.setGroups(apiEntity.getGroups());
    }
    final ApiEntity duplicatedApi = apiService.createWithApiDefinition(newApiEntity, getAuthenticatedUsername(), null);
    if (!duplicateApiEntity.getFilteredFields().contains("members")) {
        final Set<MembershipEntity> membershipsToDuplicate = membershipService.getMembershipsByReference(io.gravitee.rest.api.model.MembershipReferenceType.API, apiId);
        RoleEntity primaryOwnerRole = roleService.findPrimaryOwnerRoleByOrganization(organizationId, RoleScope.API);
        if (primaryOwnerRole != null) {
            String primaryOwnerRoleId = primaryOwnerRole.getId();
            membershipsToDuplicate.forEach(membership -> {
                String roleId = membership.getRoleId();
                if (!primaryOwnerRoleId.equals(roleId)) {
                    membershipService.addRoleToMemberOnReference(io.gravitee.rest.api.model.MembershipReferenceType.API, duplicatedApi.getId(), membership.getMemberType(), membership.getMemberId(), roleId);
                }
            });
        }
    }
    if (!duplicateApiEntity.getFilteredFields().contains("pages")) {
        final List<PageEntity> pages = pageService.search(new PageQuery.Builder().api(apiId).build(), true, environmentId);
        pageService.duplicatePages(pages, environmentId, duplicatedApi.getId());
    }
    if (!duplicateApiEntity.getFilteredFields().contains("plans")) {
        final Set<PlanEntity> plans = planService.findByApi(apiId);
        planService.duplicatePlans(plans, environmentId, duplicatedApi.getId());
    }
    return duplicatedApi;
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DuplicateApiEntity(io.gravitee.rest.api.model.api.DuplicateApiEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Proxy(io.gravitee.definition.model.Proxy) VirtualHost(io.gravitee.definition.model.VirtualHost)

Example 8 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method convertToEntity.

private UpdateApiEntity convertToEntity(String apiDefinition, JsonNode jsonNode) throws JsonProcessingException {
    final UpdateApiEntity importedApi = objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(apiDefinition, UpdateApiEntity.class);
    // Initialize with a default path
    if (Objects.equals(importedApi.getGraviteeDefinitionVersion(), DefinitionVersion.V1.getLabel()) && (importedApi.getPaths() == null || importedApi.getPaths().isEmpty())) {
        importedApi.setPaths(Collections.singletonMap("/", new ArrayList<>()));
    }
    // create group if not exist & replace groupName by groupId
    if (importedApi.getGroups() != null) {
        Set<String> groupNames = new HashSet<>(importedApi.getGroups());
        importedApi.getGroups().clear();
        for (String name : groupNames) {
            List<GroupEntity> groupEntities = groupService.findByName(name);
            GroupEntity group;
            if (groupEntities.isEmpty()) {
                NewGroupEntity newGroupEntity = new NewGroupEntity();
                newGroupEntity.setName(name);
                group = groupService.create(newGroupEntity);
            } else {
                group = groupEntities.get(0);
            }
            importedApi.getGroups().add(group.getId());
        }
    }
    // Views & Categories
    // Before 3.0.2, API 'categories' were called 'views'. This is for compatibility.
    final JsonNode viewsDefinition = jsonNode.path("views");
    if (viewsDefinition != null && viewsDefinition.isArray()) {
        Set<String> categories = new HashSet<>();
        for (JsonNode viewNode : viewsDefinition) {
            categories.add(viewNode.asText());
        }
        importedApi.setCategories(categories);
    }
    return importedApi;
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 9 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiService_Update_DefaultLoggingMaxDurationTest method setUp.

@Before
public void setUp() throws TechnicalException {
    PropertyFilter apiMembershipTypeFilter = new ApiPermissionFilter();
    objectMapper.setFilterProvider(new SimpleFilterProvider(Collections.singletonMap("apiMembershipTypeFilter", apiMembershipTypeFilter)));
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    when(apiRepository.update(any())).thenReturn(api);
    when(api.getId()).thenReturn(API_ID);
    when(api.getName()).thenReturn(API_NAME);
    when(api.getApiLifecycleState()).thenReturn(ApiLifecycleState.CREATED);
    existingApi = new UpdateApiEntity();
    existingApi.setName(API_NAME);
    existingApi.setVersion("v1");
    existingApi.setDescription("Ma description");
    existingApi.setLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState.CREATED);
    final Proxy proxy = new Proxy();
    EndpointGroup endpointGroup = new EndpointGroup();
    endpointGroup.setName("endpointGroupName");
    Endpoint endpoint = new HttpEndpoint("endpointName", null);
    endpointGroup.setEndpoints(singleton(endpoint));
    proxy.setGroups(singleton(endpointGroup));
    existingApi.setProxy(proxy);
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/context")));
    RoleEntity poRoleEntity = new RoleEntity();
    poRoleEntity.setName(SystemRole.PRIMARY_OWNER.name());
    poRoleEntity.setScope(RoleScope.API);
    when(roleService.findPrimaryOwnerRoleByOrganization("DEFAULT", RoleScope.API)).thenReturn(poRoleEntity);
    MemberEntity po = new MemberEntity();
    po.setId(USER_NAME);
    po.setReferenceId(API_ID);
    po.setReferenceType(io.gravitee.rest.api.model.MembershipReferenceType.API);
    po.setRoles(Collections.singletonList(poRoleEntity));
    when(membershipService.getMembersByReferencesAndRole(any(), any(), any())).thenReturn(singleton(po));
    mockStatic(System.class);
    when(System.currentTimeMillis()).thenReturn(0L);
    final SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(mock(Authentication.class));
    SecurityContextHolder.setContext(securityContext);
    when(notificationTemplateService.resolveInlineTemplateWithParam(anyString(), any(Reader.class), any())).thenReturn("toDecode=decoded-value");
    when(parameterService.find(Key.API_PRIMARY_OWNER_MODE, ParameterReferenceType.ENVIRONMENT)).thenReturn("USER");
    MembershipEntity primaryOwner = new MembershipEntity();
    primaryOwner.setMemberType(MembershipMemberType.USER);
    when(membershipService.getPrimaryOwner(eq(MembershipReferenceType.API), any())).thenReturn(primaryOwner);
    reset(searchEngineService);
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) Reader(java.io.Reader) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) PropertyFilter(com.fasterxml.jackson.databind.ser.PropertyFilter) ApiPermissionFilter(io.gravitee.rest.api.service.jackson.filter.ApiPermissionFilter) Before(org.junit.Before)

Aggregations

UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)9 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)6 UuidString (io.gravitee.rest.api.service.common.UuidString)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DuplicateApiEntity (io.gravitee.rest.api.model.api.DuplicateApiEntity)3 Proxy (io.gravitee.definition.model.Proxy)2 VirtualHost (io.gravitee.definition.model.VirtualHost)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 Before (org.junit.Before)2 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 MediaType (io.gravitee.common.http.MediaType)1 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Plan (io.gravitee.repository.management.model.Plan)1 PlanSecurityParam (io.gravitee.rest.api.management.rest.resource.param.PlanSecurityParam)1 PlanStatusParam (io.gravitee.rest.api.management.rest.resource.param.PlanStatusParam)1 Permission (io.gravitee.rest.api.management.rest.security.Permission)1