This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit f22cdb92 authored by Daniel Crazzolara's avatar Daniel Crazzolara
Browse files

Added tests

parent 47f0f5a7
2 merge requests!91Bringing JHipster7.6.0 to production,!81Bugfix/#224 export to artemis public visibility
......@@ -12,20 +12,29 @@ 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.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import at.ac.uibk.gitsearch.repository.gitlab.GitLabRepository;
import at.ac.uibk.gitsearch.security.jwt.TokenProvider;
import at.ac.uibk.gitsearch.web.rest.WithUnauthenticatedMockUser;
import org.codeability.sharing.plugins.api.SharingPluginConfig;
import org.codeability.sharing.plugins.api.search.SearchInputDTO;
import org.codeability.sharing.plugins.api.search.SearchInputMetadataDTO;
import org.codeability.sharing.plugins.api.search.SearchResultDTO;
import org.codeability.sharing.plugins.api.search.SearchResultsDTO;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.node.NodeValidationException;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.RepositoryApi;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -41,6 +50,8 @@ import at.ac.uibk.gitsearch.repository.search.testESService.ElasticSearchTestSer
import at.ac.uibk.gitsearch.service.PluginManagementService.ConnectorConfigWrapper;
import at.ac.uibk.gitsearch.service.dto.AutoCompleteEntry;
import javax.ws.rs.NotFoundException;
@SpringBootTest(classes = GitsearchApp.class)
@WithMockUser(value = TEST_USER_LOGIN, authorities = "sharing")
public class SearchServiceIT {
......@@ -51,7 +62,13 @@ public class SearchServiceIT {
@MockBean
PluginManagementService pluginManagementService;
private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceIT.class);
@MockBean
GitLabRepository gitLabRepository;
@MockBean
RepositoryApi mockedRepositoryApi;
private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceIT.class);
@Autowired
private SearchService searchService;
......@@ -63,11 +80,15 @@ public class SearchServiceIT {
@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 = new ArrayList<>();
mockConfigs.add(new ConnectorConfigWrapper(new SharingPluginConfig("mockedPlugin", new SharingPluginConfig.Action[] {
new SharingPluginConfig.Action("mockedAction", "http://unused/unused-transferURL", "mocked Action", "true")
}), "http://unused/hopefully-unused"));
mockConfigs.add(new ConnectorConfigWrapper(new SharingPluginConfig("Artemis Sharing Connector", new SharingPluginConfig.Action[] {
new SharingPluginConfig.Action("Import", "http://unused/unused-transferURL", "Export to Artemis", "true")
}), "http://unused/hopefully-unused"));
when(pluginManagementService.getRegisteredPluginConfigs()).thenReturn(mockConfigs);
}
......@@ -250,4 +271,40 @@ public class SearchServiceIT {
final List<AutoCompleteEntry> contributorAutoComplete = searchService.getContributorCreatorAutoComplete("Bast", 10);
assertThat(contributorAutoComplete, contains(hasProperty("target", is("Daniel Bastta"))));
}
@Test
public void testArtemisExportActionFilteringForAuthorizedUser() throws IOException, GitLabApiException {
GitLabApi mockedGitLabApi = mock(GitLabApi.class);
when(gitLabRepository.getGitLabApi(any(Optional.class))).thenReturn(mockedGitLabApi);
doReturn(mockedRepositoryApi).when(mockedGitLabApi).getRepositoryApi();
doReturn(new ByteArrayInputStream(" ".getBytes())).when(mockedRepositoryApi).getRepositoryArchive(eq(333), anyString(), anyString());
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null,null);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO results = searchService.searchResultPage(searchQuery);
Optional<SearchResultDTO> result = results.getSearchResult().stream().filter(entry -> entry.getExerciseId().equals("333")).findFirst();
if (result.isEmpty()) {
throw new NotFoundException("Exercise with publicVisibility and id '333' couldn't be found in the test data");
}
Assertions.assertTrue(result.get().getSupportedActions().stream().anyMatch(action -> action.getPlugin().equals("Artemis Sharing Connector")));
}
@Test
@WithMockUser(value = "User without authorities")
public void testArtemisExportActionFilteringForUnauthorizedUser() throws IOException {
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null,null);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO results = searchService.searchResultPage(searchQuery);
Optional<SearchResultDTO> result = results.getSearchResult().stream().filter(entry -> entry.getExerciseId().equals("333")).findFirst();
if (result.isEmpty()) {
throw new NotFoundException("Exercise with publicVisibility and id '333' couldn't be found in the test data");
}
Assertions.assertFalse(result.get().getSupportedActions().stream().anyMatch(action -> action.getPlugin().equals("Artemis Sharing Connector")));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment