<commit_before>package org.ovirt.engine.core.bll.validator.storage;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.failsWith;
import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.isValid;
import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.ovirt.engine.core.bll.ValidationResult;
import org.ovirt.engine.core.common.FeatureSupported;
import org.ovirt.engine.core.common.businessentities.StorageDomain;
import org.ovirt.engine.core.common.businessentities.StorageDomainType;
import org.ovirt.engine.core.common.businessentities.StorageFormatType;
import org.ovirt.engine.core.common.businessentities.StoragePool;
import org.ovirt.engine.core.common.businessentities.StoragePoolIsoMap;
import org.ovirt.engine.core.common.businessentities.storage.StorageType;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.StorageDomainDao;
import org.ovirt.engine.core.dao.StoragePoolDao;
import org.ovirt.engine.core.dao.StoragePoolIsoMapDao;
import org.ovirt.engine.core.utils.MockConfigRule;

@RunWith(MockitoJUnitRunner.class)
public class StorageDomainToPoolRelationValidatorTest {
    private static final Version UNSUPPORTED_VERSION = Version.v3_0;
    private static final Version SUPPORTED_VERSION = Version.v3_3;

    private StorageDomain storageDomain;
    private StoragePool storagePool;
    private StorageDomainToPoolRelationValidator validator;

    @Mock
    private StoragePoolDao storagePoolDao;
    @Mock
    private StorageDomainDao storageDomainDao;
    @Mock
    private StoragePoolIsoMapDao storagePoolIsoMapDao;

    @Before
    public void setUp() throws Exception {
        // Create the storage domain.
        storageDomain = new StorageDomain();
        storageDomain.setId(Guid.newGuid());
        storageDomain.setStorageFormat(StorageFormatType.V3);
        storageDomain.setStorageType(StorageType.NFS);

        // Create the storage pool.
        storagePool = new StoragePool();
        storagePool.setId(Guid.newGuid());
        storagePool.setCompatibilityVersion(Version.v3_5);

        when(storagePoolIsoMapDao.getAllForStorage(any(Guid.class))).thenReturn(new ArrayList<StoragePoolIsoMap>());
        spyValidator();
    }

    private void spyValidator() {
        // Create the spied validators.
        validator = spy(new StorageDomainToPoolRelationValidator(storageDomain.getStorageStaticData(), storagePool));

        doReturn(storagePoolIsoMapDao).when(validator).getStoragePoolIsoMapDao();

        doReturn(storagePoolDao).when(validator).getStoragePoolDao();
        doReturn(storageDomainDao).when(validator).getStorageDomainDao();
    }

