<commit_before>/*
 * Copyright © 2010 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.redhat.rhevm.api.powershell.resource;

import java.net.URI;
import java.text.MessageFormat;

import java.util.concurrent.Executor;

import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.UriBuilder;

import com.redhat.rhevm.api.common.resource.UriInfoProvider;
import com.redhat.rhevm.api.model.CdRom;
import com.redhat.rhevm.api.model.CdRoms;
import com.redhat.rhevm.api.model.Floppy;
import com.redhat.rhevm.api.model.Floppies;
import com.redhat.rhevm.api.model.Disk;
import com.redhat.rhevm.api.model.Disks;
import com.redhat.rhevm.api.model.DiskType;
import com.redhat.rhevm.api.model.File;
import com.redhat.rhevm.api.model.Network;
import com.redhat.rhevm.api.model.NIC;
import com.redhat.rhevm.api.model.Nics;
import com.redhat.rhevm.api.model.VM;
import com.redhat.rhevm.api.powershell.util.PowerShellCmd;
import com.redhat.rhevm.api.powershell.util.PowerShellParser;
import com.redhat.rhevm.api.powershell.util.PowerShellPool;
import com.redhat.rhevm.api.powershell.util.PowerShellPoolMap;

import org.junit.Test;

import org.junit.runner.RunWith;

import static org.easymock.classextension.EasyMock.expect;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replayAll;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { PowerShellCmd.class })
public class PowerShellDevicesResourceTest
    extends AbstractPowerShellResourceTest<VM, PowerShellVmResource> {

    private static final String VM_NAME = "1234";
    private static final String VM_ID = Integer.toString(VM_NAME.hashCode());
    private static final String DISK_NAME = "3456";
    private static final String DISK_ID = Integer.toString(DISK_NAME.hashCode());
    private static final String NIC_NAME = "eth11";
    private static final String NIC_ID = Integer.toString(NIC_NAME.hashCode());
    private static final String NETWORK_NAME = "net1";
    private static final String NETWORK_ID = Integer.toString(NETWORK_NAME.hashCode());
    private static final String DATA_CENTER_ID = "999";
    private static final String ISO_NAME = PowerShellVmsResourceTest.ISO_NAME;
    private static final String VFD_NAME = PowerShellVmsResourceTest.VFD_NAME;

    private static final String CDROM_ID = Integer.toString("cdrom".hashCode());
    private static final String FLOPPY_ID = Integer.toString("floppy".hashCode());

    private static final String GET_CDROMS_CMD = "get-vm \"" + VM_ID + "\"";
    private static final String UPDATE_CDROM_CMD = "$v = get-vm \"{0}\";$v.cdisopath = \"{1}\";update-vm -vmobject $v";

    private static final String GET_FLOPPIES_CMD = "get-vm \"" + VM_ID + "\"";
    private static final String UPDATE_FLOPPY_CMD = "$v = get-vm \"{0}\";$v.floppypath = \"{1}\";update-vm -vmobject $v";

    private static final long DISK_SIZE = 10;
    private static final long DISK_SIZE_BYTES = DISK_SIZE * 1024 * 1024 * 1024;

    private static final String[] diskArgs = new String[] { Long.toString(DISK_SIZE_BYTES), "", "", "", "" };
    private static final String[] networkArgs = new String[] { DATA_CENTER_ID};
    private static final String[] nicArgs = new String[] { NETWORK_NAME };

    private static final String GET_DISKS_CMD = "$v = get-vm \"" + VM_ID + "\";$v.GetDiskImages()";

    private static final String ADD_DISK_COMMAND = "$d = new-disk -disksize {0} -disktype System;$v = get-vm \"{1}\";add-disk -diskobject $d -vmobject $v";
    private static final String REMOVE_DISK_COMMAND = "remove-disk -vmid \"{0}\" -diskids \"{1}\"";

    private static final String GET_NICS_CMD = "$v = get-vm \"" + VM_ID + "\";$v.GetNetworkAdapters()";

    public static final String LOOKUP_NETWORK_ID_COMMAND = "$n = get-networks;foreach ($i in $n) {  if ($i.name -eq \"" + NETWORK_NAME + "\") {    $i  }}";

    private static final String ADD_NIC_COMMAND = "$v = get-vm \"{0}\";foreach ($i in get-networks) '{'  if ($i.networkid -eq \"{1}\") '{    $n = $i  }}'add-networkadapter -vmobject $v -interfacename \"{2}\" -networkname $n.name";
    private static final String REMOVE_NIC_COMMAND = "$v = get-vm \"{0}\";foreach ($i in $v.GetNetworkAdapters()) '{'  if ($i.id -eq \"{1}\") '{    $n = $i  }}'remove-networkadapter -vmobject $v -networkadapterobject $n";

    protected PowerShellVmResource getResource(Executor executor, PowerShellPoolMap poolMap, PowerShellParser parser, UriInfoProvider uriProvider) {
        return new PowerShellVmResource(VM_ID, executor, uriProvider, poolMap, parser);
    }

    protected String formatVm(String name) {<commit_msg>powershell: further fix for PowerShellDevicesResourceTest<commit_after>/*
 * Copyright © 2010 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.redhat.rhevm.api.powershell.resource;

import java.net.URI;
import java.text.MessageFormat;

import java.util.concurrent.Executor;

import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.UriBuilder;

import com.redhat.rhevm.api.common.resource.UriInfoProvider;
import com.redhat.rhevm.api.model.CdRom;
import com.redhat.rhevm.api.model.CdRoms;
import com.redhat.rhevm.api.model.Floppy;
import com.redhat.rhevm.api.model.Floppies;
import com.redhat.rhevm.api.model.Disk;
import com.redhat.rhevm.api.model.Disks;
import com.redhat.rhevm.api.model.DiskType;
import com.redhat.rhevm.api.model.File;
import com.redhat.rhevm.api.model.Network;
import com.redhat.rhevm.api.model.NIC;
import com.redhat.rhevm.api.model.Nics;
import com.redhat.rhevm.api.model.VM;
import com.redhat.rhevm.api.powershell.util.PowerShellCmd;
import com.redhat.rhevm.api.powershell.util.PowerShellParser;
import com.redhat.rhevm.api.powershell.util.PowerShellPool;
import com.redhat.rhevm.api.powershell.util.PowerShellPoolMap;

import org.junit.Test;

import org.junit.runner.RunWith;

import static org.easymock.classextension.EasyMock.expect;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replayAll;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { PowerShellCmd.class })
public class PowerShellDevicesResourceTest
    extends AbstractPowerShellResourceTest<VM, PowerShellVmResource> {

    private static final String VM_NAME = "1234";
    private static final String VM_ID = Integer.toString(VM_NAME.hashCode());
    private static final String DISK_NAME = "3456";
    private static final String DISK_ID = Integer.toString(DISK_NAME.hashCode());
    private static final String NIC_NAME = "eth11";
    private static final String NIC_ID = Integer.toString(NIC_NAME.hashCode());
    private static final String NETWORK_NAME = "net1";
    private static final String NETWORK_ID = Integer.toString(NETWORK_NAME.hashCode());
    private static final String DATA_CENTER_ID = "999";
    private static final String ISO_NAME = PowerShellVmsResourceTest.ISO_NAME;
    private static final String VFD_NAME = PowerShellVmsResourceTest.VFD_NAME;

    private static final String CDROM_ID = Integer.toString("cdrom".hashCode());
    private static final String FLOPPY_ID = Integer.toString("floppy".hashCode());

    private static final String GET_CDROMS_CMD = "get-vm \"" + VM_ID + "\"";
    private static final String UPDATE_CDROM_CMD = "$v = get-vm \"{0}\";$v.cdisopath = \"{1}\";try '{'mount-disk -vmobject $v -isofilename \"{1}\";'} catch {update-vm -vmobject $v;}'";

    private static final String GET_FLOPPIES_CMD = "get-vm \"" + VM_ID + "\"";
    private static final String UPDATE_FLOPPY_CMD = "$v = get-vm \"{0}\";$v.floppypath = \"{1}\";update-vm -vmobject $v";

    private static final long DISK_SIZE = 10;
    private static final long DISK_SIZE_BYTES = DISK_SIZE * 1024 * 1024 * 1024;

    private static final String[] diskArgs = new String[] { Long.toString(DISK_SIZE_BYTES), "", "", "", "" };
    private static final String[] networkArgs = new String[] { DATA_CENTER_ID};
    private static final String[] nicArgs = new String[] { NETWORK_NAME };

    private static final String GET_DISKS_CMD = "$v = get-vm \"" + VM_ID + "\";$v.GetDiskImages()";

    private static final String ADD_DISK_COMMAND = "$d = new-disk -disksize {0} -disktype System;$v = get-vm \"{1}\";add-disk -diskobject $d -vmobject $v";
    private static final String REMOVE_DISK_COMMAND = "remove-disk -vmid \"{0}\" -diskids \"{1}\"";

    private static final String GET_NICS_CMD = "$v = get-vm \"" + VM_ID + "\";$v.GetNetworkAdapters()";

    public static final String LOOKUP_NETWORK_ID_COMMAND = "$n = get-networks;foreach ($i in $n) {  if ($i.name -eq \"" + NETWORK_NAME + "\") {    $i  }}";

    private static final String ADD_NIC_COMMAND = "$v = get-vm \"{0}\";foreach ($i in get-networks) '{'  if ($i.networkid -eq \"{1}\") '{    $n = $i  }}'add-networkadapter -vmobject $v -interfacename \"{2}\" -networkname $n.name";
    private static final String REMOVE_NIC_COMMAND = "$v = get-vm \"{0}\";foreach ($i in $v.GetNetworkAdapters()) '{'  if ($i.id -eq \"{1}\") '{    $n = $i  }}'remove-networkadapter -vmobject $v -networkadapterobject $n";

    protected PowerShellVmResource getResource(Executor executor, PowerShellPoolMap poolMap, PowerShellParser parser, UriInfoProvider uriProvider) {
        return new PowerShellVmResource(VM_ID, executor, uriProvider, poolMap, parser);
    }

    protected String formatVm(String name) {
{'fraction_non_alphanumeric': 0.12347111865012053, 'fraction_numerical': 0.010624051423980001, 'mean_word_length': 4.388167388167388, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '2376733', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3591, 'n_words': 977}