Skip to content

Commit

Permalink
chore: (ART-10688) polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jadz94 committed Nov 29, 2024
1 parent 670f9a7 commit e97062a
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.mapstruct.CollectionMappingStrategy.ADDER_PREFERRED;
import static org.mapstruct.NullValueCheckStrategy.ALWAYS;

@Mapper(componentModel = "jakarta", nullValueCheckStrategy = ALWAYS, nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT, collectionMappingStrategy = ADDER_PREFERRED)
public interface CkanMapper {
public interface CkanDatasetsMapper {

DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import io.github.genomicdatainfrastructure.discovery.datasets.application.ports.DatasetsRepository;
import io.github.genomicdatainfrastructure.discovery.datasets.domain.exceptions.DatasetNotFoundException;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper.CkanMapper;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper.CkanDatasetsMapper;
import io.github.genomicdatainfrastructure.discovery.model.*;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.*;
Expand All @@ -25,14 +25,14 @@
public class CkanDatasetsRepository implements DatasetsRepository {

private final CkanQueryApi ckanQueryApi;
private final CkanMapper ckanMapper;
private final CkanDatasetsMapper ckanDatasetsMapper;

@Inject
public CkanDatasetsRepository(
@RestClient CkanQueryApi ckanQueryApi, CkanMapper ckanMapper
@RestClient CkanQueryApi ckanQueryApi, CkanDatasetsMapper ckanDatasetsMapper
) {
this.ckanQueryApi = ckanQueryApi;
this.ckanMapper = ckanMapper;
this.ckanDatasetsMapper = ckanDatasetsMapper;
}

@Override
Expand Down Expand Up @@ -73,14 +73,14 @@ public List<SearchedDataset> search(
request
);

return ckanMapper.map(response.getResult());
return ckanDatasetsMapper.map(response.getResult());
}

@Override
public RetrievedDataset findById(String id, String accessToken) {
try {
var ckanPackage = ckanQueryApi.packageShow(id);
return ckanMapper.map(ckanPackage.getResult());
return ckanDatasetsMapper.map(ckanPackage.getResult());
} catch (WebApplicationException e) {
if (e.getResponse().getStatus() == 404) {
throw new DatasetNotFoundException(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.github.genomicdatainfrastructure.discovery.filters.infrastructure.ckan;

import io.github.genomicdatainfrastructure.discovery.filters.application.ports.FiltersRepository;
import io.github.genomicdatainfrastructure.discovery.filters.infrastructure.mapper.CkanFilterMapper;
import io.github.genomicdatainfrastructure.discovery.model.ValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanFacet;
Expand All @@ -23,52 +24,29 @@ public class CkanFiltersRepository implements FiltersRepository {
private static final String SELECTED_FACETS_PATTERN = "[\"%s\"]";

private final CkanQueryApi ckanQueryApi;
private final CkanFilterMapper ckanFilterMapper;

public CkanFiltersRepository(@RestClient CkanQueryApi ckanQueryApi) {
public CkanFiltersRepository(@RestClient CkanQueryApi ckanQueryApi,
CkanFilterMapper ckanFilterMapper) {
this.ckanQueryApi = ckanQueryApi;

this.ckanFilterMapper = ckanFilterMapper;
}

@Override
public List<ValueLabel> getValuesForFilter(String key) {
public List<ValueLabel> getValuesForFilter(final String key) {

var facetField = SELECTED_FACETS_PATTERN.formatted(key);
final var facetField = SELECTED_FACETS_PATTERN.formatted(key);

var request = PackageSearchRequest.builder()
final var request = PackageSearchRequest.builder()
.facetField(facetField)
.rows(0)
.facetLimit(-1)
.build();

var response = ckanQueryApi.packageSearch(
final var response = ckanQueryApi.packageSearch(
request
);

var ckanFacet = ofNullable(response.getResult())
.map(PackagesSearchResult::getSearchFacets)
.orElseGet(Map::of)
.get(key);
if (ckanFacet == null) {
return List.of();
}
return filters(ckanFacet);

return ckanFilterMapper.map(response, key);
}

private List<ValueLabel> filters(CkanFacet facet) {

var values = ofNullable(facet.getItems())
.orElseGet(List::of)
.stream()
.map(value -> ValueLabel.builder()
.value(value.getName())
.label(value.getDisplayName())
.count(value.getCount())
.build()
)
.toList();

return values;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.discovery.filters.infrastructure.mapper;

import io.github.genomicdatainfrastructure.discovery.model.ValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanFacet;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.PackagesSearchResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.mapstruct.CollectionMappingStrategy.ADDER_PREFERRED;
import static org.mapstruct.NullValueCheckStrategy.ALWAYS;

@Mapper(componentModel = "jakarta", nullValueCheckStrategy = ALWAYS, nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT, collectionMappingStrategy = ADDER_PREFERRED)
public interface CkanFilterMapper {

default List<ValueLabel> map(final PackagesSearchResponse packagesSearchResponse,
final String key) {
if (Objects.isNull(packagesSearchResponse) || Objects.isNull(packagesSearchResponse
.getResult())) {
return Collections.emptyList();
}
return map(packagesSearchResponse.getResult().getSearchFacets(), key);
}

default List<ValueLabel> map(final Map<String, CkanFacet> ckanFacets, final String key) {
if (Objects.isNull(ckanFacets)) {
return Collections.emptyList();
}
return map(ckanFacets.get(key));
}

default List<ValueLabel> map(final CkanFacet ckanFacet) {
if (Objects.isNull(ckanFacet)) {
return Collections.emptyList();
}
return map(ckanFacet.getItems());
}

List<ValueLabel> map(final List<CkanValueLabel> items);

@Mapping(target = "value", source = "name")
@Mapping(target = "label", source = "displayName")
ValueLabel map(final CkanValueLabel valueLabel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.discovery.services;
package io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper;

import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper.CkanMapper;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper.CkanMapperImpl;
import io.github.genomicdatainfrastructure.discovery.model.*;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.*;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,9 +21,9 @@
import static java.time.OffsetDateTime.parse;
import static org.assertj.core.api.Assertions.assertThat;

class PackageShowMapperTest {
class CkanDatasetsMapperTest {

private final CkanMapper mapper = new CkanMapperImpl();
private final CkanDatasetsMapper mapper = new CkanDatasetsMapperImpl();

@Nested
class RetrievedDatasetTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.genomicdatainfrastructure.discovery.filters.infrastructure.mapper;

import io.github.genomicdatainfrastructure.discovery.model.ValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanFacet;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.PackagesSearchResponse;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.PackagesSearchResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class CkanFilterMapperTest {

private final CkanFilterMapper mapper = new CkanFilterMapperImpl();

static Stream<PackagesSearchResponse> edgeCases() {
return Stream.of(
null,
new PackagesSearchResponse().result(null),
new PackagesSearchResponse().result(new PackagesSearchResult().searchFacets(null)),
new PackagesSearchResponse().result(new PackagesSearchResult().searchFacets(Map.of(
"", new CkanFacet()))));
}

@ParameterizedTest
@MethodSource("edgeCases")
void given_PackagesSearchResponse_but_with_null_objects_should_return_emptyList(
PackagesSearchResponse input) {
final var actual = mapper.map(input, "randomKey");
assertThat(actual).isEmpty();
}

@Test
void given_PackagesSearchResponse_with_valid_input_should_return_expected_ListOfValueLabel() {
final var input = new PackagesSearchResponse()
.result(new PackagesSearchResult().searchFacets(
Map.of("requestedKey", new CkanFacet()
.items(List.of(new CkanValueLabel()
.name("CkanValueName1")
.count(1),
new CkanValueLabel()
.name("CkanValueName2")
.displayName("CkanValueDisplayName2"),
new CkanValueLabel()
.displayName("CkanValueDisplayName3")
.count(3))),
"notRequestedKey", new CkanFacet())
));
final var expected = List.of(new ValueLabel()
.value("CkanValueName1")
.count(1),
new ValueLabel()
.value("CkanValueName2")
.label("CkanValueDisplayName2"),
new ValueLabel()
.label("CkanValueDisplayName3")
.count(3)
);

final var actual = mapper.map(input, "requestedKey");

assertThat(actual).isNotEmpty();
assertThat(actual).usingRecursiveComparison()
.isEqualTo(expected);
}
}

0 comments on commit e97062a

Please sign in to comment.