<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.ambari.server.controller.nagios;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.configuration.Configuration;
import org.apache.ambari.server.controller.ganglia.TestStreamProvider;
import org.apache.ambari.server.controller.internal.ResourceImpl;
import org.apache.ambari.server.controller.spi.Request;
import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.TemporalInfo;
import org.apache.ambari.server.controller.utilities.PropertyHelper;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Service;
import org.apache.ambari.server.state.ServiceComponent;
import org.apache.ambari.server.state.ServiceComponentHost;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * Tests the nagios property provider
 */
public class NagiosPropertyProviderTest {

  private static final String HOST = "c6401.ambari.apache.org";

  private GuiceModule module = null;
  private Clusters clusters = null;
  private Injector injector = null;

  @Before
  public void setup() throws Exception {

    module = new GuiceModule();
    injector = Guice.createInjector(module);
    NagiosPropertyProvider.init(injector);
    
    
    clusters = injector.getInstance(Clusters.class);
    Cluster cluster = createMock(Cluster.class);
    expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes();

    Service nagiosService = createMock(Service.class);
    expect(cluster.getService("NAGIOS")).andReturn(nagiosService).anyTimes();
    
    ServiceComponent nagiosServiceComponent = createMock(ServiceComponent.class);
    expect(nagiosService.getServiceComponent("NAGIOS_SERVER")).andReturn(
        nagiosServiceComponent).anyTimes();
    
    ServiceComponentHost nagiosScHost = createMock(ServiceComponentHost.class);
    Map<String, ServiceComponentHost> map1 = new HashMap<String, ServiceComponentHost>();
    map1.put(HOST, nagiosScHost);
    expect(nagiosServiceComponent.getServiceComponentHosts()).andReturn(
        map1).anyTimes();
    
    replay(clusters, cluster, nagiosService, nagiosServiceComponent);
  }
  
  @Test
  public void testNoNagiosService() throws Exception {
    Cluster cluster = clusters.getCluster("c1");
    reset(cluster); // simulate an error that NAGIOS not with the cluster
    expect(cluster.getService("NAGIOS")).andThrow(new AmbariException("No Service"));
    replay(cluster);
    
    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");
    
    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", "c1");
    resource.setProperty("ServiceInfo/service_name", "HBASE");
    
    // request with an empty set should get all supported properties
    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(), new HashMap<String, TemporalInfo>());

    Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
    Assert.assertEquals(1, set.size());
    
    Resource res = set.iterator().next();
    
    Map<String, Map<String, Object>> values = res.getPropertiesMap();
    
    Assert.assertFalse("Expected no alerts", values.containsKey("alerts"));
  }


  @Test
  public void testClusterDoesNotExistNPE() throws Exception {
    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");

    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", null);
    resource.setProperty("ServiceInfo/service_name", "HBASE");

    // request with an empty set should get all supported properties
    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(),
            new HashMap<String, TemporalInfo>());

    Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
    Assert.assertEquals(1, set.size());

  }
  
  @Test
  public void testNoNagiosServerCompoonent() throws Exception {
    
    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");
    
    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", "c1");<commit_msg>AMBARI-5359. unittest NagiosPropertyProviderTest fails (dlysnichenko)<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.ambari.server.controller.nagios;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.configuration.Configuration;
import org.apache.ambari.server.controller.ganglia.TestStreamProvider;
import org.apache.ambari.server.controller.internal.ResourceImpl;
import org.apache.ambari.server.controller.spi.Request;
import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.TemporalInfo;
import org.apache.ambari.server.controller.utilities.PropertyHelper;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Service;
import org.apache.ambari.server.state.ServiceComponent;
import org.apache.ambari.server.state.ServiceComponentHost;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * Tests the nagios property provider
 */
public class NagiosPropertyProviderTest {

  private static final String HOST = "c6401.ambari.apache.org";

  private GuiceModule module = null;
  private Clusters clusters = null;
  private Injector injector = null;

  @Before
  public void setup() throws Exception {

    module = new GuiceModule();
    injector = Guice.createInjector(module);
    NagiosPropertyProvider.init(injector);
    
    
    clusters = injector.getInstance(Clusters.class);
    Cluster cluster = createMock(Cluster.class);
    expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes();

    Service nagiosService = createMock(Service.class);
    expect(cluster.getService("NAGIOS")).andReturn(nagiosService).anyTimes();
    
    ServiceComponent nagiosServiceComponent = createMock(ServiceComponent.class);
    expect(nagiosService.getServiceComponent("NAGIOS_SERVER")).andReturn(
        nagiosServiceComponent).anyTimes();
    
    ServiceComponentHost nagiosScHost = createMock(ServiceComponentHost.class);
    Map<String, ServiceComponentHost> map1 = new HashMap<String, ServiceComponentHost>();
    map1.put(HOST, nagiosScHost);
    expect(nagiosServiceComponent.getServiceComponentHosts()).andReturn(
        map1).anyTimes();
    
    replay(clusters, cluster, nagiosService, nagiosServiceComponent);
  }
  
  @Test
  public void testNoNagiosService() throws Exception {
    Cluster cluster = clusters.getCluster("c1");
    reset(cluster); // simulate an error that NAGIOS not with the cluster
    expect(cluster.getService("NAGIOS")).andThrow(new AmbariException("No Service"));
    replay(cluster);
    
    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");
    npp.forceReset();
    
    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", "c1");
    resource.setProperty("ServiceInfo/service_name", "HBASE");
    
    // request with an empty set should get all supported properties
    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(), new HashMap<String, TemporalInfo>());

    Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
    Assert.assertEquals(1, set.size());
    
    Resource res = set.iterator().next();
    
    Map<String, Map<String, Object>> values = res.getPropertiesMap();
    
    Assert.assertFalse("Expected no alerts", values.containsKey("alerts"));
  }


  @Test
  public void testClusterDoesNotExistNPE() throws Exception {
    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");
    npp.forceReset();

    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", null);
    resource.setProperty("ServiceInfo/service_name", "HBASE");

    // request with an empty set should get all supported properties
    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(),
            new HashMap<String, TemporalInfo>());

    Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
    Assert.assertEquals(1, set.size());

  }
  
  @Test
  public void testNoNagiosServerCompoonent() throws Exception {

    Cluster cluster = clusters.getCluster("c1");
    reset(cluster);

    Service nagiosService = createMock(Service.class);
    expect(cluster.getService("NAGIOS")).andReturn(nagiosService);

    ServiceComponent nagiosServiceComponent = createMock(ServiceComponent.class);
    expect(nagiosService.getServiceComponent("NAGIOS_SERVER")).andThrow(new AmbariException("No Component"));

    replay(cluster, nagiosService);

    TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");

    NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
        streamProvider,
        "ServiceInfo/cluster_name",
        "ServiceInfo/service_name");
    npp.forceReset();
    
    Resource resource = new ResourceImpl(Resource.Type.Service);
    resource.setProperty("ServiceInfo/cluster_name", "c1");
{'fraction_non_alphanumeric': 0.10162380545412167, 'fraction_numerical': 0.0030300675938155545, 'mean_word_length': 4.6880247459125055, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1896640', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3539, 'n_words': 816}