Skip to content

Commit

Permalink
Add dc mappings (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling authored Aug 8, 2024
1 parent de4339a commit f40b127
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ protected void registerChannelTags() {
channelTags.putIfAbsent("/rss/channel/image/height", (channel, value) -> createIfNullOptional(channel::getImage, channel::setImage, Image::new).ifPresent(i -> mapInteger(value, i::setHeight)));
channelTags.putIfAbsent("/rss/channel/image/width", (channel, value) -> createIfNullOptional(channel::getImage, channel::setImage, Image::new).ifPresent(i -> mapInteger(value, i::setWidth)));
channelTags.putIfAbsent("dc:language", (channel, value) -> Mapper.mapIfEmpty(value, channel::getLanguage, channel::setLanguage));
channelTags.putIfAbsent("dc:publisher", (channel, value) -> Mapper.mapIfEmpty(value, channel::getCopyright, channel::setCopyright));
channelTags.putIfAbsent("dc:rights", (channel, value) -> Mapper.mapIfEmpty(value, channel::getCopyright, channel::setCopyright));
channelTags.putIfAbsent("dc:title", (channel, value) -> Mapper.mapIfEmpty(value, channel::getTitle, channel::setTitle));
channelTags.putIfAbsent("dc:date", (channel, value) -> Mapper.mapIfEmpty(value, channel::getPubDate, channel::setPubDate));
}

/**
Expand Down Expand Up @@ -209,18 +210,19 @@ protected void registerItemTags() {
itemTags.putIfAbsent("category", Item::addCategory);
itemTags.putIfAbsent("pubDate", Item::setPubDate);
itemTags.putIfAbsent("published", Item::setPubDate);
itemTags.putIfAbsent("updated", (item, value) -> { if (item.getPubDate().isEmpty()) item.setPubDate(value); });
itemTags.putIfAbsent("updated", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate));
itemTags.putIfAbsent("comments", Item::setComments);
itemTags.putIfAbsent("dc:creator", (item, value) -> Mapper.mapIfEmpty(value, item::getAuthor, item::setAuthor));
itemTags.putIfAbsent("dc:date", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate));
itemTags.putIfAbsent("dc:identifier", (item, value) -> Mapper.mapIfEmpty(value, item::getGuid, item::setGuid));
itemTags.putIfAbsent("dc:title", (item, value) -> Mapper.mapIfEmpty(value, item::getTitle, item::setTitle));
itemTags.putIfAbsent("dc:description", (item, value) -> Mapper.mapIfEmpty(value, item::getDescription, item::setDescription));

onItemTags.put("enclosure", item -> item.addEnclosure(new Enclosure()));
}

/**
* Register itam attributes for mapping to item object fields
* Register item attributes for mapping to item object fields
*/
protected void registerItemAttributes() {
itemAttributes.computeIfAbsent("link", k -> new HashMap<>()).putIfAbsent("href", Item::setLink);
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/apptasticsoftware/rssreader/util/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void mapLong(String text, Consumer<Long> func) {
}

private static <T> void mapNumber(String text, Consumer<T> func, Function<String, T> convert) {
if (text != null && !text.isBlank()) {
if (!isNullOrEmpty(text)) {
try {
func.accept(convert.apply(text));
} catch (NumberFormatException e) {
Expand All @@ -67,7 +67,7 @@ private static <T> void mapNumber(String text, Consumer<T> func, Function<String
* @param <T> type
*/
public static <T> void mapIfEmpty(String text, Supplier<T> getter, Consumer<String> setter) {
if ((getter.get() == null || "".equals(getter.get()) || getter.get() == Optional.empty()) && text != null && !text.isBlank()) {
if (isNullOrEmpty(getter) && !isNullOrEmpty(text)) {
setter.accept(text);
}
}
Expand Down Expand Up @@ -101,4 +101,20 @@ public static <T> Optional<T> createIfNullOptional(Supplier<Optional<T>> getter,
}
return instance;
}

private static <T> boolean isNullOrEmpty(Supplier<T> getter) {
return getter.get() == null ||
"".equals(getter.get()) ||
getter.get() == Optional.empty() ||
getter.get() instanceof Optional<?> &&
((Optional<?>) getter.get())
.filter(String.class::isInstance)
.map(String.class::cast)
.map(String::isBlank)
.orElse(false);
}

private static boolean isNullOrEmpty(String text) {
return text == null || text.isBlank();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,16 @@ void httpClient() throws IOException, KeyManagementException, NoSuchAlgorithmExc
}

private String getRssFeedAsString(String url) throws IOException, InterruptedException {
HttpRequest req = HttpRequest.newBuilder(URI.create(url))
var req = HttpRequest.newBuilder(URI.create(url))
.timeout(Duration.ofSeconds(25))
.GET()
.build();

HttpClient client = HttpClient.newBuilder()
var client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build();

HttpResponse<String> response = client.send(req, HttpResponse.BodyHandlers.ofString());
var response = client.send(req, HttpResponse.BodyHandlers.ofString());
return response.body();
}

Expand Down Expand Up @@ -740,7 +740,6 @@ void readRdfFeed() {
assertEquals("tandf: Journal of Web Librarianship: Table of Contents", item.getChannel().getTitle());
assertEquals("Table of Contents for Journal of Web Librarianship. List of articles from both the latest and ahead of print issues.", item.getChannel().getDescription());
assertEquals("en-US", item.getChannel().getLanguage().orElse(""));
assertEquals("tandf", item.getChannel().getCopyright().orElse(""));
assertEquals("I Can’t Get No Satis-Searching: Reassessing Discovery Layers in Academic Libraries Journal of Web Librarianship", item.getTitle().orElse(""));
assertEquals("Volume 18, Issue 1, January-March 2024, Page 1-14<br/>. <br/>", item.getDescription().orElse(""));
assertEquals("doi:10.1080/19322909.2024.2326687", item.getGuid().orElse(""));
Expand Down
86 changes: 84 additions & 2 deletions src/test/java/com/apptasticsoftware/rssreader/util/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import com.apptasticsoftware.rssreader.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static com.apptasticsoftware.rssreader.util.Mapper.mapInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class MapperTest {

Expand All @@ -27,6 +33,14 @@ void testMapBooleanFalse(String falseValue) {
assertEquals(false, item.getIsPermaLink().orElse(null));
}

@ParameterizedTest
@ValueSource(strings = {"Bad value", ""})
void testMapBooleanBadValue(String falseValue) {
Item item = new Item(new DateTime());
Mapper.mapBoolean(falseValue, item::setIsPermaLink);
assertNull(item.getIsPermaLink().orElse(null));
}

@ParameterizedTest
@ValueSource(strings = {"1", "-1", "0", "12345", "-12345"})
void testMapInt(String intTextValue) {
Expand Down Expand Up @@ -80,4 +94,72 @@ void testCreateIfNullOptional() {
assertEquals(200, channel.getImage().flatMap(Image::getHeight).orElse(0));
}

@Test
void testBadNumberLogging() {
var logger = Logger.getLogger("com.apptasticsoftware.rssreader.util");
logger.setLevel(Level.ALL);

var image = new Image();
Mapper.mapInteger("-", image::setHeight);
assertEquals(Optional.empty(), image.getHeight());

logger.setLevel(Level.OFF);
Mapper.mapInteger("-", image::setHeight);
assertEquals(Optional.empty(), image.getHeight());

Mapper.mapInteger("", image::setHeight);
assertEquals(Optional.empty(), image.getHeight());

Mapper.mapInteger(null, image::setHeight);
assertEquals(Optional.empty(), image.getHeight());
}

@ParameterizedTest
@MethodSource("mapIfEmptyParameters")
void testMapIfEmpty(TestObject testObject, String value, String expected) {
Mapper.mapIfEmpty(value, testObject::getText, testObject::setText);
assertEquals(expected, testObject.getText());
}

@ParameterizedTest
@MethodSource("mapIfEmptyParameters")
void testOptionalMapIfEmpty(TestObject testObject, String value, String expected) {
Mapper.mapIfEmpty(value, testObject::getOptionalText, testObject::setText);
assertEquals(expected, testObject.getText());
}

private static Stream<Arguments> mapIfEmptyParameters() {
return Stream.of(
Arguments.of(new TestObject(null), "value", "value"),
Arguments.of(new TestObject(""), "value", "value"),
Arguments.of(new TestObject(null), "", null),
Arguments.of(new TestObject(null), null, null),
Arguments.of(new TestObject(""), "", ""),
Arguments.of(new TestObject(""), null, ""),
Arguments.of(new TestObject("value"), "other value", "value")
);
}


static class TestObject {
private String text;

public TestObject(String value) {
text = value;
}

public void setText(String value) {
text = value;
}

public String getText() {
return text;
}

public Optional<String> getOptionalText() {
return Optional.ofNullable(text);
}

}

}

0 comments on commit f40b127

Please sign in to comment.