use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.
the class ViewsResource method update.
@PUT
@Path("{id}")
@ApiOperation("Update view")
@AuditEvent(type = ViewsAuditEventTypes.VIEW_UPDATE)
public ViewDTO update(@ApiParam(name = "id") @PathParam("id") @NotEmpty String id, @ApiParam @Valid ViewDTO dto, @Context SearchUser searchUser) {
final ViewDTO updatedDTO = dto.toBuilder().id(id).build();
if (!searchUser.canUpdateView(updatedDTO)) {
throw new ForbiddenException("Not allowed to edit " + summarize(updatedDTO) + ".");
}
validateIntegrity(updatedDTO, searchUser);
return dbService.update(updatedDTO);
}
use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.
the class ViewsResource method validateIntegrity.
private void validateIntegrity(ViewDTO dto, SearchUser searchUser) {
final Search search = searchDomain.getForUser(dto.searchId(), searchUser).orElseThrow(() -> new BadRequestException("Search " + dto.searchId() + " not available"));
final Set<String> searchQueries = search.queries().stream().map(Query::id).collect(Collectors.toSet());
final Set<String> stateQueries = dto.state().keySet();
if (!searchQueries.containsAll(stateQueries)) {
final Sets.SetView<String> diff = Sets.difference(searchQueries, stateQueries);
throw new BadRequestException("Search queries do not correspond to view/state queries, missing query IDs: " + diff);
}
final Set<String> searchTypes = search.queries().stream().flatMap(q -> q.searchTypes().stream()).map(SearchType::id).collect(Collectors.toSet());
final Set<String> stateTypes = dto.state().values().stream().flatMap(v -> v.widgetMapping().values().stream()).flatMap(Collection::stream).collect(Collectors.toSet());
if (!searchTypes.containsAll(stateTypes)) {
final Sets.SetView<String> diff = Sets.difference(searchTypes, stateTypes);
throw new BadRequestException("Search types do not correspond to view/search types, missing searches: " + diff);
}
final Set<String> widgetIds = dto.state().values().stream().flatMap(v -> v.widgets().stream()).map(WidgetDTO::id).collect(Collectors.toSet());
final Set<String> widgetPositions = dto.state().values().stream().flatMap(v -> v.widgetPositions().keySet().stream()).collect(Collectors.toSet());
if (!widgetPositions.containsAll(widgetIds)) {
final Sets.SetView<String> diff = Sets.difference(widgetPositions, widgetIds);
throw new BadRequestException("Widget positions don't correspond to widgets, missing widget possitions: " + diff);
}
}
use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.
the class ViewFacadeTest method itShouldCreateADTOFromAnEntity.
@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldCreateADTOFromAnEntity() throws Exception {
final StreamImpl stream = new StreamImpl(Collections.emptyMap());
final Entity viewEntity = createViewEntity();
final Map<EntityDescriptor, Object> nativeEntities = new HashMap<>(1);
nativeEntities.put(EntityDescriptor.create(newStreamId, ModelTypes.STREAM_V1), stream);
final UserImpl fakeUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "testuser"));
when(userService.load("testuser")).thenReturn(fakeUser);
final NativeEntity<ViewDTO> nativeEntity = facade.createNativeEntity(viewEntity, Collections.emptyMap(), nativeEntities, "testuser");
assertThat(nativeEntity.descriptor().title()).isEqualTo("title");
assertThat(nativeEntity.descriptor().type()).isEqualTo(ModelTypes.SEARCH_V1);
Optional<ViewDTO> resultedView = viewService.get(nativeEntity.descriptor().id().id());
assertThat(resultedView).isPresent();
Optional<Search> search = searchDbService.get(resultedView.get().searchId());
assertThat(search).isPresent();
final Query query = search.get().queries().iterator().next();
assertThat(query.filter()).isNotNull();
assertThat(query.filter().filters()).isNotEmpty();
final StreamFilter streamFilter = (StreamFilter) query.filter().filters().iterator().next();
assertThat(streamFilter.streamId()).doesNotMatch(newStreamId);
}
use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.
the class ViewFacadeTest method itShouldCreateAViewEntity.
@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldCreateAViewEntity() {
final ViewDTO viewDTO = viewService.get(viewId).orElseThrow(() -> new NotFoundException("Missing view with id: " + viewId));
final EntityDescriptor searchDescriptor = EntityDescriptor.create(viewDTO.id(), ModelTypes.SEARCH_V1);
final EntityDescriptor streamDescriptor = EntityDescriptor.create(streamId, ModelTypes.STREAM_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(searchDescriptor, streamDescriptor);
final Optional<Entity> optionalEntity = facade.exportEntity(searchDescriptor, entityDescriptorIds);
assertThat(optionalEntity).isPresent();
final Entity entity = optionalEntity.get();
assertThat(entity).isInstanceOf(EntityV1.class);
final EntityV1 entityV1 = (EntityV1) entity;
assertThat(entityV1.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(searchDescriptor).orElse(null)));
assertThat(entityV1.type()).isEqualTo(ModelTypes.SEARCH_V1);
final ViewEntity viewEntity = objectMapper.convertValue(entityV1.data(), ViewEntity.class);
assertThat(viewEntity.title().asString()).isEqualTo(viewDTO.title());
assertThat(viewEntity.type().toString()).isEqualTo(ViewDTO.Type.SEARCH.toString());
assertThat(viewEntity.search().queries().size()).isEqualTo(1);
final QueryEntity queryEntity = viewEntity.search().queries().iterator().next();
assertThat(queryEntity.filter().filters().size()).isEqualTo(1);
final StreamFilter filter = (StreamFilter) queryEntity.filter().filters().iterator().next();
assertThat(filter.streamId()).doesNotMatch(streamId);
}
use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.
the class ViewFacadeTest method itShouldListEntityExcerptsForAllViewsInDB.
@Test
@MongoDBFixtures("ViewFacadeTest.json")
public void itShouldListEntityExcerptsForAllViewsInDB() {
final ViewDTO viewDTO = viewService.get(viewId).orElseThrow(() -> new NotFoundException("Missing view with id: " + viewId));
final EntityExcerpt entityExcerpt = EntityExcerpt.builder().title(viewDTO.title()).id(ModelId.of(viewId)).type(ModelTypes.SEARCH_V1).build();
final Set<EntityExcerpt> entityExcerpts = facade.listEntityExcerpts();
assertThat(entityExcerpts).hasSize(1).contains(entityExcerpt);
}
Aggregations