private Test mapTest(
     PuzzleTestDto puzzleTest, Runner runner, Map<String, ByteArrayOutputStream> testFiles)
     throws IOException {
   Test test = new Test();
   test.setIndex(Integer.parseInt(puzzleTest.getIndex()));
   test.setRunner(runner);
   test = testRepository.save(test);
   String inputFileName = test.getId() + "/" + puzzleTest.getIndex() + ".txt";
   if (testFiles == null) {
     storage.uploadPublic(
         testsBucket,
         inputFileName,
         new ByteArrayInputStream(puzzleTest.getData().getBytes()),
         "text/plain");
   } else {
     storage.uploadPublic(
         testsBucket,
         inputFileName,
         new ByteArrayInputStream(testFiles.get(puzzleTest.getData()).toByteArray()),
         "text/plain");
   }
   storage.upload(
       testsBucket,
       test.getId() + "/output.txt",
       new ByteArrayInputStream(puzzleTest.getSolution().getBytes()),
       "text/plain");
   Map<String, String> testParams = new HashMap<>();
   testParams.put("test", testsBucket + "/" + test.getId() + "/output.txt");
   testParams.put("stdin", testsBucket + "/" + inputFileName);
   test.setParams(testParams);
   test = testRepository.save(test);
   test.setRunner(runner);
   return test;
 }
 private Task mapTask(
     PuzzleDto puzzle,
     ChallengeTemplate challengeTemplate,
     Organization organization,
     Map<String, ByteArrayOutputStream> files,
     Duration gameDuration,
     Runner runner,
     Endpoint endpoint,
     Set<Language> languages)
     throws IOException {
   Task task = new Task();
   task.setCanonicalName(
       fixCanonicalName(challengeTemplate.getCanonicalName() + "-" + puzzle.getCanonicalName()));
   task.setName(puzzle.getCanonicalName());
   task.setEndpoint(endpoint);
   task.setDescription(puzzle.getCanonicalName());
   task.setInstructions(
       storage.uploadPublic(
           instructionsBucket,
           instructionsFileName(challengeTemplate, puzzle.getInstructionsFile()),
           new ByteArrayInputStream(files.get(puzzle.getInstructionsFile()).toByteArray()),
           "application/pdf"));
   task.setDuration(gameDuration);
   task.setRunner(runner);
   task.setLanguages(languages);
   task.setOrganization(organization);
   return task;
 }
 public UUID createChallengeTemplateFromGameResources(MultipartFile gameZip, UUID organizationId)
     throws IOException {
   Map<String, ByteArrayOutputStream> files = unzip(gameZip.getBytes());
   CodingContestGameDto game = null;
   for (String fileName : files.keySet()) {
     if (fileName.endsWith(".xml")) {
       byte[] byts = files.get(fileName).toByteArray();
       ObjectMapper mapper = new XmlMapper();
       mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
       game = mapper.readValue(new ByteArrayInputStream(byts), CodingContestGameDto.class);
     }
   }
   if (game == null) {
     throw new CodunoIllegalArgumentException("ccc.game.zip.invalid");
   }
   return createChallengeTemplate(game, organizationId, files);
 }
 private Map<String, ByteArrayOutputStream> unzip(byte[] zipbytes) throws IOException {
   final int bufSize = 2048;
   Map<String, ByteArrayOutputStream> map = new HashMap<>();
   ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(zipbytes));
   ZipEntry entry;
   byte[] buf = new byte[bufSize];
   while ((entry = zip.getNextEntry()) != null) {
     String fileName = entry.getName().substring(entry.getName().indexOf("/") + 1);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     int count;
     while ((count = zip.read(buf, 0, bufSize)) != -1) {
       baos.write(buf, 0, count);
     }
     map.put(fileName, baos);
   }
   return map;
 }
  private UUID createChallengeTemplate(
      CodingContestGameDto dto, UUID organizationId, Map<String, ByteArrayOutputStream> files)
      throws IOException {
    ChallengeTemplate challengeTemplate =
        challengeTemplateRepository.findOneByCanonicalName(dto.getCanonicalName());
    if (challengeTemplate != null) {
      return challengeTemplate.getId();
    }
    Organization organization = organizationRepository.findOne(organizationId);
    if (organization == null) {
      throw new CodunoIllegalArgumentException("organization.invalid");
    }

    if (dto.getPuzzles()
        .stream()
        .findAny()
        .filter(puzzleDto -> puzzleDto.getValidationClass() != null)
        .isPresent()) {
      throw new CodunoIllegalArgumentException("ccc.game.structure.unsuported");
    }

    Runner runner = getRunner("/io");
    Endpoint taskEndpoint = getEndpoint("CCC general task", "ccc-io-task");
    Set<Language> languages = new HashSet<>(languageRepository.findAll());
    Duration gameDuration = parseGameDuration(dto.getTimeframe());

    challengeTemplate = mapChallengeTemplate(dto, organization, gameDuration);

    for (PuzzleDto puzzle : dto.getPuzzles()) {
      Task task =
          mapTask(
              puzzle,
              challengeTemplate,
              organization,
              files,
              gameDuration,
              runner,
              taskEndpoint,
              languages);
      Map<String, ByteArrayOutputStream> testFiles = null;
      if (puzzle.getInputFilePath() != null) {
        testFiles = unzip(files.get(puzzle.getInputFilePath()).toByteArray());
      }
      for (PuzzleTestDto puzzleTest : puzzle.getTests()) {
        Test test = mapTest(puzzleTest, runner, testFiles);
        task.addTest(test);
      }
      task = taskRepository.save(task);
      challengeTemplate.addTask(task);
    }
    return challengeTemplateRepository.save(challengeTemplate).getId();
  }