|
| 1 | +package com.sap.oss.phosphor.fosstars.data.github; |
| 2 | + |
| 3 | +import static com.sap.oss.phosphor.fosstars.model.feature.oss.OssFeatures.HAS_EXECUTABLE_BINARIES; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.spy; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.sap.oss.phosphor.fosstars.model.Value; |
| 11 | +import com.sap.oss.phosphor.fosstars.model.ValueSet; |
| 12 | +import com.sap.oss.phosphor.fosstars.model.subject.oss.GitHubProject; |
| 13 | +import com.sap.oss.phosphor.fosstars.model.value.ValueHashSet; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.UncheckedIOException; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.Optional; |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.eclipse.jgit.lib.Repository; |
| 22 | +import org.junit.AfterClass; |
| 23 | +import org.junit.BeforeClass; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +public class HasExecutableBinariesTest extends TestGitHubDataFetcherHolder { |
| 27 | + |
| 28 | + private static Path BASE_DIR; |
| 29 | + |
| 30 | + private static final GitHubProject PROJECT = new GitHubProject("org", "test"); |
| 31 | + |
| 32 | + private static LocalRepository LOCAL_REPOSITORY; |
| 33 | + |
| 34 | + @BeforeClass |
| 35 | + public static void setup() { |
| 36 | + try { |
| 37 | + BASE_DIR = Files.createTempDirectory(HasExecutableBinariesTest.class.getName()); |
| 38 | + Path git = BASE_DIR.resolve(".git"); |
| 39 | + Files.createDirectory(git); |
| 40 | + Path submodule = BASE_DIR.resolve("submodule"); |
| 41 | + Files.createDirectory(submodule); |
| 42 | + Path packageJson = submodule.resolve("pom.xml"); |
| 43 | + Files.write(packageJson, StringUtils.repeat("x", 500).getBytes()); |
| 44 | + |
| 45 | + LocalRepositoryInfo localRepositoryInfo = mock(LocalRepositoryInfo.class); |
| 46 | + when(localRepositoryInfo.path()).thenReturn(BASE_DIR); |
| 47 | + Repository repository = mock(Repository.class); |
| 48 | + when(repository.getDirectory()).thenReturn(git.toFile()); |
| 49 | + |
| 50 | + LOCAL_REPOSITORY = new LocalRepository(localRepositoryInfo, repository); |
| 51 | + LOCAL_REPOSITORY = spy(LOCAL_REPOSITORY); |
| 52 | + when(LOCAL_REPOSITORY.info()).thenReturn(localRepositoryInfo); |
| 53 | + |
| 54 | + TestGitHubDataFetcher.addForTesting(PROJECT, LOCAL_REPOSITORY); |
| 55 | + } catch (IOException e) { |
| 56 | + throw new UncheckedIOException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testExecutableIsPresent() throws IOException { |
| 62 | + Path exe = BASE_DIR.resolve("game.exe"); |
| 63 | + try { |
| 64 | + Files.write(exe, StringUtils.repeat("x", 1000).getBytes()); |
| 65 | + |
| 66 | + HasExecutableBinaries provider = new HasExecutableBinaries(fetcher); |
| 67 | + provider = spy(provider); |
| 68 | + when(provider.loadLocalRepository(PROJECT)).thenReturn(LOCAL_REPOSITORY); |
| 69 | + |
| 70 | + ValueSet values = new ValueHashSet(); |
| 71 | + provider.update(PROJECT, values); |
| 72 | + |
| 73 | + assertTrue(values.has(HAS_EXECUTABLE_BINARIES)); |
| 74 | + |
| 75 | + Optional<Value<Boolean>> something = values.of(HAS_EXECUTABLE_BINARIES); |
| 76 | + assertTrue(something.isPresent()); |
| 77 | + |
| 78 | + Value<Boolean> value = something.get(); |
| 79 | + assertTrue(value.get()); |
| 80 | + } finally { |
| 81 | + FileUtils.forceDeleteOnExit(exe.toFile()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testExecutableIsNotPresent() throws IOException { |
| 87 | + Path javaFile = BASE_DIR.resolve("Test.java"); |
| 88 | + try { |
| 89 | + Files.write(javaFile, StringUtils.repeat("x", 1000).getBytes()); |
| 90 | + |
| 91 | + HasExecutableBinaries provider = new HasExecutableBinaries(fetcher); |
| 92 | + provider = spy(provider); |
| 93 | + when(provider.loadLocalRepository(PROJECT)).thenReturn(LOCAL_REPOSITORY); |
| 94 | + |
| 95 | + ValueSet values = new ValueHashSet(); |
| 96 | + provider.update(PROJECT, values); |
| 97 | + |
| 98 | + assertTrue(values.has(HAS_EXECUTABLE_BINARIES)); |
| 99 | + |
| 100 | + Optional<Value<Boolean>> something = values.of(HAS_EXECUTABLE_BINARIES); |
| 101 | + assertTrue(something.isPresent()); |
| 102 | + |
| 103 | + Value<Boolean> value = something.get(); |
| 104 | + assertFalse(value.get()); |
| 105 | + } finally { |
| 106 | + FileUtils.forceDeleteOnExit(javaFile.toFile()); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @AfterClass |
| 112 | + public static void shutdown() { |
| 113 | + try { |
| 114 | + FileUtils.forceDeleteOnExit(BASE_DIR.toFile()); |
| 115 | + } catch (IOException e) { |
| 116 | + throw new UncheckedIOException(e); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments