Search in sources :

Example 1 with FeaturedMod

use of com.faforever.api.data.domain.FeaturedMod in project faf-java-api by FAForever.

the class GitHubDeploymentServiceTest method deployEnvironmentMatch.

@Test
public void deployEnvironmentMatch() throws Exception {
    apiProperties.getGitHub().setDeploymentEnvironment(ENVIRONMENT);
    // Couldn't be mocked since calling ghDeployment.getId() threw an NPE
    GHDeployment ghDeployment = new GHDeployment() {

        @Override
        public int getId() {
            return 1;
        }

        @Override
        public String getEnvironment() {
            return ENVIRONMENT;
        }

        @Override
        public String getPayload() {
            return "faf";
        }
    };
    Deployment deployment = mock(Deployment.class);
    when(deployment.getDeployment()).thenReturn(ghDeployment);
    GHRepository ghRepository = mock(GHRepository.class);
    GHDeploymentStatusBuilder builder = mock(GHDeploymentStatusBuilder.class);
    when(builder.description(any())).thenReturn(builder);
    when(ghRepository.createDeployStatus(anyInt(), any())).thenReturn(builder);
    when(deployment.getRepository()).thenReturn(ghRepository);
    LegacyFeaturedModDeploymentTask task = mock(LegacyFeaturedModDeploymentTask.class);
    when(task.setFeaturedMod(any())).thenReturn(task);
    when(task.setStatusDescriptionListener(any())).thenReturn(task);
    when(applicationContext.getBean(LegacyFeaturedModDeploymentTask.class)).thenReturn(task);
    when(featuredModService.findModByTechnicalName("faf")).thenReturn(Optional.of(new FeaturedMod()));
    instance.deploy(deployment);
    verify(task).run();
    verify(builder).create();
}
Also used : GHRepository(org.kohsuke.github.GHRepository) GHDeployment(org.kohsuke.github.GHDeployment) GHDeploymentStatusBuilder(org.kohsuke.github.GHDeploymentStatusBuilder) Deployment(org.kohsuke.github.GHEventPayload.Deployment) GHDeployment(org.kohsuke.github.GHDeployment) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) Test(org.junit.Test)

Example 2 with FeaturedMod

use of com.faforever.api.data.domain.FeaturedMod in project faf-java-api by FAForever.

the class LegacyFeaturedModDeploymentTaskTest method testRunNoFileIds.

@Test
public void testRunNoFileIds() throws Exception {
    instance.setFeaturedMod(new FeaturedMod().setGitBranch("branch").setFileExtension("nx3").setTechnicalName("faf").setAllowOverride(true).setGitUrl("git@example.com/FAForever/faf"));
    Mockito.doAnswer(invocation -> {
        Path repoFolder = invocation.getArgument(0);
        Files.createDirectories(repoFolder.resolve("someDir"));
        Files.copy(LegacyFeaturedModDeploymentTaskTest.class.getResourceAsStream("/featured_mod/mod_info.lua"), repoFolder.resolve("mod_info.lua"));
        return null;
    }).when(gitWrapper).checkoutRef(any(), any());
    when(featuredModService.getFeaturedMods()).thenReturn(Collections.singletonList(new FeaturedMod().setTechnicalName("faf")));
    when(featuredModService.getFileIds("faf")).thenReturn(Collections.emptyMap());
    Path dummyExe = repositoriesFolder.getRoot().toPath().resolve("TemplateForgedAlliance.exe");
    createDummyExe(dummyExe);
    properties.getDeployment().setForgedAllianceExePath(dummyExe.toAbsolutePath().toString());
    instance.run();
    verify(featuredModService, never()).save(anyString(), anyShort(), any());
}
Also used : Path(java.nio.file.Path) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) Test(org.junit.Test)

Example 3 with FeaturedMod

use of com.faforever.api.data.domain.FeaturedMod in project faf-java-api by FAForever.

the class LegacyFeaturedModDeploymentTask method run.