    @ClassRule
    public static MockConfigRule mcr = new MockConfigRule(
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_1.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_2.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_3.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_6.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, UNSUPPORTED_VERSION.toString(), false),
            mockConfig(ConfigValues.PosixStorageEnabled, SUPPORTED_VERSION.toString(), true),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, UNSUPPORTED_VERSION.toString(), false),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, SUPPORTED_VERSION.toString(), true)
    );

    @Test
    public void testAttachOnValidDomain() {
        assertTrue("Attaching a valid domain to attach was failed",
                validator.validateDomainCanBeAttachedToPool().isValid());
    }

    /**
     * Mixed types are not allowed on version lower than V3.4, test that attempting to attach a domain of different type
     * than what already exists in the data center will fail for versions 3.0 to 3.3 inclusive
     */
    @Test
    public void testMixedTypesOnAllVersions() {
        // Use an old format so the domain will be able to attach to each DC.
        storageDomain.setStorageFormat(StorageFormatType.V1);

        // Mock the pool to have a NFS type domain
        when(storagePoolDao.getStorageTypesInPool(storagePool.getId())).thenReturn(Collections.singletonList(StorageType.NFS));

        storageDomain.setStorageType(StorageType.ISCSI);
        for (Version version : Version.ALL) {
            if (version.compareTo(Version.v3_0) >= 0) { // No reason to test unsupported versions
                testAddingMixedTypes(version, FeatureSupported.mixedDomainTypesOnDataCenter(version));
            }
        }
    }

    private void testAddingMixedTypes(Version version, boolean addingMixedTypesShouldSucceed) {
        storagePool.setCompatibilityVersion(version);

        ValidationResult attachDomainResult = validator.validateDomainCanBeAttachedToPool();
        if (addingMixedTypesShouldSucceed) {
            assertTrue("Attaching an ISCSI domain to a pool with NFS domain with with mixed type allowed failed, version: " + version, attachDomainResult.isValid());
        }
        else {
            assertFalse("Attaching an ISCSI domain to a pool with NFS domain with no mixed type allowed succeeded, version: " + version, attachDomainResult.isValid());<commit_msg>core: rename test method<commit_after>package org.ovirt.engine.core.bll.validator.storage;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.failsWith;
import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.isValid;
import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.ovirt.engine.core.bll.ValidationResult;
import org.ovirt.engine.core.common.FeatureSupported;
import org.ovirt.engine.core.common.businessentities.StorageDomain;
import org.ovirt.engine.core.common.businessentities.StorageDomainType;
import org.ovirt.engine.core.common.businessentities.StorageFormatType;
import org.ovirt.engine.core.common.businessentities.StoragePool;
import org.ovirt.engine.core.common.businessentities.StoragePoolIsoMap;
import org.ovirt.engine.core.common.businessentities.storage.StorageType;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.StorageDomainDao;
import org.ovirt.engine.core.dao.StoragePoolDao;
import org.ovirt.engine.core.dao.StoragePoolIsoMapDao;
import org.ovirt.engine.core.utils.MockConfigRule;

@RunWith(MockitoJUnitRunner.class)
public class StorageDomainToPoolRelationValidatorTest {
    private static final Version UNSUPPORTED_VERSION = Version.v3_0;
    private static final Version SUPPORTED_VERSION = Version.v3_3;

    private StorageDomain storageDomain;
    private StoragePool storagePool;
    private StorageDomainToPoolRelationValidator validator;

    @Mock
    private StoragePoolDao storagePoolDao;
    @Mock
    private StorageDomainDao storageDomainDao;
    @Mock
    private StoragePoolIsoMapDao storagePoolIsoMapDao;

    @Before
    public void setUp() throws Exception {
        // Create the storage domain.
        storageDomain = new StorageDomain();
        storageDomain.setId(Guid.newGuid());
        storageDomain.setStorageFormat(StorageFormatType.V3);
        storageDomain.setStorageType(StorageType.NFS);

        // Create the storage pool.
        storagePool = new StoragePool();
        storagePool.setId(Guid.newGuid());
        storagePool.setCompatibilityVersion(Version.v3_5);

        when(storagePoolIsoMapDao.getAllForStorage(any(Guid.class))).thenReturn(new ArrayList<StoragePoolIsoMap>());
        spyValidator();
    }

    private void spyValidator() {
        // Create the spied validators.
        validator = spy(new StorageDomainToPoolRelationValidator(storageDomain.getStorageStaticData(), storagePool));

        doReturn(storagePoolIsoMapDao).when(validator).getStoragePoolIsoMapDao();

        doReturn(storagePoolDao).when(validator).getStoragePoolDao();
        doReturn(storageDomainDao).when(validator).getStorageDomainDao();
    }

    @ClassRule
    public static MockConfigRule mcr = new MockConfigRule(
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_0.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_1.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_2.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_3.toString(), false),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_4.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_5.toString(), true),
            mockConfig(ConfigValues.MixedDomainTypesInDataCenter, Version.v3_6.toString(), true),
            mockConfig(ConfigValues.PosixStorageEnabled, UNSUPPORTED_VERSION.toString(), false),
            mockConfig(ConfigValues.PosixStorageEnabled, SUPPORTED_VERSION.toString(), true),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, UNSUPPORTED_VERSION.toString(), false),
            mockConfig(ConfigValues.GlusterFsStorageEnabled, SUPPORTED_VERSION.toString(), true)
    );

    @Test
    public void testAttachOnValidDomain() {
        assertTrue("Attaching a valid domain to attach was failed",
                validator.validateDomainCanBeAttachedToPool().isValid());
    }

    /**
     * Mixed types are not allowed on version lower than V3.4, test that attempting to attach a domain of different type
     * than what already exists in the data center will fail for versions 3.0 to 3.3 inclusive
     */
    @Test
    public void testMixedTypesOnAllVersions() {
        // Use an old format so the domain will be able to attach to each DC.
        storageDomain.setStorageFormat(StorageFormatType.V1);

        // Mock the pool to have a NFS type domain
        when(storagePoolDao.getStorageTypesInPool(storagePool.getId())).thenReturn(Collections.singletonList(StorageType.NFS));

        storageDomain.setStorageType(StorageType.ISCSI);
        for (Version version : Version.ALL) {
            if (version.compareTo(Version.v3_0) >= 0) { // No reason to test unsupported versions
                assertAddingMixedTypes(version, FeatureSupported.mixedDomainTypesOnDataCenter(version));
            }
        }
    }

    private void assertAddingMixedTypes(Version version, boolean addingMixedTypesShouldSucceed) {
        storagePool.setCompatibilityVersion(version);

        ValidationResult attachDomainResult = validator.validateDomainCanBeAttachedToPool();
        if (addingMixedTypesShouldSucceed) {
            assertTrue("Attaching an ISCSI domain to a pool with NFS domain with with mixed type allowed failed, version: " + version, attachDomainResult.isValid());
        }
        else {
            assertFalse("Attaching an ISCSI domain to a pool with NFS domain with no mixed type allowed succeeded, version: " + version, attachDomainResult.isValid());
{'fraction_non_alphanumeric': 0.09069984895346328, 'fraction_numerical': 0.006185715313241746, 'mean_word_length': 4.20749063670412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1380304', 'n_tokens_mistral': 3854, 'n_tokens_neox': 3864, 'n_words': 736}