<commit_before>/*
 * Copyright 2015 Rackspace
 *
 *    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
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    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 com.rackspacecloud.blueflood.service;

import com.rackspacecloud.blueflood.io.astyanax.AstyanaxReader;
import com.rackspacecloud.blueflood.io.astyanax.AstyanaxWriter;
import com.rackspacecloud.blueflood.io.DiscoveryIO;
import com.rackspacecloud.blueflood.io.SearchResult;
import com.rackspacecloud.blueflood.types.IMetric;
import com.rackspacecloud.blueflood.types.Locator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

public class EnumValidatorTest {

    AstyanaxReader readerMock = mock(AstyanaxReader.class);
    AstyanaxWriter writerMock = mock(AstyanaxWriter.class);
    DiscoveryIO discoveryIOMock = mock(DiscoveryIO.class);

    String tenant_id = "tenant1";
    String metric_name = "metric1";
    Locator locator1 = Locator.createLocatorFromPathComponents(tenant_id, metric_name);
    HashSet<Locator> locators;

    @Before
    public void setup() {
        // set threshold
        System.setProperty(CoreConfig.ENUM_UNIQUE_VALUES_THRESHOLD.name(), "3");

        // set locators
        locators = new HashSet<Locator>();
        locators.add(locator1);
    }

    @After
    public void tearDown() {
        System.clearProperty(CoreConfig.ENUM_UNIQUE_VALUES_THRESHOLD.name());
    }

    private EnumValidator setupEnumValidatorWithMock(Set<Locator> locators) {
        EnumValidator enumValidator = new EnumValidator(locators);
        enumValidator.setReader(readerMock);
        enumValidator.setWriter(writerMock);
        enumValidator.setDiscoveryIO(discoveryIOMock);
        return enumValidator;
    }

    @Test
    public void testNullLocators() throws Exception {
        // create EnumValidator with locators set = null
        EnumValidator validator = setupEnumValidatorWithMock(null);

        // execute validator
        validator.run();

        // verify no writes to cassandra and elasticsearch<commit_msg>Re-arrange some setup code.<commit_after>/*
 * Copyright 2015 Rackspace
 *
 *    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
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    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 com.rackspacecloud.blueflood.service;

import com.rackspacecloud.blueflood.io.astyanax.AstyanaxReader;
import com.rackspacecloud.blueflood.io.astyanax.AstyanaxWriter;
import com.rackspacecloud.blueflood.io.DiscoveryIO;
import com.rackspacecloud.blueflood.io.SearchResult;
import com.rackspacecloud.blueflood.types.IMetric;
import com.rackspacecloud.blueflood.types.Locator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

public class EnumValidatorTest {

    final String tenant_id = "tenant1";
    final String metric_name = "metric1";

    Locator locator1;
    HashSet<Locator> locators;

    AstyanaxReader readerMock;
    AstyanaxWriter writerMock;
    DiscoveryIO discoveryIOMock;

    @Before
    public void setup() {
        // set threshold
        System.setProperty(CoreConfig.ENUM_UNIQUE_VALUES_THRESHOLD.name(), "3");

        // set locators
        locator1 = Locator.createLocatorFromPathComponents(tenant_id, metric_name);
        locators = new HashSet<Locator>();
        locators.add(locator1);

        // set up mocks
        readerMock = mock(AstyanaxReader.class);
        writerMock = mock(AstyanaxWriter.class);
        discoveryIOMock = mock(DiscoveryIO.class);
    }

    @After
    public void tearDown() {
        System.clearProperty(CoreConfig.ENUM_UNIQUE_VALUES_THRESHOLD.name());
    }

    private EnumValidator setupEnumValidatorWithMock(Set<Locator> locators) {
        EnumValidator enumValidator = new EnumValidator(locators);
        enumValidator.setReader(readerMock);
        enumValidator.setWriter(writerMock);
        enumValidator.setDiscoveryIO(discoveryIOMock);
        return enumValidator;
    }

    @Test
    public void testNullLocators() throws Exception {
        // create EnumValidator with locators set = null
        EnumValidator validator = setupEnumValidatorWithMock(null);

        // execute validator
        validator.run();

        // verify no writes to cassandra and elasticsearch
{'fraction_non_alphanumeric': 0.08362889677058055, 'fraction_numerical': 0.005040134403584096, 'mean_word_length': 3.517706576728499, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3145237', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1512, 'n_words': 393}