@SuppressWarnings("unchecked") @Test void testFindAttributes() { Repository<Attribute> attributeRepository = mock(Repository.class); EntityType entityType = mock(EntityType.class); when(attributeRepository.getEntityType()).thenReturn(entityType); when(metadataService.getRepository("sys_md_Attribute", Attribute.class)) .thenReturn(Optional.of(attributeRepository)); Query query = mock(Query.class); when(queryMapper.map(query, attributeRepository)).thenReturn(new QueryImpl<>()); Attribute attribute = mock(Attribute.class); ArgumentCaptor<org.molgenis.data.Query<Attribute>> findQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class); when(attributeRepository.findAll(findQueryCaptor.capture())).thenReturn(Stream.of(attribute)); long total = 100L; ArgumentCaptor<org.molgenis.data.Query<Attribute>> countQueryCaptor = ArgumentCaptor.forClass(org.molgenis.data.Query.class); when(attributeRepository.count(countQueryCaptor.capture())).thenReturn(total); Sort sort = Sort.EMPTY_SORT; org.molgenis.data.Sort repositorySort = mock(org.molgenis.data.Sort.class); when(sortMapper.map(sort, entityType)).thenReturn(repositorySort); int size = 5; int number = 2; Attributes expectedAttributes = Attributes.builder() .setAttributes(ImmutableList.of(attribute)) .setTotal((int) total) .build(); String entityTypeId = "MyEntityTypeId"; when(metadataService.hasEntityType(entityTypeId)).thenReturn(true); Attributes attributes = metadataApiService.findAttributes(entityTypeId, query, sort, size, number); org.molgenis.data.Query<Attribute> findQuery = findQueryCaptor.getValue(); org.molgenis.data.Query<Attribute> countQuery = countQueryCaptor.getValue(); assertAll( () -> assertEquals(expectedAttributes, attributes), () -> assertEquals( singletonList(new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), findQuery.getRules()), () -> assertEquals(size * number, findQuery.getOffset()), () -> assertEquals(size, findQuery.getPageSize()), () -> assertEquals(repositorySort, findQuery.getSort()), () -> assertEquals( singletonList(new QueryRule("entity", QueryRule.Operator.EQUALS, entityTypeId)), countQuery.getRules()), () -> assertEquals(0, countQuery.getOffset()), () -> assertEquals(Integer.MAX_VALUE, countQuery.getPageSize())); }
@Override public Attributes findAttributes( String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number) { if (!metadataService.hasEntityType(entityTypeId)) { throw new UnknownEntityTypeException(entityTypeId); } Repository<Attribute> repository = getAttributeRepository(); org.molgenis.data.Query<Attribute> repositoryQuery = toAttributeRepositoryQuery(entityTypeId, query, repository); org.molgenis.data.Query<Attribute> findQuery = new QueryImpl<>(repositoryQuery); findQuery.offset(number * size); findQuery.pageSize(size); findQuery.sort(sortMapper.map(sort, repository.getEntityType())); List<Attribute> attributes = repository.findAll(findQuery).collect(toList()); org.molgenis.data.Query<Attribute> countQuery = new QueryImpl<>(repositoryQuery); countQuery.offset(0); countQuery.pageSize(Integer.MAX_VALUE); int count = Math.toIntExact(repository.count(countQuery)); return Attributes.builder().setAttributes(attributes).setTotal(count).build(); }
MetadataApiServiceImpl implements MetadataApiService { @Override public Attributes findAttributes( String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number) { if (!metadataService.hasEntityType(entityTypeId)) { throw new UnknownEntityTypeException(entityTypeId); } Repository<Attribute> repository = getAttributeRepository(); org.molgenis.data.Query<Attribute> repositoryQuery = toAttributeRepositoryQuery(entityTypeId, query, repository); org.molgenis.data.Query<Attribute> findQuery = new QueryImpl<>(repositoryQuery); findQuery.offset(number * size); findQuery.pageSize(size); findQuery.sort(sortMapper.map(sort, repository.getEntityType())); List<Attribute> attributes = repository.findAll(findQuery).collect(toList()); org.molgenis.data.Query<Attribute> countQuery = new QueryImpl<>(repositoryQuery); countQuery.offset(0); countQuery.pageSize(Integer.MAX_VALUE); int count = Math.toIntExact(repository.count(countQuery)); return Attributes.builder().setAttributes(attributes).setTotal(count).build(); } }
MetadataApiServiceImpl implements MetadataApiService { @Override public Attributes findAttributes( String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number) { if (!metadataService.hasEntityType(entityTypeId)) { throw new UnknownEntityTypeException(entityTypeId); } Repository<Attribute> repository = getAttributeRepository(); org.molgenis.data.Query<Attribute> repositoryQuery = toAttributeRepositoryQuery(entityTypeId, query, repository); org.molgenis.data.Query<Attribute> findQuery = new QueryImpl<>(repositoryQuery); findQuery.offset(number * size); findQuery.pageSize(size); findQuery.sort(sortMapper.map(sort, repository.getEntityType())); List<Attribute> attributes = repository.findAll(findQuery).collect(toList()); org.molgenis.data.Query<Attribute> countQuery = new QueryImpl<>(repositoryQuery); countQuery.offset(0); countQuery.pageSize(Integer.MAX_VALUE); int count = Math.toIntExact(repository.count(countQuery)); return Attributes.builder().setAttributes(attributes).setTotal(count).build(); } MetadataApiServiceImpl(
      MetaDataService metadataService,
      QueryMapper queryMapperV3,
      SortMapper sortMapper,
      MetadataApiJobService metadataApiJobService); }
