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));
}
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;
}
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;
}
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);
}
Aggregations