generated from GenomicDataInfrastructure/oss-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...b/genomicdatainfrastructure/discovery/filters/infrastructure/mapper/CkanFilterMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...nomicdatainfrastructure/discovery/filters/infrastructure/mapper/CkanFilterMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |