Skip to content

Commit

Permalink
Added ScrapingCacheRepository [#301]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce authored and BRUCELLA2 committed Jul 18, 2020
1 parent 28f26b7 commit 49ffc3e
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 15 deletions.
@@ -0,0 +1,44 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.repositories.scraping;

import org.comixed.model.scraping.ScrapingCache;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
* <code>ScrapingCacheRepository</code> handls storing and fetching instances of {@link
* ScrapingCache}.
*
* @author Darryl L. Pierce
*/
@Repository
public interface ScrapingCacheRepository extends JpaRepository<ScrapingCache, Long> {
/**
* Retrieves the cache entry for the given source with the given key.
*
* @param source the source
* @param key the key
* @return the entry, or null
*/
@Query("SELECT c FROM ScrapingCache c WHERE c.source = :source AND c.cacheKey = :key")
ScrapingCache getFromCache(@Param("source") String source, @Param("key") String key);
}
@@ -0,0 +1,110 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.repositories.scraping;

import static junit.framework.TestCase.*;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import org.comixed.model.scraping.ScrapingCache;
import org.comixed.model.scraping.ScrapingCacheEntry;
import org.comixed.repositories.RepositoryContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RepositoryContext.class)
@TestPropertySource(locations = "classpath:application.properties")
@DatabaseSetup("classpath:test-database.xml")
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
public class ScrapingCacheRepositoryTest {
private static final String TEST_SOURCE = "ComicVine";
private static final String TEST_CACHE_KEY = "volumes[Iron Man]";
private static final String TEST_NEW_ENTRY_CACHE_KEY = "volumes[The Avengers]";

@Autowired private ScrapingCacheRepository scrapingCacheRepository;

@Test
public void testGetFromCacheNonexistent() {
final ScrapingCache result =
this.scrapingCacheRepository.getFromCache(TEST_SOURCE, TEST_CACHE_KEY.substring(1));

assertNull(result);
}

@Test
public void testGetFromCache() {
final ScrapingCache result =
this.scrapingCacheRepository.getFromCache(TEST_SOURCE, TEST_CACHE_KEY);

assertNotNull(result);
assertEquals(TEST_SOURCE, result.getSource());
assertEquals(TEST_CACHE_KEY, result.getCacheKey());
assertFalse(result.getEntries().isEmpty());
for (int index = 0; index < result.getEntries().size(); index++) {
final ScrapingCacheEntry entry = result.getEntries().get(index);
assertEquals(index, entry.getEntryNumber().intValue());
assertEquals(String.format("cached-value-%d", index), entry.getEntryValue());
}
}

@Test
public void testSaveToCache() {
final ScrapingCache entry = new ScrapingCache();
entry.setSource(TEST_SOURCE);
entry.setCacheKey(TEST_NEW_ENTRY_CACHE_KEY);
for (int index = 0; index < 100; index++) {
final ScrapingCacheEntry value = new ScrapingCacheEntry();
value.setEntryNumber(index);
value.setEntryValue(String.valueOf(System.currentTimeMillis()));
value.setScrapingCache(entry);
entry.getEntries().add(value);
}

this.scrapingCacheRepository.save(entry);

final ScrapingCache result =
this.scrapingCacheRepository.getFromCache(TEST_SOURCE, TEST_NEW_ENTRY_CACHE_KEY);

assertNotNull(result);
assertEquals(TEST_SOURCE, result.getSource());
assertEquals(TEST_NEW_ENTRY_CACHE_KEY, result.getCacheKey());
assertFalse(result.getEntries().isEmpty());
for (int index = 0; index < entry.getEntries().size(); index++) {
assertEquals(index, result.getEntries().get(index).getEntryNumber().intValue());
assertEquals(
entry.getEntries().get(index).getEntryValue(),
result.getEntries().get(index).getEntryValue());
}
}
}
59 changes: 44 additions & 15 deletions comixed-library/src/test/resources/test-database.xml
Expand Up @@ -483,21 +483,50 @@
negative="false"
operator="1"
value="Image"/>
<comic_vine_volume_query_cache id="1000"
series_name="Cached Series"
sequence="0"
created="2019-11-01 09:14:00"
content="First entry"/>
<comic_vine_volume_query_cache id="1001"
series_name="Cached Series"
sequence="1"
created="2019-11-01 09:14:00"
content="Second entry"/>
<comic_vine_volume_query_cache id="1002"
series_name="Cached Series"
sequence="2"
created="2019-11-01 09:14:00"
content="Third entry"/>
<scraping_cache id="1000"
source="ComicVine"
cache_key="volumes[Iron Man]"
date_fetched="[now]"/>
<scraping_cache_entries id="1000"
scraping_cache_id="1000"
entry_number="0"
entry_value="cached-value-0"/>
<scraping_cache_entries id="1001"
scraping_cache_id="1000"
entry_number="1"
entry_value="cached-value-1"/>
<scraping_cache_entries id="1002"
scraping_cache_id="1000"
entry_number="2"
entry_value="cached-value-2"/>
<scraping_cache_entries id="1003"
scraping_cache_id="1000"
entry_number="3"
entry_value="cached-value-3"/>
<scraping_cache_entries id="1004"
scraping_cache_id="1000"
entry_number="4"
entry_value="cached-value-4"/>
<scraping_cache_entries id="1005"
scraping_cache_id="1000"
entry_number="5"
entry_value="cached-value-5"/>
<scraping_cache_entries id="1006"
scraping_cache_id="1000"
entry_number="6"
entry_value="cached-value-6"/>
<scraping_cache_entries id="1007"
scraping_cache_id="1000"
entry_number="7"
entry_value="cached-value-7"/>
<scraping_cache_entries id="1008"
scraping_cache_id="1000"
entry_number="8"
entry_value="cached-value-8"/>
<scraping_cache_entries id="1009"
scraping_cache_id="1000"
entry_number="9"
entry_value="cached-value-9"/>
<publishers ID="1000"
NAME="Super Soft Publishing"
COMIC_VINE_ID="23173"
Expand Down
@@ -1,3 +1,21 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.service.library;

import static junit.framework.TestCase.*;
Expand Down

0 comments on commit 49ffc3e

Please sign in to comment.