<commit_before>package i5.las2peer.services.codeGenerationService;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Properties;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import i5.cae.simpleModel.SimpleModel;
import i5.las2peer.p2p.LocalNode;
import i5.las2peer.p2p.ServiceNameVersion;
import i5.las2peer.security.ServiceAgent;
import i5.las2peer.services.codeGenerationService.exception.GitHubException;
import i5.las2peer.services.codeGenerationService.generators.Generator;
import i5.las2peer.services.gitHubProxyService.GitHubProxyService;


/**
 * 
 * Central test class for the CAE-Code-Generation-Service.
 *
 */
public class CodeGenerationServiceTest {

  private static LocalNode node;

  private static final String codeGenerationService =
      CodeGenerationService.class.getName();

  private static final String gitHubProxyService = GitHubProxyService.class.getName();

  private static SimpleModel model1;
  private static SimpleModel model2;
  private static SimpleModel model3;
  private static SimpleModel[] model4;
  private static ServiceAgent testService;
  private static ServiceNameVersion serviceNameVersion;

  private static ServiceAgent gitHubProxyTestService;
  private static ServiceNameVersion gitHubProxyServiceNameVersion;

  private static String gitHubOrganization = null;
  private static String gitHubUser = null;
  private static String gitHubPassword = <PASSWORD>;
  @SuppressWarnings("unused")
  private static String gitHubUserMail = null;
  @SuppressWarnings("unused")
  private static String templateRepository = null;


  /**
   * 
   * Called before the tests start. Sets up the node and loads test models for later usage.
   * 
   * @throws Exception
   * 
   */
  @BeforeClass
  public static void startServer() throws Exception {
    // load models
    String modelPath1 = "./testModels/My First Testservice.model";
    String modelPath2 = "./testModels/My First Testservice without DB.model";
    String modelPath3 = "./testModels/My Test Widget.model";
    String[] modelPathes4 = new String[7];
    modelPathes4[0] = "./testModels/applicationTestModel/CAE Example Application.model";
    modelPathes4[1] = "./testModels/applicationTestModel/Graph Widget.model";
    modelPathes4[2] = "./testModels/applicationTestModel/LAS2peer Load Store Graph Service.model";
    modelPathes4[3] = "./testModels/applicationTestModel/LAS2peer Video List Service.model";
    modelPathes4[4] = "./testModels/applicationTestModel/Load Store Widget.model";
    modelPathes4[5] = "./testModels/applicationTestModel/Video List Widget.model";
    modelPathes4[6] = "./testModels/applicationTestModel/Video Player Widget.model";

    Properties properties = new Properties();
    String propertiesFile =
        "./etc/i5.las2peer.services.codeGenerationService.CodeGenerationService.properties";

    try {
      InputStream file1 = new FileInputStream(modelPath1);
      InputStream buffer1 = new BufferedInputStream(file1);
      ObjectInput input1 = new ObjectInputStream(buffer1);
      model1 = (SimpleModel) input1.readObject();
      InputStream file2 = new FileInputStream(modelPath2);
      InputStream buffer2 = new BufferedInputStream(file2);
      ObjectInput input2 = new ObjectInputStream(buffer2);
      model2 = (SimpleModel) input2.readObject();
      InputStream file3 = new FileInputStream(modelPath3);
      InputStream buffer3 = new BufferedInputStream(file3);
      ObjectInput input3 = new ObjectInputStream(buffer3);
      model3 = (SimpleModel) input3.readObject();
      model4 = new SimpleModel[modelPathes4.length];
      int i = 0;
      for (String modelPath4 : modelPathes4) {
        InputStream file4 = new FileInputStream(modelPath4);
        InputStream buffer4 = new BufferedInputStream(file4);
        ObjectInput input4 = new ObjectInputStream(buffer4);
        model4[i] = (SimpleModel) input4.readObject();
        input4.close();
        i++;
      }
      input1.close();
      input2.close();
      input3.close();

      FileReader reader = new FileReader(propertiesFile);
      properties.load(reader);
      gitHubUser = properties.getProperty("gitHubUser");
      gitHubUserMail = properties.getProperty("gitHubUserMail");
      gitHubOrganization = properties.getProperty("gitHubOrganization");
      templateRepository = properties.getProperty("templateRepository");
      gitHubPassword = properties.getProperty("gitHubPassword");

    } catch (IOException ex) {
      fail("Error reading test models and configuration file!");
    }


    // start node
    node = LocalNode.newNode();
    node.launch();

    serviceNameVersion = new ServiceNameVersion(codeGenerationService, "0.1");
    testService = ServiceAgent.createServiceAgent(serviceNameVersion, "a pass");
    testService.unlockPrivateKey("a pass");

    gitHubProxyServiceNameVersion = new ServiceNameVersion(gitHubProxyService, "0.1");
    gitHubProxyTestService = ServiceAgent.createServiceAgent(gitHubProxyServiceNameVersion, "a pass");
    gitHubProxyTestService.unlockPrivateKey("a pass");


    node.registerReceiver(gitHubProxyTestService);
    node.registerReceiver(testService);

    // waiting here not needed because no connector is running!

  }


