Search in sources :

Example 1 with EventEntity

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

the class PlatformEventsResource method listEvents.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.MANAGEMENT_PLATFORM, acls = RolePermissionAction.READ) })
public Page<EventEntity> listEvents(@BeanParam EventSearchParam eventSearchParam) {
    eventSearchParam.validate();
    Map<String, Object> properties = new HashMap<>();
    if (eventSearchParam.getApiIdsParam() != null && eventSearchParam.getApiIdsParam().getIds() != null && !eventSearchParam.getApiIdsParam().getIds().isEmpty()) {
        properties.put(Event.EventProperties.API_ID.getValue(), eventSearchParam.getApiIdsParam().getIds());
    }
    Page<EventEntity> events = eventService.search(eventSearchParam.getEventTypeListParam().getEventTypes(), properties, eventSearchParam.getFrom(), eventSearchParam.getTo(), eventSearchParam.getPage(), eventSearchParam.getSize());
    events.getContent().forEach(event -> {
        Map<String, String> properties1 = event.getProperties();
        // Event relative to API
        if (properties1 != null && properties1.containsKey(Event.EventProperties.API_ID.getValue())) {
            // Remove payload content from response since it's not required anymore
            event.setPayload(null);
            // Retrieve additional data
            String apiId = properties1.get(Event.EventProperties.API_ID.getValue());
            ApiEntity api = apiService.findById(apiId);
            properties1.put("api_name", api.getName());
            properties1.put("api_version", api.getVersion());
        }
    });
    return events;
}
Also used : HashMap(java.util.HashMap) EventEntity(io.gravitee.management.model.EventEntity) ApiEntity(io.gravitee.management.model.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Permissions(io.gravitee.management.rest.security.Permissions)

Example 2 with EventEntity

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

the class EventServiceImpl method convert.

private EventEntity convert(Event event) {
    EventEntity eventEntity = new EventEntity();
    eventEntity.setId(event.getId());
    eventEntity.setType(io.gravitee.management.model.EventType.valueOf(event.getType().toString()));
    eventEntity.setPayload(event.getPayload());
    eventEntity.setParentId(event.getParentId());
    eventEntity.setProperties(event.getProperties());
    eventEntity.setCreatedAt(event.getCreatedAt());
    eventEntity.setUpdatedAt(event.getUpdatedAt());
    if (event.getProperties() != null) {
        final String userId = event.getProperties().get(Event.EventProperties.USER.getValue());
        if (userId != null && !userId.isEmpty()) {
            try {
                eventEntity.setUser(userService.findById(userId));
            } catch (UserNotFoundException unfe) {
                UserEntity user = new UserEntity();
                user.setSource("system");
                user.setId(userId);
                user.setUsername("system");
                eventEntity.setUser(user);
            }
        }
    }
    return eventEntity;
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) EventEntity(io.gravitee.management.model.EventEntity) NewEventEntity(io.gravitee.management.model.NewEventEntity) UserEntity(io.gravitee.management.model.UserEntity)

Example 3 with EventEntity

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

the class ApiService_StartTest method shouldStart.

@Test
public void shouldStart() throws Exception {
    objectMapper.addMixIn(Api.class, ApiMixin.class);
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    when(apiRepository.update(api)).thenReturn(api);
    final EventEntity event = mockEvent(EventType.PUBLISH_API);
    when(eventService.findByApi(API_ID)).thenReturn(Collections.singleton(event));
    Membership po = new Membership(USER_NAME, API_ID, MembershipReferenceType.API);
    po.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), any(), any())).thenReturn(Collections.singleton(po));
    apiService.start(API_ID, USER_NAME);
    verify(api).setUpdatedAt(any());
    verify(api).setLifecycleState(LifecycleState.STARTED);
    verify(apiRepository).update(api);
    verify(eventService).create(EventType.START_API, event.getPayload(), event.getProperties());
}
Also used : EventEntity(io.gravitee.management.model.EventEntity) Test(org.junit.Test)

Example 4 with EventEntity

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

the class ApiService_StopTest method mockEvent.

private EventEntity mockEvent(EventType eventType) throws Exception {
    final JsonNodeFactory factory = JsonNodeFactory.instance;
    ObjectNode node = factory.objectNode();
    node.set("id", factory.textNode(API_ID));
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(Event.EventProperties.API_ID.getValue(), API_ID);
    properties.put(Event.EventProperties.USER.getValue(), USER_NAME);
    Api api = new Api();
    api.setId(API_ID);
    EventEntity event = new EventEntity();
    event.setType(eventType);
    event.setId(UUID.randomUUID().toString());
    event.setPayload(objectMapper.writeValueAsString(api));
    event.setCreatedAt(new Date());
    event.setUpdatedAt(event.getCreatedAt());
    event.setProperties(properties);
    return event;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EventEntity(io.gravitee.management.model.EventEntity) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 5 with EventEntity

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

the class ApiService_StopTest method shouldStop.

@Test
public void shouldStop() throws Exception {
    objectMapper.addMixIn(Api.class, ApiMixin.class);
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    when(apiRepository.update(api)).thenReturn(api);
    final EventEntity event = mockEvent(EventType.PUBLISH_API);
    when(eventService.findByApi(API_ID)).thenReturn(Collections.singleton(event));
    Membership po = new Membership(USER_NAME, API_ID, MembershipReferenceType.API);
    po.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), any(), any())).thenReturn(Collections.singleton(po));
    apiService.stop(API_ID, USER_NAME);
    verify(api).setUpdatedAt(any());
    verify(api).setLifecycleState(LifecycleState.STOPPED);
    verify(apiRepository).update(api);
    verify(eventService).create(EventType.STOP_API, event.getPayload(), event.getProperties());
}
Also used : EventEntity(io.gravitee.management.model.EventEntity) Test(org.junit.Test)

Aggregations

EventEntity (io.gravitee.management.model.EventEntity)11 Test (org.junit.Test)7 NewEventEntity (io.gravitee.management.model.NewEventEntity)6 EventCriteria (io.gravitee.repository.management.api.search.EventCriteria)3 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ApiEntity (io.gravitee.management.model.ApiEntity)1 UserEntity (io.gravitee.management.model.UserEntity)1 Permissions (io.gravitee.management.rest.security.Permissions)1 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1