diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/AttributeQuery.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/AttributeQuery.java index 0a75fb3b9..8ab7b7024 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/AttributeQuery.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/AttributeQuery.java @@ -6,7 +6,9 @@ @Data @Entity -@Table(indexes = @Index(columnList = "attributeName, statistics_category_id", unique = true)) +@Table( + name = "attribute_query", + indexes = @Index(columnList = "attributeName, statistics_category_id", unique = true)) public class AttributeQuery { @Id private Long id; diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtKBStatisticsEntry.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtKBStatisticsEntry.java index 0ef284fee..247a54b30 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtKBStatisticsEntry.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtKBStatisticsEntry.java @@ -6,7 +6,7 @@ @Data @Entity -@Table(name = "uniprotkb_statistics_entry") +@Table(name = "uniprotkb_statistics_entry", indexes = @Index(columnList = "release_name")) public class UniProtKBStatisticsEntry { @Id private Long id; private String attributeName; @@ -21,7 +21,7 @@ public class UniProtKBStatisticsEntry { @ManyToOne @JoinColumn(name = "release_name") - private UniProtRelease releaseName; + private UniProtRelease uniProtRelease; @Enumerated(EnumType.STRING) private EntryType entryType; diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtRelease.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtRelease.java index a0b6bd1f6..0422a7017 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtRelease.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/entity/UniProtRelease.java @@ -2,9 +2,7 @@ import java.util.Date; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; import lombok.Data; @@ -12,6 +10,13 @@ @Entity @Table(name = "uniprot_release") public class UniProtRelease { - @Id private String id; + @Id private int id; + + @Temporal(TemporalType.DATE) private Date date; + + @Enumerated(EnumType.STRING) + private EntryType entryType; + + private String name; } diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/mapper/StatisticsMapper.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/mapper/StatisticsMapper.java index 99f4068ff..14d8b4d45 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/mapper/StatisticsMapper.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/mapper/StatisticsMapper.java @@ -54,8 +54,8 @@ public StatisticsModuleStatisticsAttribute map( public StatisticsModuleStatisticsHistory mapHistory(UniProtKBStatisticsEntry entry) { return StatisticsModuleStatisticsHistoryImpl.builder() .statisticsType(map(entry.getEntryType())) - .releaseName(entry.getReleaseName().getId()) - .releaseDate(entry.getReleaseName().getDate()) + .releaseName(entry.getUniProtRelease().getName()) + .releaseDate(entry.getUniProtRelease().getDate()) .valueCount(entry.getValueCount()) .entryCount(entry.getEntryCount()) .build(); diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepository.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepository.java index fe3769ac7..facbf548b 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepository.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepository.java @@ -11,5 +11,6 @@ @Repository @Primary public interface AttributeQueryRepository extends JpaRepository { - Optional findByStatisticsCategoryAndAttributeNameIgnoreCase(StatisticsCategory statisticsCategory, String attributeName); + Optional findByStatisticsCategoryAndAttributeNameIgnoreCase( + StatisticsCategory statisticsCategory, String attributeName); } diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepository.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepository.java index 794387f91..8d83f913f 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepository.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepository.java @@ -15,21 +15,13 @@ @Primary public interface UniProtKBStatisticsEntryRepository extends JpaRepository { - List findAllByReleaseNameAndEntryType( - UniProtRelease releaseName, EntryType entryType); - - List findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - UniProtRelease uniProtRelease, - EntryType entryType, - Collection statisticsCategory); - List findAllByAttributeNameIgnoreCaseAndEntryType( String attributeName, EntryType entryType); List findAllByAttributeNameIgnoreCase(String attributeName); - List findAllByReleaseName(UniProtRelease releaseName); + List findAllByUniProtRelease(UniProtRelease uniProtRelease); - List findAllByReleaseNameAndStatisticsCategoryIn( + List findAllByUniProtReleaseAndStatisticsCategoryIn( UniProtRelease releaseName, Collection statisticsCategory); } diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepository.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepository.java index 22dcdf9b8..685f83ee6 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepository.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepository.java @@ -7,11 +7,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.uniprot.api.support.data.statistics.entity.EntryType; import org.uniprot.api.support.data.statistics.entity.UniProtRelease; @Repository @Primary -public interface UniProtReleaseRepository extends JpaRepository { - @Query("SELECT MAX (ur.date) from UniProtRelease ur where ur.id { + @Query("SELECT MAX (ur.date) from UniProtRelease ur where ur.name findPreviousReleaseDate(String currentRelease); + + Optional findByNameAndEntryType(String name, EntryType entryType); } diff --git a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceImpl.java b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceImpl.java index c148894e4..24ab44465 100644 --- a/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceImpl.java +++ b/support-data-rest/src/main/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceImpl.java @@ -46,20 +46,9 @@ public StatisticsServiceImpl( @Override public List findAllByVersionAndStatisticTypeAndCategoryIn( String version, String statisticType, Set categories) { - List entries; - if (categories.isEmpty()) { - entries = - statisticsEntryRepository.findAllByReleaseNameAndEntryType( - getRelease(version), - statisticsMapper.map(getStatisticType(statisticType))); - } else { - entries = - statisticsEntryRepository - .findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - getRelease(version), - statisticsMapper.map(getStatisticType(statisticType)), - getCategories(categories)); - } + List entries = + getAllEntriesByVersionAndStatisticTypeAndCategoryIn( + version, statisticType, categories); return entries.stream() .collect(Collectors.groupingBy(UniProtKBStatisticsEntry::getStatisticsCategory)) .entrySet() @@ -68,15 +57,6 @@ public List findAllByVersionAndStatisticType .collect(Collectors.toList()); } - private UniProtRelease getRelease(String version) { - return releaseRepository - .findById(version) - .orElseThrow( - () -> - new IllegalArgumentException( - String.format("Invalid Release Version: %s", version))); - } - @Override public List findAllByAttributeAndStatisticType( String attribute, String statisticType) { @@ -98,14 +78,15 @@ public List findAllByAttributeAndStatisticTyp @Override public Collection findAllByVersionAndCategoryIn( String version, Set categories) { - List entries; - if (categories.isEmpty()) { - entries = statisticsEntryRepository.findAllByReleaseName(getRelease(version)); - } else { - entries = - statisticsEntryRepository.findAllByReleaseNameAndStatisticsCategoryIn( - getRelease(version), getCategories(categories)); + List entries = new LinkedList<>(); + + for (StatisticsModuleStatisticsType statisticsType : + StatisticsModuleStatisticsType.values()) { + entries.addAll( + getAllEntriesByVersionAndStatisticTypeAndCategoryIn( + version, statisticsType.name(), categories)); } + return entries.stream() .collect(Collectors.groupingBy(UniProtKBStatisticsEntry::getStatisticsCategory)) .entrySet() @@ -115,6 +96,34 @@ public Collection findAllByVersionAndCategor .collect(Collectors.toList()); } + private List getAllEntriesByVersionAndStatisticTypeAndCategoryIn( + String version, String statisticType, Set categories) { + List entries; + StatisticsModuleStatisticsType statisticsModuleStatisticsType = + getStatisticType(statisticType); + EntryType entryType = statisticsMapper.map(statisticsModuleStatisticsType); + UniProtRelease release = getRelease(version, entryType); + if (categories.isEmpty()) { + entries = statisticsEntryRepository.findAllByUniProtRelease(release); + } else { + entries = + statisticsEntryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + release, getCategories(categories)); + } + return entries; + } + + private UniProtRelease getRelease(String version, EntryType entryType) { + return releaseRepository + .findByNameAndEntryType(version, entryType) + .orElseThrow( + () -> + new IllegalArgumentException( + String.format( + "Invalid Release Version: %s or entry type: %s", + version, entryType))); + } + private Map.Entry> groupEntries( Map.Entry> entry) { List groupedEntries = @@ -143,7 +152,7 @@ private UniProtKBStatisticsEntry mapToSingleEntry( .mapToLong(UniProtKBStatisticsEntry::getEntryCount) .sum()); uniProtKBStatisticsEntry.setDescription(firstEntry.getDescription()); - uniProtKBStatisticsEntry.setReleaseName(firstEntry.getReleaseName()); + uniProtKBStatisticsEntry.setUniProtRelease(firstEntry.getUniProtRelease()); return uniProtKBStatisticsEntry; } @@ -187,7 +196,8 @@ private Set getCategories(Set categories) { private String getAttributeQuery(UniProtKBStatisticsEntry entry) { Optional attributeQuery = - attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(entry.getStatisticsCategory(),entry.getAttributeName()); + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + entry.getStatisticsCategory(), entry.getAttributeName()); return attributeQuery.map(query -> prepareQuery(query, entry)).orElse(""); } @@ -195,7 +205,7 @@ private String prepareQuery(AttributeQuery query, UniProtKBStatisticsEntry entry String result = query.getQuery(); if (result.contains(PREVIOUS_RELEASE_DATE)) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - String currentRelease = entry.getReleaseName().getId(); + String currentRelease = entry.getUniProtRelease().getName(); Date previousReleaseDate = releaseRepository .findPreviousReleaseDate(currentRelease) diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/TestEntityGeneratorUtil.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/TestEntityGeneratorUtil.java index b691398de..08394f2d2 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/TestEntityGeneratorUtil.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/TestEntityGeneratorUtil.java @@ -63,14 +63,15 @@ public class TestEntityGeneratorUtil { }; public static final UniProtRelease[] RELEASES = new UniProtRelease[] { - createRelease(REL_0, DATES[0]), - createRelease(REL_1, DATES[1]), - createRelease(REL_2, DATES[2]) + createRelease(0, REL_0, DATES[0]), + createRelease(1, REL_1, DATES[1]), + createRelease(2, REL_2, DATES[2]) }; - private static UniProtRelease createRelease(String name, Date date) { + private static UniProtRelease createRelease(int id, String name, Date date) { UniProtRelease release = new UniProtRelease(); - release.setId(name); + release.setId(id); + release.setName(name); release.setDate(date); return release; } @@ -110,7 +111,7 @@ private static UniProtKBStatisticsEntry createStatisticsEntry(int index) { uniprotkbStatisticsEntry.setValueCount(VALUE_COUNTS[index]); uniprotkbStatisticsEntry.setEntryCount(ENTRY_COUNTS[index]); uniprotkbStatisticsEntry.setDescription(DESCRIPTIONS[index]); - uniprotkbStatisticsEntry.setReleaseName( + uniprotkbStatisticsEntry.setUniProtRelease( Set.of(0, 1, 3, 4, 5).contains(index) ? RELEASES[0] : RELEASES[1]); uniprotkbStatisticsEntry.setEntryType(ENTRY_TYPES[index]); return uniprotkbStatisticsEntry; diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/controller/StatisticsControllerIT.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/controller/StatisticsControllerIT.java index aeba042b6..c4ed9f61f 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/controller/StatisticsControllerIT.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/controller/StatisticsControllerIT.java @@ -39,7 +39,7 @@ static void postgreSQLProperties(DynamicPropertyRegistry registry) { @Test void getByReleaseAndType() throws Exception { this.mockMvc - .perform(get("/statistics/releases/2021_03/reviewed")) + .perform(get("/statistics/releases/2021_02/reviewed")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(3))) @@ -52,7 +52,7 @@ void getByReleaseAndType() throws Exception { .andExpect( jsonPath( "$.results[0].items[0].query", - is("(reviewed:true) AND (previous_release_date:2021-03-25)"))) + is("(reviewed:true) AND (previous_release_date:2021-01-25)"))) .andExpect(jsonPath("$.results[0].items[0].count", is(329))) .andExpect(jsonPath("$.results[0].items[0].entryCount", is(254))) .andExpect(jsonPath("$.results[1].categoryName", is("TOP_ORGANISM"))) @@ -91,7 +91,7 @@ void getByReleaseAndType() throws Exception { @Test void getByRelease() throws Exception { this.mockMvc - .perform(get("/statistics/releases/2021_03")) + .perform(get("/statistics/releases/2021_02")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(3))) @@ -104,7 +104,7 @@ void getByRelease() throws Exception { .andExpect( jsonPath( "$.results[0].items[0].query", - is("(previous_release_date:2021-03-25)"))) + is("(previous_release_date:2021-01-25)"))) .andExpect(jsonPath("$.results[0].items[0].count", is(329))) .andExpect(jsonPath("$.results[0].items[0].entryCount", is(254))) .andExpect(jsonPath("$.results[1].categoryName", is("TOP_ORGANISM"))) @@ -137,7 +137,7 @@ void getByRelease() throws Exception { @Test void getByReleaseAndTypeAndSingleCategory() throws Exception { this.mockMvc - .perform(get("/statistics/releases/2021_03/reviewed?categories=EUKARYOTA")) + .perform(get("/statistics/releases/2021_02/reviewed?categories=EUKARYOTA")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(1))) @@ -165,7 +165,7 @@ void getByReleaseAndTypeAndSingleCategory() throws Exception { @Test void getByReleaseAndSingleCategory() throws Exception { this.mockMvc - .perform(get("/statistics/releases/2021_03?categories=EUKARYOTA")) + .perform(get("/statistics/releases/2021_02?categories=EUKARYOTA")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(1))) @@ -189,7 +189,7 @@ void getByReleaseAndTypeAndMultipleCategories() throws Exception { this.mockMvc .perform( get( - "/statistics/releases/2021_03/reviewed?categories=EUKARYOTA,TOP_ORGANISM")) + "/statistics/releases/2021_02/reviewed?categories=EUKARYOTA,TOP_ORGANISM")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(2))) @@ -229,7 +229,7 @@ void getByReleaseAndTypeAndMultipleCategories() throws Exception { @Test void getByReleaseAndMultipleCategories() throws Exception { this.mockMvc - .perform(get("/statistics/releases/2021_03?categories=EUKARYOTA,TOP_ORGANISM")) + .perform(get("/statistics/releases/2021_02?categories=EUKARYOTA,TOP_ORGANISM")) .andDo(log()) .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(2))) @@ -268,8 +268,8 @@ void getAllByAttributeAndStatisticsType() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(1))) .andExpect(jsonPath("$.results[0].statisticsType", is("REVIEWED"))) - .andExpect(jsonPath("$.results[0].releaseName", is("2021_03"))) - .andExpect(jsonPath("$.results[0].releaseDate", is("2021-05-25"))) + .andExpect(jsonPath("$.results[0].releaseName", is("2021_02"))) + .andExpect(jsonPath("$.results[0].releaseDate", is("2021-03-25"))) .andExpect(jsonPath("$.results[0].valueCount", is(35360))) .andExpect(jsonPath("$.results[0].entryCount", is(35360))); } @@ -311,13 +311,13 @@ void getAllByAttributeAndAttributeType_withoutAttributeType() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$.results.size()", is(2))) .andExpect(jsonPath("$.results[0].statisticsType", is("REVIEWED"))) - .andExpect(jsonPath("$.results[0].releaseName", is("2021_03"))) - .andExpect(jsonPath("$.results[0].releaseDate", is("2021-05-25"))) + .andExpect(jsonPath("$.results[0].releaseName", is("2021_02"))) + .andExpect(jsonPath("$.results[0].releaseDate", is("2021-03-25"))) .andExpect(jsonPath("$.results[0].valueCount", is(35360))) .andExpect(jsonPath("$.results[0].entryCount", is(35360))) .andExpect(jsonPath("$.results[1].statisticsType", is("UNREVIEWED"))) - .andExpect(jsonPath("$.results[1].releaseName", is("2021_03"))) - .andExpect(jsonPath("$.results[1].releaseDate", is("2021-05-25"))) + .andExpect(jsonPath("$.results[1].releaseName", is("2021_02"))) + .andExpect(jsonPath("$.results[1].releaseDate", is("2021-03-25"))) .andExpect(jsonPath("$.results[1].valueCount", is(12793422))) .andExpect(jsonPath("$.results[1].entryCount", is(12793422))); } diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepositoryTest.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepositoryTest.java index 6b402f81c..999377c56 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepositoryTest.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/AttributeQueryRepositoryTest.java @@ -55,7 +55,8 @@ void setUp() { @Test void findByAttributeName_whenExist() { Optional result = - attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORY, ATTRIBUTES[1]); + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORY, ATTRIBUTES[1]); AttributeQuery attributeQuery = result.get(); assertEquals(IDS[1], attributeQuery.getId()); @@ -65,7 +66,8 @@ void findByAttributeName_whenExist() { @Test void findByAttributeName_whenExistAndCaseDiff() { Optional result = - attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORY, ATTRIBUTES[1].toUpperCase()); + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORY, ATTRIBUTES[1].toUpperCase()); AttributeQuery attributeQuery = result.get(); assertEquals(IDS[1], attributeQuery.getId()); @@ -75,7 +77,8 @@ void findByAttributeName_whenExistAndCaseDiff() { @Test void findByAttributeName_whenAbsent() { Optional result = - attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORY, "random"); + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORY, "random"); assertFalse(result.isPresent()); } diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepositoryTest.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepositoryTest.java index 785a59237..3cee7645f 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepositoryTest.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtKBStatisticsEntryRepositoryTest.java @@ -45,23 +45,10 @@ void setUp() { Arrays.stream(RELEASES).forEach(entityManager::persist); } - @Test - void findAllByReleaseNameAndEntryType() { - List results = - entryRepository.findAllByReleaseNameAndEntryType(RELEASES[0], SWISSPROT); - - assertThat( - results, - containsInAnyOrder( - STATISTICS_ENTRIES[0], - STATISTICS_ENTRIES[1], - STATISTICS_ENTRIES[3], - STATISTICS_ENTRIES[4])); - } - @Test void findAllByReleaseName() { - List results = entryRepository.findAllByReleaseName(RELEASES[0]); + List results = + entryRepository.findAllByUniProtRelease(RELEASES[0]); assertThat( results, @@ -76,14 +63,15 @@ void findAllByReleaseName() { @Test void findAllByReleaseNameAndEntryType_whenNoMatch() { List results = - entryRepository.findAllByReleaseNameAndEntryType(RELEASES[2], TREMBL); + entryRepository.findAllByUniProtRelease(RELEASES[2]); assertThat(results, empty()); } @Test void findAllByReleaseName_whenNoMatch() { - List results = entryRepository.findAllByReleaseName(RELEASES[2]); + List results = + entryRepository.findAllByUniProtRelease(RELEASES[2]); assertThat(results, empty()); } @@ -91,10 +79,8 @@ void findAllByReleaseName_whenNoMatch() { @Test void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn() { List results = - entryRepository.findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - RELEASES[0], - SWISSPROT, - List.of(STATISTICS_CATEGORIES[0], STATISTICS_CATEGORIES[1])); + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + RELEASES[0], List.of(STATISTICS_CATEGORIES[0], STATISTICS_CATEGORIES[1])); assertThat( results, @@ -105,7 +91,7 @@ void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn() { @Test void findAllByReleaseNameAndStatisticsCategoryIdIn() { List results = - entryRepository.findAllByReleaseNameAndStatisticsCategoryIn( + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( RELEASES[0], List.of(STATISTICS_CATEGORIES[0], STATISTICS_CATEGORIES[1])); assertThat( @@ -117,8 +103,8 @@ void findAllByReleaseNameAndStatisticsCategoryIdIn() { @Test void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn_whenSingleCategoryPassed() { List results = - entryRepository.findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - RELEASES[0], SWISSPROT, List.of(STATISTICS_CATEGORIES[0])); + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + RELEASES[0], List.of(STATISTICS_CATEGORIES[0])); assertThat(results, containsInAnyOrder(STATISTICS_ENTRIES[0], STATISTICS_ENTRIES[1])); } @@ -126,7 +112,7 @@ void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn_whenSingleCategor @Test void findAllByReleaseNameAndStatisticsCategoryIdIn_whenSingleCategoryPassed() { List results = - entryRepository.findAllByReleaseNameAndStatisticsCategoryIn( + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( RELEASES[0], List.of(STATISTICS_CATEGORIES[0])); assertThat(results, containsInAnyOrder(STATISTICS_ENTRIES[0], STATISTICS_ENTRIES[1])); @@ -135,8 +121,8 @@ void findAllByReleaseNameAndStatisticsCategoryIdIn_whenSingleCategoryPassed() { @Test void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn_whenNoCategoryPassed() { List results = - entryRepository.findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - RELEASES[0], SWISSPROT, Collections.emptyList()); + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + RELEASES[0], Collections.emptyList()); assertThat(results, empty()); } @@ -144,7 +130,7 @@ void findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIdIn_whenNoCategoryPas @Test void findAllByReleaseNameAndStatisticsCategoryIdIn_whenNoCategoryPassed() { List results = - entryRepository.findAllByReleaseNameAndStatisticsCategoryIn( + entryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( RELEASES[2], Collections.emptyList()); assertThat(results, empty()); diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepositoryTest.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepositoryTest.java index 7cf828768..3586b0825 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepositoryTest.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/repository/UniProtReleaseRepositoryTest.java @@ -4,9 +4,11 @@ import java.time.Instant; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; import java.util.Optional; +import org.apache.commons.lang.time.DateUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -31,13 +33,14 @@ class UniProtReleaseRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private UniProtReleaseRepository uniProtReleaseRepository; - private static final String[] IDS = new String[] {"2023_05", "2023_06", "2024_01", "2024_02"}; + private static final int[] IDS = new int[] {0, 1, 2, 3}; + private static final String[] NAMES = new String[] {"2023_05", "2023_06", "2024_01", "2024_02"}; private static final Date[] DATES = new Date[] { - Date.from(Instant.ofEpochMilli(100L)), - Date.from(Instant.ofEpochMilli(200L)), - Date.from(Instant.ofEpochMilli(300L)), - Date.from(Instant.ofEpochMilli(400L)) + DateUtils.truncate(Date.from(Instant.ofEpochMilli(100L)), Calendar.DATE), + DateUtils.truncate(Date.from(Instant.ofEpochMilli(200L)), Calendar.DATE), + DateUtils.truncate(Date.from(Instant.ofEpochMilli(300L)), Calendar.DATE), + DateUtils.truncate(Date.from(Instant.ofEpochMilli(400L)), Calendar.DATE) }; private static final UniProtRelease[] UNIPROT_RELEASES = new UniProtRelease[4]; @@ -46,6 +49,7 @@ void setUp() { for (int i = 0; i < 4; i++) { UniProtRelease uniProtRelease = new UniProtRelease(); uniProtRelease.setId(IDS[i]); + uniProtRelease.setName(NAMES[i]); uniProtRelease.setDate(DATES[i]); UNIPROT_RELEASES[i] = uniProtRelease; } diff --git a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceTest.java b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceTest.java index 9eb64f1d9..4479eea9b 100644 --- a/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceTest.java +++ b/support-data-rest/src/test/java/org/uniprot/api/support/data/statistics/service/StatisticsServiceTest.java @@ -8,6 +8,8 @@ import static org.mockito.Mockito.when; import static org.uniprot.api.support.data.statistics.TestEntityGeneratorUtil.*; import static org.uniprot.api.support.data.statistics.entity.EntryType.SWISSPROT; +import static org.uniprot.api.support.data.statistics.entity.EntryType.TREMBL; +import static org.uniprot.api.support.data.statistics.model.StatisticsModuleStatisticsType.*; import java.time.Instant; import java.util.*; @@ -33,10 +35,24 @@ class StatisticsServiceTest { private static final String STATISTIC_TYPE = "reviewed"; private static final EntryType ENTRY_TYPE = EntryType.SWISSPROT; - private static final StatisticsModuleStatisticsType STATISTIC_TYPE_ENUM = - StatisticsModuleStatisticsType.REVIEWED; + private static final StatisticsModuleStatisticsType STATISTIC_TYPE_ENUM = REVIEWED; private static final String VERSION = "version"; - private static final UniProtRelease UNI_PROT_RELEASE = new UniProtRelease(); + private static final UniProtRelease UNI_PROT_RELEASE_SWISSPROT; + + static { + UniProtRelease uniProtRelease = new UniProtRelease(); + uniProtRelease.setEntryType(ENTRY_TYPE); + UNI_PROT_RELEASE_SWISSPROT = uniProtRelease; + } + + private static final UniProtRelease UNI_PROT_RELEASE_TREMBL; + + static { + UniProtRelease uniProtRelease = new UniProtRelease(); + uniProtRelease.setEntryType(TREMBL); + UNI_PROT_RELEASE_TREMBL = uniProtRelease; + } + public static final String NAME_0 = "name0"; private static final Date PREVIOUS_RELEASE_DATE = Date.from(Instant.ofEpochMilli(1653476723000L)); @@ -92,6 +108,7 @@ void setUp() { .searchField(SEARCH_FIELDS[2]) .build(); lenient().when(statisticsMapper.map(STATISTIC_TYPE_ENUM)).thenReturn(ENTRY_TYPE); + lenient().when(statisticsMapper.map(UNREVIEWED)).thenReturn(TREMBL); lenient() .when(statisticsMapper.map(STATISTICS_ENTRIES[0], QUERIES[0])) .thenReturn(statisticsModuleStatisticsAttribute0); @@ -125,22 +142,35 @@ void setUp() { getCommonEntry(STATISTICS_ENTRIES[3]), QUERIES_COMMON[3])) .thenReturn(statisticsModuleStatisticsAttribute3); lenient() - .when(releaseRepository.findById(VERSION)) - .thenReturn(Optional.of(UNI_PROT_RELEASE)); + .when(releaseRepository.findByNameAndEntryType(VERSION, ENTRY_TYPE)) + .thenReturn(Optional.of(UNI_PROT_RELEASE_SWISSPROT)); + lenient() + .when(releaseRepository.findByNameAndEntryType(VERSION, TREMBL)) + .thenReturn(Optional.of(UNI_PROT_RELEASE_TREMBL)); lenient() - .when(attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORIES[1],ENTRY_NAMES[0])) + .when( + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORIES[1], ENTRY_NAMES[0])) .thenReturn(Optional.of(ATTRIBUTE_QUERIES[0])); lenient() - .when(attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORIES[0],ENTRY_NAMES[0])) + .when( + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORIES[0], ENTRY_NAMES[0])) .thenReturn(Optional.of(ATTRIBUTE_QUERIES[0])); lenient() - .when(attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORIES[0], ENTRY_NAMES[1])) + .when( + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORIES[0], ENTRY_NAMES[1])) .thenReturn(Optional.of(ATTRIBUTE_QUERIES[1])); lenient() - .when(attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORIES[2], ENTRY_NAMES[1])) + .when( + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORIES[2], ENTRY_NAMES[1])) .thenReturn(Optional.of(ATTRIBUTE_QUERIES[1])); lenient() - .when(attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase(STATISTICS_CATEGORIES[0], ENTRY_NAMES[5])) + .when( + attributeQueryRepository.findByStatisticsCategoryAndAttributeNameIgnoreCase( + STATISTICS_CATEGORIES[0], ENTRY_NAMES[5])) .thenReturn(Optional.empty()); lenient() .when(releaseRepository.findPreviousReleaseDate(REL_0)) @@ -154,14 +184,13 @@ private UniProtKBStatisticsEntry getCommonEntry(UniProtKBStatisticsEntry statist entry.setValueCount(statisticsEntry.getValueCount()); entry.setEntryCount(statisticsEntry.getEntryCount()); entry.setDescription(statisticsEntry.getDescription()); - entry.setReleaseName(statisticsEntry.getReleaseName()); + entry.setUniProtRelease(statisticsEntry.getUniProtRelease()); return entry; } @Test void findAllByVersionAndStatisticTypeAndCategoryIn_whenEmptyListOfCategoriesPassed() { - when(statisticsEntryRepository.findAllByReleaseNameAndEntryType( - UNI_PROT_RELEASE, ENTRY_TYPE)) + when(statisticsEntryRepository.findAllByUniProtRelease(UNI_PROT_RELEASE_SWISSPROT)) .thenReturn( List.of( STATISTICS_ENTRIES[0], @@ -183,7 +212,7 @@ void findAllByVersionAndStatisticTypeAndCategoryIn_whenEmptyListOfCategoriesPass @Test void findAllByVersionAndCategoryIn_whenEmptyListOfCategoriesPassed() { - when(statisticsEntryRepository.findAllByReleaseName(UNI_PROT_RELEASE)) + when(statisticsEntryRepository.findAllByUniProtRelease(UNI_PROT_RELEASE_SWISSPROT)) .thenReturn( List.of( STATISTICS_ENTRIES[0], @@ -208,9 +237,8 @@ void findAllByVersionAndStatisticTypeAndCategoryIn() { .thenReturn(Optional.of(statisticsCategory0)); when(statisticsCategoryRepository.findByCategoryIgnoreCase(CATEGORY_1)) .thenReturn(Optional.of(statisticsCategory1)); - when(statisticsEntryRepository.findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - UNI_PROT_RELEASE, - ENTRY_TYPE, + when(statisticsEntryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + UNI_PROT_RELEASE_SWISSPROT, Set.of(statisticsCategory0, statisticsCategory1))) .thenReturn( List.of( @@ -234,8 +262,9 @@ void findAllByVersionAndCategoryIn() { .thenReturn(Optional.of(statisticsCategory0)); when(statisticsCategoryRepository.findByCategoryIgnoreCase(CATEGORY_1)) .thenReturn(Optional.of(statisticsCategory1)); - when(statisticsEntryRepository.findAllByReleaseNameAndStatisticsCategoryIn( - UNI_PROT_RELEASE, Set.of(statisticsCategory0, statisticsCategory1))) + when(statisticsEntryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + UNI_PROT_RELEASE_SWISSPROT, + Set.of(statisticsCategory0, statisticsCategory1))) .thenReturn( List.of( STATISTICS_ENTRIES[0], @@ -256,8 +285,8 @@ void findAllByVersionAndCategoryIn() { void findAllByVersionAndStatisticTypeAndCategoryIn_whenSingleCategoryIsPassed() { when(statisticsCategoryRepository.findByCategoryIgnoreCase(CATEGORY_0)) .thenReturn(Optional.of(statisticsCategory0)); - when(statisticsEntryRepository.findAllByReleaseNameAndEntryTypeAndStatisticsCategoryIn( - UNI_PROT_RELEASE, ENTRY_TYPE, Set.of(statisticsCategory0))) + when(statisticsEntryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + UNI_PROT_RELEASE_SWISSPROT, Set.of(statisticsCategory0))) .thenReturn(List.of(STATISTICS_ENTRIES[0], STATISTICS_ENTRIES[1])); Collection results = @@ -271,8 +300,8 @@ void findAllByVersionAndStatisticTypeAndCategoryIn_whenSingleCategoryIsPassed() void findAllByVersionAndCategoryIn_whenSingleCategoryIsPassed() { when(statisticsCategoryRepository.findByCategoryIgnoreCase(CATEGORY_0)) .thenReturn(Optional.of(statisticsCategory0)); - when(statisticsEntryRepository.findAllByReleaseNameAndStatisticsCategoryIn( - UNI_PROT_RELEASE, Set.of(statisticsCategory0))) + when(statisticsEntryRepository.findAllByUniProtReleaseAndStatisticsCategoryIn( + UNI_PROT_RELEASE_SWISSPROT, Set.of(statisticsCategory0))) .thenReturn(List.of(STATISTICS_ENTRIES[0], STATISTICS_ENTRIES[1])); Collection results = diff --git a/support-data-rest/src/test/resources/init.sql b/support-data-rest/src/test/resources/init.sql index e5156a97e..e16cc9427 100644 --- a/support-data-rest/src/test/resources/init.sql +++ b/support-data-rest/src/test/resources/init.sql @@ -23,7 +23,7 @@ CREATE TABLE uniprotkb_statistics_entry value_count int8 NULL, entry_count int8 NULL, description varchar NULL, - release_name varchar(32) NOT NULL, + release_name int8 NOT NULL, entry_type varchar(32) NULL, CONSTRAINT uniprotkb_statistics_entry_un UNIQUE (attribute_name, statistics_category_id, release_name, entry_type) ); @@ -34,8 +34,10 @@ ALTER TABLE uniprotkb_statistics_entry CREATE TABLE uniprot_release ( - id varchar(64) PRIMARY KEY, - date date NOT NULL + id int8 PRIMARY KEY, + date date NOT NULL, + entry_type varchar(32) NOT NULL, + name varchar(32) NOT NULL ); ALTER TABLE uniprotkb_statistics_entry @@ -57,26 +59,32 @@ VALUES (45, 'SEQUENCE_AMINO_ACID', 'UNIPROTKB', 'Sequence Amino Acid', 'sf Seque INSERT INTO statistics_category (id, category, db_type, label, search_field) VALUES (52, 'TOP_ORGANISM', 'UNIPROTKB', 'Top Organism', 'sf Organism'); -INSERT INTO uniprot_release (id, date) -VALUES ('2021_02', '2021-03-25'); -INSERT INTO uniprot_release (id, date) -VALUES ('2021_03', '2021-05-25'); +INSERT INTO uniprot_release (id, name, date, entry_type) +VALUES (3, '2021_01', '2021-01-25', 'SWISSPROT'); +INSERT INTO uniprot_release (id, name, date, entry_type) +VALUES (4, '2021_01', '2021-01-25', 'TREMBL'); +INSERT INTO uniprot_release (id, name, date, entry_type) +VALUES (0, '2021_02', '2021-03-25', 'SWISSPROT'); +INSERT INTO uniprot_release (id, name, date, entry_type) +VALUES (1, '2021_02', '2021-03-25', 'TREMBL'); +INSERT INTO uniprot_release (id, name, date, entry_type) +VALUES (2, '2021_03', '2021-05-25', 'SWISSPROT'); INSERT INTO uniprotkb_statistics_entry (id, attribute_name, statistics_category_id, value_count, entry_count, description, release_name, entry_type) -VALUES (47549, 'Fungi', 39, 35360, 35360, null, '2021_03', 'SWISSPROT'); +VALUES (47549, 'Fungi', 39, 35360, 35360, null, 0, 'SWISSPROT'); INSERT INTO uniprotkb_statistics_entry (id, attribute_name, statistics_category_id, value_count, entry_count, description, release_name, entry_type) -VALUES (47550, 'Insecta', 39, 9457, 9457, null, '2021_03', 'SWISSPROT'); +VALUES (47550, 'Insecta', 39, 9457, 9457, null, 0, 'SWISSPROT'); INSERT INTO uniprotkb_statistics_entry (id, attribute_name, statistics_category_id, value_count, entry_count, description, release_name, entry_type) -VALUES (50289, 'Fungi', 39, 12793422, 12793422, null, '2021_03', 'TREMBL'); +VALUES (50289, 'Fungi', 39, 12793422, 12793422, null, 1, 'TREMBL'); INSERT INTO uniprotkb_statistics_entry (id, attribute_name, statistics_category_id, value_count, entry_count, description, release_name, entry_type) -VALUES (47175, 'AMINO_ACID_U', 45, 329, 254, null, '2021_03', 'SWISSPROT'); +VALUES (47175, 'AMINO_ACID_U', 45, 329, 254, null, 0, 'SWISSPROT'); INSERT INTO uniprotkb_statistics_entry (id, attribute_name, statistics_category_id, value_count, entry_count, description, release_name, entry_type) -VALUES (47206, 'Salmonella paratyphi B (strain ATCC BAA-1250 / SPB7)', 52, 716, 716, null, '2021_03', 'SWISSPROT'); +VALUES (47206, 'Salmonella paratyphi B (strain ATCC BAA-1250 / SPB7)', 52, 716, 716, null, 0, 'SWISSPROT'); INSERT INTO attribute_query (id, attribute_name, query, statistics_category_id) VALUES (1,'Fungi','(taxonomy_id:4751)', 39);