MetadataApiServiceImpl implements MetadataApiService { @Override public Attributes findAttributes( String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number) { if (!metadataService.hasEntityType(entityTypeId)) { throw new UnknownEntityTypeException(entityTypeId); } Repository<Attribute> repository = getAttributeRepository(); org.molgenis.data.Query<Attribute> repositoryQuery = toAttributeRepositoryQuery(entityTypeId, query, repository); org.molgenis.data.Query<Attribute> findQuery = new QueryImpl<>(repositoryQuery); findQuery.offset(number * size); findQuery.pageSize(size); findQuery.sort(sortMapper.map(sort, repository.getEntityType())); List<Attribute> attributes = repository.findAll(findQuery).collect(toList()); org.molgenis.data.Query<Attribute> countQuery = new QueryImpl<>(repositoryQuery); countQuery.offset(0); countQuery.pageSize(Integer.MAX_VALUE); int count = Math.toIntExact(repository.count(countQuery)); return Attributes.builder().setAttributes(attributes).setTotal(count).build(); } MetadataApiServiceImpl(
      MetaDataService metadataService,
      QueryMapper queryMapperV3,
      SortMapper sortMapper,
      MetadataApiJobService metadataApiJobService); EntityTypes findEntityTypes(
      @Nullable @CheckForNull Query query, Sort sort, int size, int number); @Override EntityType findEntityType(String identifier); @Override Attributes findAttributes(
      String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number); @Override Attribute findAttribute(String entityTypeId, String attributeId); @Override MetadataUpsertJobExecution deleteAttributeAsync(String entityTypeId, String attributeId); @Override MetadataUpsertJobExecution deleteAttributesAsync(String entityTypeId, Query query); @Override void createEntityType(EntityType entityType); @Override MetadataUpsertJobExecution updateEntityTypeAsync(EntityType entityType); @Override MetadataDeleteJobExecution deleteEntityTypeAsync(String entityTypeId); @Override MetadataDeleteJobExecution deleteEntityTypesAsync(Query query); }
MetadataApiServiceImpl implements MetadataApiService { @Override public Attributes findAttributes( String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number) { if (!metadataService.hasEntityType(entityTypeId)) { throw new UnknownEntityTypeException(entityTypeId); } Repository<Attribute> repository = getAttributeRepository(); org.molgenis.data.Query<Attribute> repositoryQuery = toAttributeRepositoryQuery(entityTypeId, query, repository); org.molgenis.data.Query<Attribute> findQuery = new QueryImpl<>(repositoryQuery); findQuery.offset(number * size); findQuery.pageSize(size); findQuery.sort(sortMapper.map(sort, repository.getEntityType())); List<Attribute> attributes = repository.findAll(findQuery).collect(toList()); org.molgenis.data.Query<Attribute> countQuery = new QueryImpl<>(repositoryQuery); countQuery.offset(0); countQuery.pageSize(Integer.MAX_VALUE); int count = Math.toIntExact(repository.count(countQuery)); return Attributes.builder().setAttributes(attributes).setTotal(count).build(); } MetadataApiServiceImpl(
      MetaDataService metadataService,
      QueryMapper queryMapperV3,
      SortMapper sortMapper,
      MetadataApiJobService metadataApiJobService); EntityTypes findEntityTypes(
      @Nullable @CheckForNull Query query, Sort sort, int size, int number); @Override EntityType findEntityType(String identifier); @Override Attributes findAttributes(
      String entityTypeId, @Nullable @CheckForNull Query query, Sort sort, int size, int number); @Override Attribute findAttribute(String entityTypeId, String attributeId); @Override MetadataUpsertJobExecution deleteAttributeAsync(String entityTypeId, String attributeId); @Override MetadataUpsertJobExecution deleteAttributesAsync(String entityTypeId, Query query); @Override void createEntityType(EntityType entityType); @Override MetadataUpsertJobExecution updateEntityTypeAsync(EntityType entityType); @Override MetadataDeleteJobExecution deleteEntityTypeAsync(String entityTypeId); @Override MetadataDeleteJobExecution deleteEntityTypesAsync(Query query);  }