<commit_before>package org.ovirt.optimizer.service.problemspace;

import org.junit.Test;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.ovirt.engine.sdk.entities.Host;
import org.ovirt.engine.sdk.entities.HostedEngine;
import org.ovirt.engine.sdk.entities.Status;
import org.ovirt.engine.sdk.entities.VM;
import org.ovirt.optimizer.service.facts.PolicyUnit;
import org.ovirt.optimizer.service.test.ClusterFeatures;
import org.ovirt.optimizer.service.test.TestOptimizer;

import java.util.EnumSet;

import static org.junit.Assert.*;

public class SituationTest {

    /**
     * Test whether a pinned to host VM can be started
     */
    @Test
    public void testStartPinnedVm() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(host);
        vm.getPlacementPolicy().setAffinity("pinned");

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm, host)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertEquals(0, result.getHardScore());
    }

    /**
     * Test whether a pinned to host VM can be started on different host
     */
    @Test
    public void testStartPinnedVmOnWrongHost() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);
        Host host2 = optimizer.createHost("H2", 1000000000L);

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(host);
        vm.getPlacementPolicy().setAffinity("pinned");

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm, host2)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertNotEquals(0, result.getHardScore());
    }

    /**
     * Test whether a pinned to host VM can be started on proper host
     * with migration of other VM performed first
     */
    @Test
    public void testStartPinnedVmWithMigration() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);
        Host host2 = optimizer.createHost("H2", 1000000000L);

        // Create new instance of host to mimic the SDK's behaviour better
        Host dummyhost = new Host();
        dummyhost.setId("H1");

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(dummyhost);
        vm.getPlacementPolicy().setAffinity("pinned");

        VM vm2 = optimizer.createVm("VM-A", 10000000L);
        vm2.setHost(host);

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm2, host2)
                .addMigration(vm, host)
                .enablePolicyUnit(PolicyUnit.BALANCE_VM_COUNT)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertNotEquals(0, result.getHardScore());
    }

    /**<commit_msg>Fix - bug in situation tests<commit_after>package org.ovirt.optimizer.service.problemspace;

import org.junit.Test;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.ovirt.engine.sdk.entities.Host;
import org.ovirt.engine.sdk.entities.HostedEngine;
import org.ovirt.engine.sdk.entities.Status;
import org.ovirt.engine.sdk.entities.VM;
import org.ovirt.optimizer.service.facts.PolicyUnit;
import org.ovirt.optimizer.service.test.ClusterFeatures;
import org.ovirt.optimizer.service.test.TestOptimizer;

import java.util.EnumSet;

import static org.junit.Assert.*;

public class SituationTest {

    /**
     * Test whether a pinned to host VM can be started
     */
    @Test
    public void testStartPinnedVm() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(host);
        vm.getPlacementPolicy().setAffinity("pinned");

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm, host)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertEquals(0, result.getHardScore());
    }

    /**
     * Test whether a pinned to host VM can be started on different host
     */
    @Test
    public void testStartPinnedVmOnWrongHost() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);
        Host host2 = optimizer.createHost("H2", 1000000000L);

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(host);
        vm.getPlacementPolicy().setAffinity("pinned");

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm, host2)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertNotEquals(0, result.getHardScore());
    }

    /**
     * Test whether a pinned to host VM can be started on proper host
     * with migration of other VM performed first
     */
    @Test
    public void testStartPinnedVmWithMigration() {
        TestOptimizer optimizer = new TestOptimizer(EnumSet.noneOf(ClusterFeatures.class));

        Host host = optimizer.createHost("H1", 1000000000L);
        Host host2 = optimizer.createHost("H2", 1000000000L);

        // Create new instance of host to mimic the SDK's behaviour better
        Host dummyhost = new Host();
        dummyhost.setId("H1");

        VM vm = optimizer.createVm("VM-A", 10000000L);
        vm.getPlacementPolicy().setHost(dummyhost);
        vm.getPlacementPolicy().setAffinity("pinned");

        VM vm2 = optimizer.createVm("VM-B", 10000000L);
        vm2.setHost(host);

        HardSoftScore result = optimizer
                .startVm(vm)
                .addMigration(vm2, host2)
                .addMigration(vm, host)
                .enablePolicyUnit(PolicyUnit.BALANCE_VM_COUNT)
                .enablePolicyUnit(PolicyUnit.PIN_TO_HOST_FILTER)
                .score();

        assertEquals(0, result.getHardScore());
    }

    /**
{'fraction_non_alphanumeric': 0.10704441041347626, 'fraction_numerical': 0.03001531393568147, 'mean_word_length': 2.638440111420613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 18, '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': '7594852', 'n_tokens_mistral': 2128, 'n_tokens_neox': 1919, 'n_words': 378}