From 53d74bce05e37dc603a29282416e3e19397f02a6 Mon Sep 17 00:00:00 2001 From: Eduard Frankford <e.frankford@student.uibk.ac.at> Date: Mon, 28 Jun 2021 10:38:13 +0200 Subject: [PATCH] created achievementService and separated some functionality out of achievementResource to make it easier to test and wrote the tests for achievements --- .../gitsearch/service/AchievementService.java | 59 ++++++++ .../gitsearch/service/SearchServiceIT.java | 126 ++++++++++++------ 2 files changed, 143 insertions(+), 42 deletions(-) create mode 100644 src/main/java/at/ac/uibk/gitsearch/service/AchievementService.java diff --git a/src/main/java/at/ac/uibk/gitsearch/service/AchievementService.java b/src/main/java/at/ac/uibk/gitsearch/service/AchievementService.java new file mode 100644 index 000000000..77fc7856c --- /dev/null +++ b/src/main/java/at/ac/uibk/gitsearch/service/AchievementService.java @@ -0,0 +1,59 @@ +package at.ac.uibk.gitsearch.service; + + +import java.io.IOException; +import java.util.Optional; + +import org.codeability.sharing.plugins.api.search.SearchResultDTO; +import org.codeability.sharing.plugins.api.search.SearchResultsDTO; + +import org.springframework.transaction.annotation.Transactional; + + +import at.ac.uibk.gitsearch.service.SearchService; +import at.ac.uibk.gitsearch.service.StatisticsService; +import at.ac.uibk.gitsearch.service.UserService; +import at.ac.uibk.gitsearch.service.dto.StatisticsDTO; +import org.springframework.stereotype.Service; + +@Service +@Transactional +public class AchievementService { + + + private final SearchService searchService; + + private final StatisticsService statisticsService; + + private final UserService userService; + + + public AchievementService(SearchService searchService, StatisticsService statisticsService, UserService userService) { + this.searchService = searchService; + this.statisticsService = statisticsService; + this.userService = userService; + } + + + public StatisticsDTO searchUserStatistics(String email) throws IOException { + + SearchResultsDTO results = searchService.searchResultForAuthorEmail(email, 1000); + int totalAmountOfViews = 0; + int totalAmountOfDownloads = 0; + for (SearchResultDTO result : results.getSearchResult()) { + Optional<StatisticsDTO> statisticsDTO = statisticsService.findOneByExerciseID((Long.parseLong(result.getExerciseId()))); + if (statisticsDTO.isPresent()) { + StatisticsDTO newStats = statisticsDTO.get(); + totalAmountOfViews += newStats.getViews(); + totalAmountOfDownloads += newStats.getDownloads(); + } + } + StatisticsDTO toReturn = new StatisticsDTO(); + toReturn.setViews(totalAmountOfViews); + toReturn.setDownloads(totalAmountOfDownloads); + return toReturn; + + + } + +} diff --git a/src/test/java/at/ac/uibk/gitsearch/service/SearchServiceIT.java b/src/test/java/at/ac/uibk/gitsearch/service/SearchServiceIT.java index 5ba263d25..4f0f40bea 100644 --- a/src/test/java/at/ac/uibk/gitsearch/service/SearchServiceIT.java +++ b/src/test/java/at/ac/uibk/gitsearch/service/SearchServiceIT.java @@ -11,6 +11,7 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; import java.io.IOException; @@ -37,46 +38,56 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.security.test.context.support.WithMockUser; import at.ac.uibk.gitsearch.GitsearchApp; +import at.ac.uibk.gitsearch.domain.Statistics; +import at.ac.uibk.gitsearch.repository.StatisticsRepository; import at.ac.uibk.gitsearch.repository.search.testESService.ElasticSearchTestServerConfiguration; +import at.ac.uibk.gitsearch.security.AuthoritiesConstants; import at.ac.uibk.gitsearch.service.PluginManagementService.ConnectorConfigWrapper; import at.ac.uibk.gitsearch.service.dto.AutoCompleteEntry; - +import at.ac.uibk.gitsearch.service.dto.StatisticsDTO; +import at.ac.uibk.gitsearch.service.AchievementService; @SpringBootTest(classes = GitsearchApp.class) @WithMockUser(value = TEST_USER_LOGIN, authorities = "sharing") public class SearchServiceIT { - + @Autowired RestHighLevelClient searchClient; - + @MockBean PluginManagementService pluginManagementService; - private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceIT.class); - + private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceIT.class); + @Autowired private SearchService searchService; + @Autowired + private AchievementService achievementService; + + @Autowired + private StatisticsRepository statisticsRepository; + @BeforeAll public static void setUpESServer() throws IOException, NodeValidationException { ElasticSearchTestServerConfiguration.getTestInstance().startTestNode(); } - + @BeforeEach public void initMock() { - Collection<ConnectorConfigWrapper> mockConfigs = Collections.singletonList( - new ConnectorConfigWrapper(new SharingPluginConfig("mockedPlugin", new SharingPluginConfig.Action[] { - new SharingPluginConfig.Action("mockedAction", "http://unused/unused-transferURL", "mocked Action", "true") - }), "http://unused/hopefully-unused") - ); + Collection<ConnectorConfigWrapper> mockConfigs = Collections.singletonList(new ConnectorConfigWrapper( + new SharingPluginConfig("mockedPlugin", + new SharingPluginConfig.Action[] { new SharingPluginConfig.Action("mockedAction", + "http://unused/unused-transferURL", "mocked Action", "true") }), + "http://unused/hopefully-unused")); when(pluginManagementService.getRegisteredPluginConfigs()).thenReturn(mockConfigs); } @Test public void testAllSearch() throws Exception { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null,null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null, null); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); - + Assert.assertTrue("At least one hit?", searchResultPage.getSearchResult().size() >= 1); Assert.assertEquals("We start at 0", 0, searchResultPage.getPageStartIndex()); LOGGER.info("found {} hits for all", searchResultPage.getHitCount()); @@ -86,7 +97,7 @@ public class SearchServiceIT { public void testFullTextSearch() throws Exception { LOGGER.info("starting testFullTextSearch"); final String WIEN = "Wien"; - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null,null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null, null); SearchInputDTO searchQuery = new SearchInputDTO(WIEN, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -104,20 +115,22 @@ public class SearchServiceIT { @Test public void testSearchByAutor() throws Exception { final String PODLIPNIG = "Stefan Podlipnig"; - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, PODLIPNIG, null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, PODLIPNIG, + null); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); org.junit.Assert.assertNotNull(searchResultPage.getSearchResult()); org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1); - org.junit.Assert.assertThat(searchResultPage.getSearchResult(), everyItem( - hasProperty("metadata", hasProperty("creator", hasItemInArray(hasProperty("name", containsString(PODLIPNIG))))))); + org.junit.Assert.assertThat(searchResultPage.getSearchResult(), everyItem(hasProperty("metadata", + hasProperty("creator", hasItemInArray(hasProperty("name", containsString(PODLIPNIG))))))); } @Test public void testProgrammingLanguageSearch() throws Exception { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO("Java".toLowerCase(), null, null, null, null,null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO("Java".toLowerCase(), null, null, null, + null, null); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -131,7 +144,8 @@ public class SearchServiceIT { @Test public void testCreatorSearch() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, "Podlipnig",null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, "Podlipnig", + null); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -142,21 +156,46 @@ public class SearchServiceIT { } - // @Test - // public void testEmailSearch() throws IOException { - // searchClient.getLowLevelClient().getNodes().forEach(n -> {LOGGER.info("Using node {}", n);}); - // SearchResultsDTO searchResultPage = searchService.searchResultForAuthorEmail("stefan.podlipnig@tuwien.ac.at", SearchInputDTO.PAGE_SIZE); - // org.junit.Assert.assertNotNull(searchResultPage.getSearchResult()); - // org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1); - // assertThat(searchResultPage.getSearchResult(), everyItem(hasProperty("metadata", - // hasProperty("creator", hasItemInArray(hasProperty("email", containsString("stefan.podlipnig@tuwien.ac.at"))))))); + @Test + public void testEmailSearch() throws IOException { + searchClient.getLowLevelClient().getNodes().forEach(n -> { + LOGGER.info("Using node {}", n); + }); + SearchResultsDTO searchResultPage = searchService.searchResultForAuthorEmail("stefan.podlipnig@tuwien.ac.at", + 1000); + org.junit.Assert.assertNotNull(searchResultPage.getSearchResult()); + org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1); + assertThat(searchResultPage.getSearchResult(), everyItem(hasProperty("metadata", hasProperty("creator", + hasItemInArray(hasProperty("email", containsString("stefan.podlipnig@tuwien.ac.at"))))))); + + } + + @Test + @WithMockUser(authorities = AuthoritiesConstants.ADMIN) + public void testSearchUserStatistics() throws IOException { + + // Create the Statistics + Statistics statistics = new Statistics(); + statistics.setDownloads(1000); + statistics.setViews(10000); + statistics.setExerciseID(3L); - // } + statisticsRepository.saveAndFlush(statistics); + + searchClient.getLowLevelClient().getNodes().forEach(n -> { + LOGGER.info("Using node {}", n); + }); + StatisticsDTO stats = achievementService.searchUserStatistics("stefan.podlipnig@tuwien.ac.at"); + + assertEquals(1000, stats.getDownloads()); + assertEquals(10000, stats.getViews()); + } @Test public void testKeywordSearch() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null,null); - org.codeability.sharing.plugins.api.search.SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null, null); + org.codeability.sharing.plugins.api.search.SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, + null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); org.junit.Assert.assertNotNull(searchResultPage.getSearchResult()); @@ -168,7 +207,7 @@ public class SearchServiceIT { @Test public void testFormatSearch() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null,"latex"); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null, "latex"); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -181,7 +220,7 @@ public class SearchServiceIT { @Test public void testTypesSearch() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null,null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null, null); searchMetadata.setTypes(Collections.singletonList(ExerciseType.COLLECTION)); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -195,8 +234,9 @@ public class SearchServiceIT { @Test public void testLanguagesSearch() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null,null); - searchMetadata.setNaturalLanguage(List.of("de"));; + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null, null); + searchMetadata.setNaturalLanguage(List.of("de")); + ; SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -209,21 +249,22 @@ public class SearchServiceIT { @Test public void testTypesSearchNegative() throws IOException { - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null,null); + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null, null); searchMetadata.setTypes(Collections.singletonList(ExerciseType.OTHER)); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); org.junit.Assert.assertNotNull(searchResultPage.getSearchResult()); - org.junit.Assert.assertTrue("We expect no hit for \"latex\" and ExerciseType.OTHER", searchResultPage.getHitCount() == 0); + org.junit.Assert.assertTrue("We expect no hit for \"latex\" and ExerciseType.OTHER", + searchResultPage.getHitCount() == 0); } -// @Test() -// @Ignore() // Test funktioniert momentan nicht? + // @Test() + // @Ignore() // Test funktioniert momentan nicht? public void testLicenseSearch() throws IOException { - - final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, "MIT", null,null); + + final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, "MIT", null, null); SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0); SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery); @@ -233,7 +274,7 @@ public class SearchServiceIT { everyItem(hasProperty("metadata", hasProperty("license", containsString("MIT"))))); } - + @Test public void testKeywordAutocompletion() throws IOException { final List<AutoCompleteEntry> keywordsAutoComplete = searchService.getKeywordsAutoComplete("Jav", 10); @@ -256,7 +297,8 @@ public class SearchServiceIT { @Test public void testContributorCreatorAutocompletion() throws IOException { - final List<AutoCompleteEntry> contributorAutoComplete = searchService.getContributorCreatorAutoComplete("Bast", 10); + final List<AutoCompleteEntry> contributorAutoComplete = searchService.getContributorCreatorAutoComplete("Bast", + 10); assertThat(contributorAutoComplete, contains(hasProperty("target", is("Daniel Bastta")))); } } -- GitLab