  /**
   * 
   * Called after the tests have finished. Deletes all test repositories and shuts down the server.
   * Just comment out repositories you want to check on after the tests.
   * 
   * @throws Exception
   * 
   **/
  @AfterClass
  public static void shutDownServer() throws Exception {
    String model1GitHubName = "microservice-" + model1.getName().replace(" ", "-");
    String model2GitHubName = "microservice-" + model2.getName().replace(" ", "-");
    String model3GitHubName = "frontendComponent-" + model3.getName().replace(" ", "-");
    String model4GitHubName = "application-" + model4[0].getName().replace(" ", "-");

    try {
      Generator.deleteRemoteRepository(model1GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model2GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model3GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model4GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    node.shutDown();
    node = null;
    LocalNode.reset();
  }


  /**<commit_msg>Fixed test errors<commit_after>package i5.las2peer.services.codeGenerationService;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Properties;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import i5.cae.simpleModel.SimpleModel;
import i5.las2peer.p2p.LocalNode;
import i5.las2peer.p2p.ServiceNameVersion;
import i5.las2peer.security.ServiceAgent;
import i5.las2peer.services.codeGenerationService.exception.GitHubException;
import i5.las2peer.services.codeGenerationService.generators.Generator;
import i5.las2peer.services.gitHubProxyService.GitHubProxyService;


/**
 * 
 * Central test class for the CAE-Code-Generation-Service.
 *
 */
public class CodeGenerationServiceTest {

  private static LocalNode node;

  private static final String codeGenerationService =
      CodeGenerationService.class.getName();

  private static final String gitHubProxyService = GitHubProxyService.class.getName();

  private static SimpleModel model1;
  private static SimpleModel model2;
  private static SimpleModel model3;
  private static SimpleModel[] model4;
  private static ServiceAgent testService;
  private static ServiceNameVersion serviceNameVersion;

  private static ServiceAgent gitHubProxyTestService;
  private static ServiceNameVersion gitHubProxyServiceNameVersion;

  private static String usedGitHost = null;
  private static String gitHubOrganization = null;
  private static String gitHubUser = null;
  private static String gitHubPassword = <PASSWORD>;
  @SuppressWarnings("unused")
  private static String gitHubUserMail = null;
  @SuppressWarnings("unused")
  private static String templateRepository = null;


  /**
   * 
   * Called before the tests start. Sets up the node and loads test models for later usage.
   * 
   * @throws Exception
   * 
   */
  @BeforeClass
  public static void startServer() throws Exception {
    // load models
    String modelPath1 = "./testModels/My First Testservice.model";
    String modelPath2 = "./testModels/My First Testservice without DB.model";
    String modelPath3 = "./testModels/My Test Widget.model";
    String[] modelPathes4 = new String[7];
    modelPathes4[0] = "./testModels/applicationTestModel/CAE Example Application.model";
    modelPathes4[1] = "./testModels/applicationTestModel/Graph Widget.model";
    modelPathes4[2] = "./testModels/applicationTestModel/LAS2peer Load Store Graph Service.model";
    modelPathes4[3] = "./testModels/applicationTestModel/LAS2peer Video List Service.model";
    modelPathes4[4] = "./testModels/applicationTestModel/Load Store Widget.model";
    modelPathes4[5] = "./testModels/applicationTestModel/Video List Widget.model";
    modelPathes4[6] = "./testModels/applicationTestModel/Video Player Widget.model";

    Properties properties = new Properties();
    String propertiesFile =
        "./etc/i5.las2peer.services.codeGenerationService.CodeGenerationService.properties";

    try {
      InputStream file1 = new FileInputStream(modelPath1);
      InputStream buffer1 = new BufferedInputStream(file1);
      ObjectInput input1 = new ObjectInputStream(buffer1);
      model1 = (SimpleModel) input1.readObject();
      InputStream file2 = new FileInputStream(modelPath2);
      InputStream buffer2 = new BufferedInputStream(file2);
      ObjectInput input2 = new ObjectInputStream(buffer2);
      model2 = (SimpleModel) input2.readObject();
      InputStream file3 = new FileInputStream(modelPath3);
      InputStream buffer3 = new BufferedInputStream(file3);
      ObjectInput input3 = new ObjectInputStream(buffer3);
      model3 = (SimpleModel) input3.readObject();
      model4 = new SimpleModel[modelPathes4.length];
      int i = 0;
      for (String modelPath4 : modelPathes4) {
        InputStream file4 = new FileInputStream(modelPath4);
        InputStream buffer4 = new BufferedInputStream(file4);
        ObjectInput input4 = new ObjectInputStream(buffer4);
        model4[i] = (SimpleModel) input4.readObject();
        input4.close();
        i++;
      }
      input1.close();
      input2.close();
      input3.close();

      FileReader reader = new FileReader(propertiesFile);
      properties.load(reader);
      gitHubUser = properties.getProperty("gitHubUser");
      gitHubUserMail = properties.getProperty("gitHubUserMail");
      gitHubOrganization = properties.getProperty("gitHubOrganization");
      templateRepository = properties.getProperty("templateRepository");
      gitHubPassword = properties.getProperty("gitHubPassword");
      usedGitHost = properties.getProperty("usedGitHost");

    } catch (IOException ex) {
      fail("Error reading test models and configuration file!");
    }


    // start node
    node = LocalNode.newNode();
    node.launch();

    serviceNameVersion = new ServiceNameVersion(codeGenerationService, "0.1");
    testService = ServiceAgent.createServiceAgent(serviceNameVersion, "a pass");
    testService.unlockPrivateKey("a pass");

    gitHubProxyServiceNameVersion = new ServiceNameVersion(gitHubProxyService, "0.1");
    gitHubProxyTestService = ServiceAgent.createServiceAgent(gitHubProxyServiceNameVersion, "a pass");
    gitHubProxyTestService.unlockPrivateKey("a pass");


    node.registerReceiver(gitHubProxyTestService);
    node.registerReceiver(testService);

    // waiting here not needed because no connector is running!

  }


  /**
   * 
   * Called after the tests have finished. Deletes all test repositories and shuts down the server.
   * Just comment out repositories you want to check on after the tests.
   * 
   * @throws Exception
   * 
   **/
  @AfterClass
  public static void shutDownServer() throws Exception {
    String model1GitHubName = "microservice-" + model1.getName().replace(" ", "-");
    String model2GitHubName = "microservice-" + model2.getName().replace(" ", "-");
    String model3GitHubName = "frontendComponent-" + model3.getName().replace(" ", "-");
    String model4GitHubName = "application-" + model4[0].getName().replace(" ", "-");

    try {
      Generator.deleteRemoteRepository(model1GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword, usedGitHost);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model2GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword, usedGitHost);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model3GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword, usedGitHost);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    try {
      Generator.deleteRemoteRepository(model4GitHubName, gitHubOrganization, gitHubUser,
          gitHubPassword, usedGitHost);
    } catch (GitHubException e) {
      e.printStackTrace();
      // that's ok, maybe some error / failure in previous tests caused this
      // catch it, to make sure that every other repository gets deleted
    }
    node.shutDown();
    node = null;
    LocalNode.reset();
  }


  /**
{'fraction_non_alphanumeric': 0.09459897555598781, 'fraction_numerical': 0.013226998638397198, 'mean_word_length': 3.907413299395482, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4336406', 'n_tokens_mistral': 4219, 'n_tokens_neox': 4080, 'n_words': 1077}