Search in sources :

Example 1 with MapVersion

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

the class MapServiceTest method positiveUploadTest.

@Test
public void positiveUploadTest() throws IOException {
    String zipFilename = "scmp_037.zip";
    when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.empty());
    try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
        byte[] mapData = ByteStreams.toByteArray(inputStream);
        Path tmpDir = temporaryDirectory.getRoot().toPath();
        instance.uploadMap(mapData, zipFilename, author, true);
        ArgumentCaptor<com.faforever.api.data.domain.Map> mapCaptor = ArgumentCaptor.forClass(com.faforever.api.data.domain.Map.class);
        verify(mapRepository, Mockito.times(1)).save(mapCaptor.capture());
        assertEquals("Sludge_Test", mapCaptor.getValue().getDisplayName());
        assertEquals("skirmish", mapCaptor.getValue().getMapType());
        assertEquals("FFA", mapCaptor.getValue().getBattleType());
        assertEquals(1, mapCaptor.getValue().getVersions().size());
        MapVersion mapVersion = mapCaptor.getValue().getVersions().get(0);
        assertEquals("The thick, brackish water clings to everything, staining anything it touches. If it weren't for this planet's proximity to the Quarantine Zone, no one would ever bother coming here.", mapVersion.getDescription());
        assertEquals(1, mapVersion.getVersion());
        assertEquals(256, mapVersion.getHeight());
        assertEquals(256, mapVersion.getWidth());
        assertEquals(3, mapVersion.getMaxPlayers());
        assertEquals("maps/sludge_test.v0001.zip", mapVersion.getFilename());
        assertFalse(Files.exists(tmpDir));
        Path generatedFile = finalDirectory.getRoot().toPath().resolve("sludge_test.v0001.zip");
        assertTrue(Files.exists(generatedFile));
        Path generatedFiles = finalDirectory.getRoot().toPath().resolve("generated_files");
        try (ZipInputStream inputStreamOfExpectedFile = new ZipInputStream(new BufferedInputStream(new FileInputStream(generatedFile.toFile())))) {
            Unzipper.from(inputStreamOfExpectedFile).to(generatedFiles).unzip();
        }
        Path expectedFiles = finalDirectory.getRoot().toPath().resolve("expected_files");
        try (ZipInputStream inputStreamOfExpectedFile = new ZipInputStream(new BufferedInputStream(loadMapResourceAsStream("sludge_test.v0001.zip")))) {
            Unzipper.from(inputStreamOfExpectedFile).to(expectedFiles).unzip();
        }
        expectedFiles = expectedFiles.resolve("sludge_test.v0001");
        try (Stream<Path> fileStream = Files.list(expectedFiles)) {
            assertEquals(fileStream.count(), (long) 4);
        }
        try (Stream<Path> fileStream = Files.list(expectedFiles)) {
            Path finalGeneratedFile = generatedFiles.resolve("sludge_test.v0001");
            fileStream.forEach(expectedFile -> FileAssert.assertEquals("Difference in " + expectedFile.getFileName().toString(), expectedFile.toFile(), finalGeneratedFile.resolve(expectedFile.getFileName().toString()).toFile()));
            assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathLarge().resolve("sludge_test.v0001.png")));
            assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathSmall().resolve("sludge_test.v0001.png")));
        }
    }
}
Also used : Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MapVersion(com.faforever.api.data.domain.MapVersion) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Map(com.faforever.api.config.FafApiProperties.Map) Test(org.junit.Test)

Example 2 with MapVersion

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

the class MapService method updateMapEntities.

private void updateMapEntities(MapUploadData progressData) {
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    Map map = progressData.getMapEntity();
    if (map == null) {
        map = new Map();
    }
    map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString()).setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring()).setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring()).setAuthor(progressData.getAuthorEntity());
    LuaValue size = scenarioInfo.get(ScenarioMapInfo.SIZE);
    MapVersion version = new MapVersion().setDescription(scenarioInfo.get(ScenarioMapInfo.DESCRIPTION).tojstring().replaceAll("<LOC .*?>", "")).setWidth(size.get(1).toint()).setHeight(size.get(2).toint()).setHidden(false).setRanked(progressData.isRanked()).setMaxPlayers(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_ARMIES).length()).setVersion(scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint());
    map.getVersions().add(version);
    version.setMap(map);
    progressData.setMapEntity(map);
    progressData.setMapVersionEntity(version);
    version.setFilename(STUPID_MAP_FOLDER_PREFIX + progressData.getFinalZipName());
    progressData.setFinalZipFile(this.fafApiProperties.getMap().getTargetDirectory().resolve(progressData.getFinalZipName()));
    if (Files.exists(progressData.getFinalZipFile())) {
        throw new ApiException(new Error(ErrorCode.MAP_NAME_CONFLICT, progressData.getFinalZipName()));
    }
    // this triggers validation
    mapRepository.save(map);
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) MapVersion(com.faforever.api.data.domain.MapVersion) LuaValue(org.luaj.vm2.LuaValue) Map(com.faforever.api.data.domain.Map) ApiException(com.faforever.api.error.ApiException)

Example 3 with MapVersion

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

the class MapServiceTest method versionExistsAlready.

@Test
public void versionExistsAlready() throws IOException {
    String zipFilename = "scmp_037.zip";
    Player me = new Player();
    me.setId(1);
    com.faforever.api.data.domain.Map map = new com.faforever.api.data.domain.Map().setAuthor(me).setVersions(Collections.singletonList(new MapVersion().setVersion(1)));
    when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.of(map));
    try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
        try {
            byte[] mapData = ByteStreams.toByteArray(inputStream);
            instance.uploadMap(mapData, zipFilename, me, true);
            fail();
        } catch (ApiException e) {
            assertThat(e, apiExceptionWithCode(ErrorCode.MAP_VERSION_EXISTS));
        }
        verify(mapRepository, never()).save(any(com.faforever.api.data.domain.Map.class));
    }
}
Also used : Player(com.faforever.api.data.domain.Player) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MapVersion(com.faforever.api.data.domain.MapVersion) Map(com.faforever.api.config.FafApiProperties.Map) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Aggregations

MapVersion (com.faforever.api.data.domain.MapVersion)3 Map (com.faforever.api.config.FafApiProperties.Map)2 ApiException (com.faforever.api.error.ApiException)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2 Test (org.junit.Test)2 Map (com.faforever.api.data.domain.Map)1 Player (com.faforever.api.data.domain.Player)1 Error (com.faforever.api.error.Error)1 ProgrammingError (com.faforever.api.error.ProgrammingError)1 Path (java.nio.file.Path)1 LuaValue (org.luaj.vm2.LuaValue)1