@Override
@SneakyThrows
@Transactional(TxType.MANDATORY)
public void run() {
    Assert.state(featuredMod != null, "Configuration must be set");
    String modName = featuredMod.getTechnicalName();
    Assert.state(featuredModService.getFeaturedMods().stream().anyMatch(featuredMod -> Objects.equals(featuredMod.getTechnicalName(), modName)), "Unknown mod: " + modName);
    String repositoryUrl = featuredMod.getGitUrl();
    String branch = featuredMod.getGitBranch();
    boolean allowOverride = Optional.ofNullable(featuredMod.isAllowOverride()).orElse(false);
    String modFilesExtension = featuredMod.getFileExtension();
    Map<String, Short> fileIds = featuredModService.getFileIds(modName);
    log.info("Starting deployment of '{}' from '{}', branch '{}', allowOverride '{}', modFilesExtension '{}'", modName, repositoryUrl, branch, allowOverride, modFilesExtension);
    Path repositoryDirectory = buildRepositoryDirectoryPath(repositoryUrl);
    checkoutCode(repositoryDirectory, repositoryUrl, branch);
    short version = readModVersion(repositoryDirectory);
    verifyVersion(version, allowOverride, modName);
    Deployment deployment = apiProperties.getDeployment();
    Path targetFolder = Paths.get(deployment.getFeaturedModsTargetDirectory(), String.format(deployment.getFilesDirectoryFormat(), modName));
    List<StagedFile> files = packageDirectories(repositoryDirectory, version, fileIds, targetFolder);
    createPatchedExe(version, fileIds, targetFolder).ifPresent(files::add);
    if (files.isEmpty()) {
        log.warn("Could not find any files to deploy. Is the configuration correct?");
        return;
    }
    files.forEach(this::finalizeFile);
    updateDatabase(files, version, modName);
    log.info("Deployment of '{}' version '{}' was successful", modName, version);
}
Also used : FeaturedModService(com.faforever.api.featuredmods.FeaturedModService) Setter(lombok.Setter) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Deployment(com.faforever.api.config.FafApiProperties.Deployment) SneakyThrows(lombok.SneakyThrows) ForgedAllianceExePatcher(com.faforever.commons.fa.ForgedAllianceExePatcher) OptionalInt(java.util.OptionalInt) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) StandardCopyOption(java.nio.file.StandardCopyOption) FilePermissionUtil(com.faforever.api.utils.FilePermissionUtil) Hashing.md5(com.google.common.hash.Hashing.md5) Files.hash(com.google.common.io.Files.hash) Map(java.util.Map) GitWrapper(com.faforever.api.deployment.git.GitWrapper) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) TxType(javax.transaction.Transactional.TxType) Transactional(javax.transaction.Transactional) Files(java.nio.file.Files) FeaturedModFile(com.faforever.api.featuredmods.FeaturedModFile) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Files.createDirectories(java.nio.file.Files.createDirectories) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Consumer(java.util.function.Consumer) FileVisitResult(java.nio.file.FileVisitResult) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) ValidationException(javax.validation.ValidationException) Paths(java.nio.file.Paths) ByteStreams(com.google.common.io.ByteStreams) Data(lombok.Data) ModReader(com.faforever.commons.mod.ModReader) Optional(java.util.Optional) Lazy(org.springframework.context.annotation.Lazy) FafApiProperties(com.faforever.api.config.FafApiProperties) NoCatch.noCatch(com.github.nocatch.NoCatch.noCatch) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) Path(java.nio.file.Path) Deployment(com.faforever.api.config.FafApiProperties.Deployment) SneakyThrows(lombok.SneakyThrows) Transactional(javax.transaction.Transactional)

Example 4 with FeaturedMod

use of com.faforever.api.data.domain.FeaturedMod in project faf-java-api by FAForever.

the class GitHubDeploymentServiceTest method createDeploymentIfEligible.

@Test
public void createDeploymentIfEligible() throws Exception {
    Push push = mock(Push.class);
    when(push.getRef()).thenReturn("refs/heads/junit");
    when(featuredModService.findByGitUrlAndGitBranch(EXAMPLE_REPO_URL, "junit")).thenReturn(Optional.of(new FeaturedMod().setTechnicalName("faf")));
    GHRepository repository = mock(GHRepository.class);
    when(repository.gitHttpTransportUrl()).thenReturn(EXAMPLE_REPO_URL);
    GHDeploymentBuilder deploymentBuilder = mock(GHDeploymentBuilder.class);
    when(deploymentBuilder.autoMerge(false)).thenReturn(deploymentBuilder);
    when(deploymentBuilder.environment(ENVIRONMENT)).thenReturn(deploymentBuilder);
    when(deploymentBuilder.payload("faf")).thenReturn(deploymentBuilder);
    when(deploymentBuilder.requiredContexts(Collections.emptyList())).thenReturn(deploymentBuilder);
    when(repository.createDeployment("refs/heads/junit")).thenReturn(deploymentBuilder);
    when(push.getRepository()).thenReturn(repository);
    apiProperties.getGitHub().setDeploymentEnvironment(ENVIRONMENT);
    instance.createDeploymentIfEligible(push);
    verify(repository).createDeployment(any());
    verify(deploymentBuilder).autoMerge(false);
    verify(deploymentBuilder).environment(ENVIRONMENT);
    verify(deploymentBuilder).create();
}
Also used : GHRepository(org.kohsuke.github.GHRepository) GHDeploymentBuilder(org.kohsuke.github.GHDeploymentBuilder) Push(org.kohsuke.github.GHEventPayload.Push) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) Test(org.junit.Test)

