<commit_before>/**
 * Copyright (C) GRyCAP - I3M - UPV 
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package es.upv.i3m.grycap.im;

import es.upv.i3m.grycap.ImTestWatcher;
import es.upv.i3m.grycap.file.NoNullOrEmptyFile;
import es.upv.i3m.grycap.file.Utf8File;
import es.upv.i3m.grycap.im.exceptions.FileException;
import es.upv.i3m.grycap.im.exceptions.ImClientException;
import es.upv.i3m.grycap.im.exceptions.ToscaContentTypeNotSupportedException;
import es.upv.i3m.grycap.im.pojo.InfOutputValues;
import es.upv.i3m.grycap.im.pojo.InfrastructureState;
import es.upv.i3m.grycap.im.pojo.InfrastructureUri;
import es.upv.i3m.grycap.im.pojo.InfrastructureUris;
import es.upv.i3m.grycap.im.pojo.Property;
import es.upv.i3m.grycap.im.pojo.VirtualMachineInfo;
import es.upv.i3m.grycap.im.rest.client.BodyContentType;
import es.upv.i3m.grycap.logger.ImJavaApiLogger;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class InfrastructureManagerTest extends ImTestWatcher {

  private static InfrastructureManager im;
  private String infrastructureId;
  private static final String IM_DUMMY_PROVIDER_URL =
      "https://servproject.i3m.upv.es:8811";

  private static final String AUTH_FILE_PATH = "./src/test/resources/auth.dat";
  private static final String RADL_ALTER_VM_FILE_PATH =
      "./src/test/resources/radls/alter-vm.radl";
  private static final String RADL_JSON_ALTER_VM_FILE_PATH =
      "./src/test/resources/radls/alter-vm.json";
  private static final String TOSCA_FILE_PATH =
      "./src/test/resources/tosca/galaxy_tosca.yaml";
  private static final String TOSCA_EXTRA_NODE_FILE_PATH =
      "./src/test/resources/tosca/galaxy_tosca_2_nodes.yaml";

  private static final String VM_DEFAULT_ID = "0";

  private String getInfrastructureId() {
    return infrastructureId;
  }

  private void setInfrastructureId(String infrastructureId) {
    this.infrastructureId = infrastructureId;
  }

  private InfrastructureManager getIm() {
    return im;
  }

  /**
   * Check if the VM state is the same as the 'state' parameter.
   * 
   * @param vmId
   *          : id of the virtual machine
   * @param vmState
   *          : state of the virtual machine
   * @throws ImClientException
   *           : exception in the IM client
   */
  private void checkVmState(String vmId, States vmState)
      throws ImClientException {
    Property vmProperty =
        getIm().getVmProperty(getInfrastructureId(), vmId, VmProperties.STATE);
    Assert.assertEquals(vmState.toString(), vmProperty.getValue());
  }

  /**
   * Creates a new rest client.
   */
  @BeforeClass
  public static void setRestClient() {
    try {
      im = new InfrastructureManager(IM_DUMMY_PROVIDER_URL,
          Paths.get(AUTH_FILE_PATH));
    } catch (ImClientException exception) {
      ImJavaApiLogger.severe(InfrastructureManagerTest.class,
          exception.getMessage());
      Assert.fail();
    }
  }

  /**
   * Creates a new infrastructure.
   * 
   * @throws ImClientException
   */
  @Before
  public void createInfrastructure() throws ImClientException {
    InfrastructureUri newInfrastructureUri = getIm()
        .createInfrastructure(readFile(TOSCA_FILE_PATH), BodyContentType.TOSCA);
    String uri = newInfrastructureUri.getUri();
    Assert.assertEquals(false, uri.isEmpty());
    setInfrastructureId(newInfrastructureUri.getInfrastructureId());
  }

  private String readFile(String filePath) throws FileException {
    return new NoNullOrEmptyFile(new Utf8File(Paths.get(filePath))).read();
  }

  @After
  public void destroyInfrastructure() throws ImClientException {
    getIm().destroyInfrastructure(getInfrastructureId());
  }

  @Test
  public void testCreateAndDestroyInfrastructure() {
    // Create empty test to check the @Before and @After methods
  }

  @Test
  public void testInfrastructuresList() throws ImClientException {
    InfrastructureUris infUris = getIm().getInfrastructureList();
    Assert.assertEquals(false, infUris.getUris().isEmpty());
  }

  @Test
  public void testInfrastructureInfo() throws ImClientException {
    String expected = IM_DUMMY_PROVIDER_URL + "/infrastructures/"
        + getInfrastructureId() + "/vms/0";
    InfrastructureUris infUris =
        getIm().getInfrastructureInfo(getInfrastructureId());

    Assert.assertEquals(1, infUris.getUris().size());
    InfrastructureUri actualUri = infUris.getUris().get(0);
    Assert.assertEquals(expected, actualUri.getUri());
  }

  @Test
  public void testGetVmInfo() throws ImClientException {
    VirtualMachineInfo vmInfo =
        getIm().getVmInfo(getInfrastructureId(), VM_DEFAULT_ID);
    List<Map<String, Object>> vmProperties = vmInfo.getVmProperties();

    // Check the value of the first map
    Map<?, ?> info = (Map<String, Object>) vmProperties.get(0);
    Assert.assertEquals("no", info.get("outbound"));

    // Check an internal map inside the map
    Map<?, ?> internalInfo = (Map<String, Object>) vmProperties.get(2);
    List<?> applications = (List<?>) internalInfo.get("disk.0.applications");
    Map<?, ?> applicationsInfo = (Map<?, ?>) applications.get(0);
    Assert.assertEquals("ansible.modules.indigo-dc.zabbix-agent",
        applicationsInfo.get("name"));
  }

  @Test
  public void testGetVmProperty() throws ImClientException {
    // Check for running state because the dummy provider never reaches the
    // 'configured' state
    checkVmState(VM_DEFAULT_ID, States.RUNNING);
  }

  @Test
  public void testGetInfrastructureContMsg()
      throws ImClientException, InterruptedException {
    Thread.sleep(10000);
    Property contextMessage =
        getIm().getInfrastructureContMsg(getInfrastructureId());
    Assert.assertEquals(false, contextMessage.getValue().isEmpty());
  }

  @Test
  public void testGetInfrastructureRadl() throws ImClientException {
    Property infrRadl = getIm().getInfrastructureRadl(getInfrastructureId());
    Assert.assertEquals(false, infrRadl.getValue().isEmpty());
  }

  @Test
  public void testGetInfrastructureState() throws ImClientException {
    getIm().addResource(getInfrastructureId(),
        readFile(TOSCA_EXTRA_NODE_FILE_PATH), BodyContentType.TOSCA);

    InfrastructureState infState =
        getIm().getInfrastructureState(getInfrastructureId());

    Assert.assertEquals("running", infState.getState());
    String vm0State = infState.getVmStates().get("0");
    Assert.assertEquals("running", vm0State);
    String vm1State = infState.getVmStates().get("1");
    Assert.assertEquals("running", vm1State);<commit_msg>Fix failing test<commit_after>/**
 * Copyright (C) GRyCAP - I3M - UPV 
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package es.upv.i3m.grycap.im;

import es.upv.i3m.grycap.ImTestWatcher;
import es.upv.i3m.grycap.file.NoNullOrEmptyFile;
import es.upv.i3m.grycap.file.Utf8File;
import es.upv.i3m.grycap.im.exceptions.FileException;
import es.upv.i3m.grycap.im.exceptions.ImClientException;
import es.upv.i3m.grycap.im.exceptions.ToscaContentTypeNotSupportedException;
import es.upv.i3m.grycap.im.pojo.InfOutputValues;
import es.upv.i3m.grycap.im.pojo.InfrastructureState;
import es.upv.i3m.grycap.im.pojo.InfrastructureUri;
import es.upv.i3m.grycap.im.pojo.InfrastructureUris;
import es.upv.i3m.grycap.im.pojo.Property;
import es.upv.i3m.grycap.im.pojo.VirtualMachineInfo;
import es.upv.i3m.grycap.im.rest.client.BodyContentType;
import es.upv.i3m.grycap.logger.ImJavaApiLogger;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class InfrastructureManagerTest extends ImTestWatcher {

  private static InfrastructureManager im;
  private String infrastructureId;
  private static final String IM_DUMMY_PROVIDER_URL =
      "https://servproject.i3m.upv.es:8811";

  private static final String AUTH_FILE_PATH = "./src/test/resources/auth.dat";
  private static final String RADL_ALTER_VM_FILE_PATH =
      "./src/test/resources/radls/alter-vm.radl";
  private static final String RADL_JSON_ALTER_VM_FILE_PATH =
      "./src/test/resources/radls/alter-vm.json";
  private static final String TOSCA_FILE_PATH =
      "./src/test/resources/tosca/galaxy_tosca.yaml";
  private static final String TOSCA_EXTRA_NODE_FILE_PATH =
      "./src/test/resources/tosca/galaxy_tosca_2_nodes.yaml";

  private static final String VM_DEFAULT_ID = "0";

  private String getInfrastructureId() {
    return infrastructureId;
  }

  private void setInfrastructureId(String infrastructureId) {
    this.infrastructureId = infrastructureId;
  }

  private InfrastructureManager getIm() {
    return im;
  }

  /**
   * Check if the VM state is the same as the 'state' parameter.
   * 
   * @param vmId
   *          : id of the virtual machine
   * @param vmState
   *          : state of the virtual machine
   * @throws ImClientException
   *           : exception in the IM client
   */
  private void checkVmState(String vmId, States vmState)
      throws ImClientException {
    Property vmProperty =
        getIm().getVmProperty(getInfrastructureId(), vmId, VmProperties.STATE);
    Assert.assertEquals(vmState.toString(), vmProperty.getValue());
  }

  /**
   * Creates a new rest client.
   */
  @BeforeClass
  public static void setRestClient() {
    try {
      im = new InfrastructureManager(IM_DUMMY_PROVIDER_URL,
          Paths.get(AUTH_FILE_PATH));
    } catch (ImClientException exception) {
      ImJavaApiLogger.severe(InfrastructureManagerTest.class,
          exception.getMessage());
      Assert.fail();
    }
  }

  /**
   * Creates a new infrastructure.
   * 
   * @throws ImClientException
   */
  @Before
  public void createInfrastructure() throws ImClientException {
    InfrastructureUri newInfrastructureUri = getIm()
        .createInfrastructure(readFile(TOSCA_FILE_PATH), BodyContentType.TOSCA);
    String uri = newInfrastructureUri.getUri();
    Assert.assertEquals(false, uri.isEmpty());
    setInfrastructureId(newInfrastructureUri.getInfrastructureId());
  }

  private String readFile(String filePath) throws FileException {
    return new NoNullOrEmptyFile(new Utf8File(Paths.get(filePath))).read();
  }

  @After
  public void destroyInfrastructure() throws ImClientException {
    getIm().destroyInfrastructure(getInfrastructureId());
  }

  @Test
  public void testCreateAndDestroyInfrastructure() {
    // Create empty test to check the @Before and @After methods
  }

  @Test
  public void testInfrastructuresList() throws ImClientException {
    InfrastructureUris infUris = getIm().getInfrastructureList();
    Assert.assertEquals(false, infUris.getUris().isEmpty());
  }

  @Test
  public void testInfrastructureInfo() throws ImClientException {
    String expected = IM_DUMMY_PROVIDER_URL + "/infrastructures/"
        + getInfrastructureId() + "/vms/0";
    InfrastructureUris infUris =
        getIm().getInfrastructureInfo(getInfrastructureId());

    Assert.assertEquals(1, infUris.getUris().size());
    InfrastructureUri actualUri = infUris.getUris().get(0);
    Assert.assertEquals(expected, actualUri.getUri());
  }

  @Test
  public void testGetVmInfo() throws ImClientException {
    VirtualMachineInfo vmInfo =
        getIm().getVmInfo(getInfrastructureId(), VM_DEFAULT_ID);
    List<Map<String, Object>> vmProperties = vmInfo.getVmProperties();

    // Check the value of the first map
    Map<?, ?> info = (Map<String, Object>) vmProperties.get(0);
    Assert.assertEquals("no", info.get("outbound"));

    // Check an internal map inside the map
    Map<?, ?> internalInfo = (Map<String, Object>) vmProperties.get(2);
    List<?> applications = (List<?>) internalInfo.get("disk.0.applications");
    Map<?, ?> applicationsInfo = (Map<?, ?>) applications.get(0);
    Assert.assertEquals("ansible.modules.indigo-dc.zabbix-agent",
        applicationsInfo.get("name"));
  }

  @Test
  public void testGetVmProperty() throws ImClientException {
    // Check for running state because the dummy provider never reaches the
    // 'configured' state
    checkVmState(VM_DEFAULT_ID, States.RUNNING);
  }

  @Test
  public void testGetInfrastructureContMsg()
      throws ImClientException, InterruptedException {
    Thread.sleep(10000);
    Property contextMessage =
        getIm().getInfrastructureContMsg(getInfrastructureId());
    System.out.println(contextMessage.getValue());
    Assert.assertEquals(true, contextMessage.getValue().isEmpty());
  }

  @Test
  public void testGetInfrastructureRadl() throws ImClientException {
    Property infrRadl = getIm().getInfrastructureRadl(getInfrastructureId());
    Assert.assertEquals(false, infrRadl.getValue().isEmpty());
  }

  @Test
  public void testGetInfrastructureState() throws ImClientException {
    getIm().addResource(getInfrastructureId(),
        readFile(TOSCA_EXTRA_NODE_FILE_PATH), BodyContentType.TOSCA);

    InfrastructureState infState =
        getIm().getInfrastructureState(getInfrastructureId());

    Assert.assertEquals("running", infState.getState());
    String vm0State = infState.getVmStates().get("0");
    Assert.assertEquals("running", vm0State);
    String vm1State = infState.getVmStates().get("1");
    Assert.assertEquals("running", vm1State);
{'fraction_non_alphanumeric': 0.10898096612382327, 'fraction_numerical': 0.006459149316292174, 'mean_word_length': 4.133686067019401, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6439964', 'n_tokens_mistral': 4502, 'n_tokens_neox': 4433, 'n_words': 960}