<commit_before>/*
 * Copyright (c) 2011-2019 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
 * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.
 */
package com.terracottatech.dynamic_config.test;

import com.terracottatech.diagnostic.common.JsonDiagnosticCodec;
import com.terracottatech.dynamic_config.test.util.Env;
import com.terracottatech.dynamic_config.test.util.NodeProcess;
import com.terracottatech.testing.lock.PortLockingRule;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.readAllLines;
import static java.nio.file.Files.walkFileTree;
import static java.nio.file.Files.write;
import static org.awaitility.pollinterval.IterativePollInterval.iterative;

public class BaseStartupIT {

  @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
  @Rule public final SystemOutRule out = new SystemOutRule().enableLog();
  @Rule public final SystemErrRule err = new SystemErrRule().enableLog();
  @Rule public final PortLockingRule ports = new PortLockingRule(2);

  volatile NodeProcess nodeProcess;

  @After
  public void tearDown() {
    if (nodeProcess != null) {
      nodeProcess.close();
    }
  }

  String toPrettyJson(Object o) {
    return new JsonDiagnosticCodec(true).serialize(o);
  }

  InetSocketAddress getServerAddress() {
    return InetSocketAddress.createUnresolved("localhost", ports.getPort());
  }

  Path configFilePath() throws Exception {
    return configFilePath("", String.valueOf(ports.getPort()));
  }

  Path configFilePath(String suffix, String port) throws Exception {
    String resourceName = "/config-property-files/single-stripe" + suffix + ".properties";
    Path original = Paths.get(NewServerStartupScriptIT.class.getResource(resourceName).toURI());
    String contents = new String(Files.readAllBytes(original));
    String replacedContents = contents.replaceAll(Pattern.quote("${PORT}"), port);
    Path newPath = temporaryFolder.newFile().toPath();
    Files.write(newPath, replacedContents.getBytes(StandardCharsets.UTF_8));
    return newPath;
  }<commit_msg>TDB-4542: Extracted ObjectMapper to a common Json class<commit_after>/*
 * Copyright (c) 2011-2019 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
 * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.
 */
package com.terracottatech.dynamic_config.test;

import com.terracottatech.dynamic_config.test.util.Env;
import com.terracottatech.dynamic_config.test.util.NodeProcess;
import com.terracottatech.testing.lock.PortLockingRule;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.readAllLines;
import static java.nio.file.Files.walkFileTree;
import static java.nio.file.Files.write;
import static org.awaitility.pollinterval.IterativePollInterval.iterative;

public class BaseStartupIT {

  @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
  @Rule public final SystemOutRule out = new SystemOutRule().enableLog();
  @Rule public final SystemErrRule err = new SystemErrRule().enableLog();
  @Rule public final PortLockingRule ports = new PortLockingRule(2);

  volatile NodeProcess nodeProcess;

  @After
  public void tearDown() {
    if (nodeProcess != null) {
      nodeProcess.close();
    }
  }

  InetSocketAddress getServerAddress() {
    return InetSocketAddress.createUnresolved("localhost", ports.getPort());
  }

  Path configFilePath() throws Exception {
    return configFilePath("", String.valueOf(ports.getPort()));
  }

  Path configFilePath(String suffix, String port) throws Exception {
    String resourceName = "/config-property-files/single-stripe" + suffix + ".properties";
    Path original = Paths.get(NewServerStartupScriptIT.class.getResource(resourceName).toURI());
    String contents = new String(Files.readAllBytes(original));
    String replacedContents = contents.replaceAll(Pattern.quote("${PORT}"), port);
    Path newPath = temporaryFolder.newFile().toPath();
    Files.write(newPath, replacedContents.getBytes(StandardCharsets.UTF_8));
    return newPath;
  }
{'fraction_non_alphanumeric': 0.10391993327773144, 'fraction_numerical': 0.004336947456213511, 'mean_word_length': 5.454251883745964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896771', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1730, 'n_words': 407}