use of io.gravitee.rest.api.portal.rest.model.Api in project gravitee-management-rest-api by gravitee-io.
the class ApisResourceNotAuthenticatedTest method init.
@Before
public void init() {
resetAllMocks();
ApiEntity publishedApi = new ApiEntity();
publishedApi.setLifecycleState(ApiLifecycleState.PUBLISHED);
publishedApi.setName("A");
publishedApi.setId("A");
ApiEntity unpublishedApi = new ApiEntity();
unpublishedApi.setLifecycleState(ApiLifecycleState.UNPUBLISHED);
unpublishedApi.setName("B");
unpublishedApi.setId("B");
ApiEntity anotherPublishedApi = new ApiEntity();
anotherPublishedApi.setLifecycleState(ApiLifecycleState.PUBLISHED);
anotherPublishedApi.setName("C");
anotherPublishedApi.setId("C");
Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(publishedApi, anotherPublishedApi));
doReturn(mockApis).when(apiService).findPublishedByUser(isNull(), any(ApiQuery.class));
doReturn(new FilteredEntities<ApiEntity>(new ArrayList<>(mockApis), null)).when(filteringService).filterApis(any(), any(), any());
doReturn(false).when(ratingService).isEnabled();
doReturn(new Api().name("A").id("A")).when(apiMapper).convert(publishedApi);
doReturn(new Api().name("B").id("B")).when(apiMapper).convert(unpublishedApi);
doReturn(new Api().name("C").id("C")).when(apiMapper).convert(anotherPublishedApi);
}
use of io.gravitee.rest.api.portal.rest.model.Api in project gravitee-management-rest-api by gravitee-io.
the class ApiMapperTest method testMinimalConvert.
@Test
public void testMinimalConvert() {
// init
apiEntity = new ApiEntity();
apiEntity.setId(API_ID);
apiEntity.setDescription(API_DESCRIPTION);
Proxy proxy = new Proxy();
proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/foo")));
apiEntity.setProxy(proxy);
doReturn(false).when(ratingService).isEnabled();
apiEntity.setCategories(new HashSet<>(Arrays.asList(API_CATEGORY, API_CATEGORY_HIDDEN)));
doReturn(false).when(parameterService).findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
apiEntity.setLifecycleState(ApiLifecycleState.CREATED);
// Test
Api responseApi = apiMapper.convert(apiEntity);
assertNotNull(responseApi);
assertNull(responseApi.getPages());
assertNull(responseApi.getPlans());
assertEquals(API_DESCRIPTION, responseApi.getDescription());
assertEquals(API_ID, responseApi.getId());
assertNull(responseApi.getName());
assertNull(responseApi.getVersion());
assertTrue(responseApi.getDraft());
List<String> entrypoints = responseApi.getEntrypoints();
assertNull(entrypoints);
List<String> labels = responseApi.getLabels();
assertNotNull(labels);
assertEquals(0, labels.size());
assertNull(responseApi.getOwner());
List<String> categories = responseApi.getCategories();
assertNotNull(categories);
assertEquals(0, categories.size());
RatingSummary ratingSummary = responseApi.getRatingSummary();
assertNull(ratingSummary);
}
use of io.gravitee.rest.api.portal.rest.model.Api in project gravitee-management-rest-api by gravitee-io.
the class ApiMapperTest method testConvert.
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
// init
apiEntity = new ApiEntity();
apiEntity.setId(API_ID);
apiEntity.setDescription(API_DESCRIPTION);
apiEntity.setName(API_NAME);
apiEntity.setLabels(new ArrayList<>(Arrays.asList(API_LABEL)));
doThrow(CategoryNotFoundException.class).when(categoryService).findNotHiddenById(API_CATEGORY_HIDDEN);
apiEntity.setCategories(new HashSet<>(Arrays.asList(API_CATEGORY, API_CATEGORY_HIDDEN)));
apiEntity.setEntrypoints(Arrays.asList(new ApiEntrypointEntity(API_ENTRYPOINT_1), new ApiEntrypointEntity(API + "/foo")));
Map<String, Object> metadata = new HashMap<>();
metadata.put("meta", API);
apiEntity.setMetadata(metadata);
apiEntity.setVersion(API_VERSION);
UserEntity ownerEntity = new UserEntity();
ownerEntity.setId(API_OWNER_ID);
ownerEntity.setEmail(API_OWNER_EMAIL);
ownerEntity.setFirstname(API_OWNER_FIRSTNAME);
ownerEntity.setLastname(API_OWNER_LASTNAME);
apiEntity.setPrimaryOwner(new PrimaryOwnerEntity(ownerEntity));
RatingSummaryEntity ratingSummaryEntity = new RatingSummaryEntity();
ratingSummaryEntity.setAverageRate(Double.valueOf(4.2));
ratingSummaryEntity.setNumberOfRatings(10);
doReturn(true).when(ratingService).isEnabled();
doReturn(ratingSummaryEntity).when(ratingService).findSummaryByApi(API_ID);
doReturn(true).when(parameterService).findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
Proxy proxy = new Proxy();
proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/foo")));
apiEntity.setProxy(proxy);
apiEntity.setLifecycleState(ApiLifecycleState.PUBLISHED);
apiEntity.setUpdatedAt(nowDate);
// Test
Api responseApi = apiMapper.convert(apiEntity);
assertNotNull(responseApi);
assertNull(responseApi.getPages());
assertNull(responseApi.getPlans());
assertEquals(API_DESCRIPTION, responseApi.getDescription());
assertEquals(API_ID, responseApi.getId());
assertEquals(API_NAME, responseApi.getName());
assertEquals(API_VERSION, responseApi.getVersion());
assertFalse(responseApi.getDraft());
List<String> entrypoints = responseApi.getEntrypoints();
assertNotNull(entrypoints);
assertEquals(2, entrypoints.size());
assertEquals(API_ENTRYPOINT_1, entrypoints.get(0));
assertEquals(API + "/foo", entrypoints.get(1));
List<String> labels = responseApi.getLabels();
assertNotNull(labels);
assertTrue(labels.contains(API_LABEL));
User owner = responseApi.getOwner();
assertNotNull(owner);
assertEquals(API_OWNER_ID, owner.getId());
assertEquals(API_OWNER_EMAIL, owner.getEmail());
assertEquals(API_OWNER_FIRSTNAME + ' ' + API_OWNER_LASTNAME, owner.getDisplayName());
assertEquals(now.toEpochMilli(), responseApi.getUpdatedAt().toInstant().toEpochMilli());
List<String> categories = responseApi.getCategories();
assertNotNull(categories);
assertTrue(categories.contains(API_CATEGORY));
RatingSummary ratingSummary = responseApi.getRatingSummary();
assertNotNull(ratingSummary);
assertEquals(Double.valueOf(4.2), ratingSummary.getAverage());
assertEquals(BigDecimal.valueOf(10), ratingSummary.getCount());
}
use of io.gravitee.rest.api.portal.rest.model.Api in project gravitee-management-rest-api by gravitee-io.
the class ApiResourceNotAuthenticatedTest method init.
@Before
public void init() {
resetAllMocks();
mockApi = new ApiEntity();
mockApi.setId(API);
doReturn(mockApi).when(apiService).findById(API);
when(accessControlService.canAccessApiFromPortal(API)).thenReturn(true);
when(accessControlService.canAccessApiFromPortal(mockApi)).thenReturn(true);
doReturn(Arrays.asList(new PageEntity())).when(pageService).search(any(), eq(GraviteeContext.getCurrentEnvironment()));
PlanEntity plan1 = new PlanEntity();
plan1.setId("A");
plan1.setStatus(PlanStatus.PUBLISHED);
PlanEntity plan2 = new PlanEntity();
plan2.setId("B");
plan2.setStatus(PlanStatus.PUBLISHED);
PlanEntity plan3 = new PlanEntity();
plan3.setId("C");
plan3.setStatus(PlanStatus.CLOSED);
doReturn(new HashSet<PlanEntity>(Arrays.asList(plan1, plan2, plan3))).when(planService).findByApi(API);
doReturn(new Api()).when(apiMapper).convert(any());
doReturn(new Page()).when(pageMapper).convert(any());
doReturn(new Plan()).when(planMapper).convert(any());
}
use of io.gravitee.rest.api.portal.rest.model.Api in project gravitee-management-rest-api by gravitee-io.
the class ApiMapper method convert.
public Api convert(ApiEntity api) {
final Api apiItem = new Api();
apiItem.setDescription(api.getDescription());
List<ApiEntrypointEntity> apiEntrypoints = api.getEntrypoints();
if (apiEntrypoints != null) {
List<String> entrypoints = apiEntrypoints.stream().map(ApiEntrypointEntity::getTarget).collect(Collectors.toList());
apiItem.setEntrypoints(entrypoints);
}
apiItem.setDraft(api.getLifecycleState() == ApiLifecycleState.UNPUBLISHED || api.getLifecycleState() == ApiLifecycleState.CREATED);
apiItem.setRunning(api.getState() == Lifecycle.State.STARTED);
apiItem.setPublic(api.getVisibility() == Visibility.PUBLIC);
apiItem.setId(api.getId());
List<String> apiLabels = api.getLabels();
if (apiLabels != null) {
apiItem.setLabels(new ArrayList<>(apiLabels));
} else {
apiItem.setLabels(new ArrayList<>());
}
apiItem.setName(api.getName());
PrimaryOwnerEntity primaryOwner = api.getPrimaryOwner();
if (primaryOwner != null) {
User owner = new User();
owner.setId(primaryOwner.getId());
owner.setDisplayName(primaryOwner.getDisplayName());
owner.setEmail(primaryOwner.getEmail());
apiItem.setOwner(owner);
}
apiItem.setPages(null);
apiItem.setPlans(null);
if (ratingService.isEnabled()) {
final RatingSummaryEntity ratingSummaryEntity = ratingService.findSummaryByApi(api.getId());
RatingSummary ratingSummary = new RatingSummary().average(ratingSummaryEntity.getAverageRate()).count(BigDecimal.valueOf(ratingSummaryEntity.getNumberOfRatings()));
apiItem.setRatingSummary(ratingSummary);
}
if (api.getUpdatedAt() != null) {
apiItem.setUpdatedAt(api.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
}
apiItem.setVersion(api.getVersion());
boolean isCategoryModeEnabled = this.parameterService.findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
if (isCategoryModeEnabled && api.getCategories() != null) {
apiItem.setCategories(api.getCategories().stream().filter(categoryId -> {
try {
categoryService.findNotHiddenById(categoryId);
return true;
} catch (CategoryNotFoundException v) {
return false;
}
}).collect(Collectors.toList()));
} else {
apiItem.setCategories(new ArrayList<>());
}
return apiItem;
}
Aggregations