Example 5 with FeaturedMod

use of com.faforever.api.data.domain.FeaturedMod in project faf-java-api by FAForever.

the class LegacyFeaturedModDeploymentTaskTest method testRun.

@Test
@SuppressWarnings("unchecked")
public void testRun() throws Exception {
    instance.setFeaturedMod(new FeaturedMod().setGitBranch("branch").setFileExtension("nx3").setTechnicalName("faf").setAllowOverride(true).setGitUrl("git@example.com/FAForever/faf"));
    Mockito.doAnswer(invocation -> {
        Path repoFolder = invocation.getArgument(0);
        Files.createDirectories(repoFolder.resolve("someDir"));
        Files.copy(LegacyFeaturedModDeploymentTaskTest.class.getResourceAsStream("/featured_mod/mod_info.lua"), repoFolder.resolve("mod_info.lua"));
        Files.copy(LegacyFeaturedModDeploymentTaskTest.class.getResourceAsStream("/featured_mod/someDir/someFile"), repoFolder.resolve("someDir/someFile"));
        return null;
    }).when(gitWrapper).checkoutRef(any(), any());
    when(featuredModService.getFeaturedMods()).thenReturn(Collections.singletonList(new FeaturedMod().setTechnicalName("faf")));
    when(featuredModService.getFileIds("faf")).thenReturn(ImmutableMap.of("ForgedAlliance.exe", (short) 1, "someDir.nx3", (short) 2));
    Path dummyExe = repositoriesFolder.getRoot().toPath().resolve("TemplateForgedAlliance.exe");
    createDummyExe(dummyExe);
    properties.getDeployment().setForgedAllianceExePath(dummyExe.toAbsolutePath().toString());
    instance.run();
    ArgumentCaptor<List<FeaturedModFile>> filesCaptor = ArgumentCaptor.forClass(List.class);
    verify(featuredModService).save(eq("faf"), eq((short) 1337), filesCaptor.capture());
    List<FeaturedModFile> files = filesCaptor.getValue();
    files.sort(Comparator.comparing(FeaturedModFile::getFileId));
    assertThat(files.get(0).getFileId(), is((short) 1));
    assertThat(files.get(0).getMd5(), is("47df959058cb52fe966ea5936dbd8f4c"));
    assertThat(files.get(0).getName(), is("ForgedAlliance.1337.exe"));
    assertThat(files.get(0).getVersion(), is(1337));
    assertThat(files.get(1).getFileId(), is((short) 2));
    assertThat(files.get(1).getMd5(), is(notNullValue()));
    assertThat(files.get(1).getName(), is("someDir.1337.nx3"));
    assertThat(files.get(1).getVersion(), is(1337));
    assertThat(Files.exists(targetFolder.getRoot().toPath().resolve("updates_faf_files/someDir.1337.nx3")), is(true));
    assertThat(Files.exists(targetFolder.getRoot().toPath().resolve("updates_faf_files/ForgedAlliance.1337.exe")), is(true));
}
Also used : Path(java.nio.file.Path) List(java.util.List) FeaturedMod(com.faforever.api.data.domain.FeaturedMod) FeaturedModFile(com.faforever.api.featuredmods.FeaturedModFile) Test(org.junit.Test)

Aggregations

FeaturedMod (com.faforever.api.data.domain.FeaturedMod)9 Test (org.junit.Test)5 Path (java.nio.file.Path)3 FeaturedModFile (com.faforever.api.featuredmods.FeaturedModFile)2 List (java.util.List)2 SneakyThrows (lombok.SneakyThrows)2 FafApiProperties (com.faforever.api.config.FafApiProperties)1 Deployment (com.faforever.api.config.FafApiProperties.Deployment)1 GitWrapper (com.faforever.api.deployment.git.GitWrapper)1 FeaturedModService (com.faforever.api.featuredmods.FeaturedModService)1 FilePermissionUtil (com.faforever.api.utils.FilePermissionUtil)1 ForgedAllianceExePatcher (com.faforever.commons.fa.ForgedAllianceExePatcher)1 ModReader (com.faforever.commons.mod.ModReader)1 NoCatch.noCatch (com.github.nocatch.NoCatch.noCatch)1 Hashing.md5 (com.google.common.hash.Hashing.md5)1 ByteStreams (com.google.common.io.ByteStreams)1 Files.hash (com.google.common.io.Files.hash)1 Data (com.yahoo.elide.jsonapi.models.Data)1 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)1 Resource (com.yahoo.elide.jsonapi.models.Resource)1