<commit_before>package com.hubspot.baragon.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.model.MultipleFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.hubspot.baragon.BaragonServiceTestModule;
import com.hubspot.baragon.data.BaragonLoadBalancerDatastore;
import com.hubspot.baragon.exceptions.InvalidRequestActionException;
import com.hubspot.baragon.exceptions.InvalidUpstreamsException;
import com.hubspot.baragon.exceptions.RequestAlreadyEnqueuedException;
import com.hubspot.baragon.models.BaragonAgentMetadata;
import com.hubspot.baragon.models.BaragonRequest;
import com.hubspot.baragon.models.BaragonRequestState;
import com.hubspot.baragon.models.BaragonResponse;
import com.hubspot.baragon.models.BaragonService;
import com.hubspot.baragon.models.RequestAction;
import com.hubspot.baragon.models.UpstreamInfo;
import com.hubspot.baragon.service.managers.RequestManager;
import com.hubspot.baragon.service.worker.BaragonRequestWorker;

@RunWith(JukitoRunner.class)
public class RequestTest {
  private static final Logger LOG = LoggerFactory.getLogger(RequestTest.class);

  public static final String REAL_LB_GROUP = "real";
  public static final String FAKE_LB_GROUP = "fake";

  public static class Module extends JukitoModule {
    @Override
    protected void configureTest() {
      install(new BaragonServiceTestModule());
    }
  }

  @Before
  public void setupLbGroups(BaragonLoadBalancerTestDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setLoadBalancerGroupsOverride(Optional.of(Collections.singleton(REAL_LB_GROUP)));
    BaragonAgentMetadata agent1 = BaragonAgentMetadata.fromString("http://1192.168.3.11:8080/baragon-agent/v2");
    Collection<BaragonAgentMetadata> agents = new ArrayList<>();
    agents.add(agent1);
    loadBalancerDatastore.setLoadBalancerAgentsOverride(Optional.of(agents));
  }

  @After
  public void clearBasePaths(BaragonLoadBalancerDatastore loadBalancerDatastore) {
    LOG.debug("Clearing base paths...");
    for (String loadBalancerGroup : loadBalancerDatastore.getLoadBalancerGroupNames()) {
      for (String basePath : loadBalancerDatastore.getBasePaths(loadBalancerGroup)) {
        LOG.debug(String.format("  Clearing %s on %s", basePath, loadBalancerGroup));
        loadBalancerDatastore.clearBasePath(loadBalancerGroup, basePath);
      }
    }
  }

  private Optional<BaragonResponse> assertResponseStateExists(RequestManager requestManager, String requestId, BaragonRequestState expected) {
    final Optional<BaragonResponse> maybeResponse = requestManager.getResponse(requestId);

    assertTrue(String.format("Response for request %s exists", requestId), maybeResponse.isPresent());
    assertEquals(expected, maybeResponse.get().getLoadBalancerState());

    return maybeResponse;
  }

  private Optional<BaragonResponse> assertResponseStateAbsent(RequestManager requestManager, String requestId) {
    final Optional<BaragonResponse> maybeResponse = requestManager.getResponse(requestId);

    assertTrue(String.format("Response for request %s does not exist", requestId), !maybeResponse.isPresent());

    return maybeResponse;
  }

  @Test
  public void testNonExistentLoadBalancerGroup(RequestManager requestManager, BaragonRequestWorker requestWorker) {
    final String requestId = "test-126";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice1", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Test
  public void testPreexistingResponse(RequestManager requestManager) throws RequestAlreadyEnqueuedException, InvalidRequestActionException, InvalidUpstreamsException {
    final String requestId = "test-127";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice2", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    BaragonResponse response = requestManager.enqueueRequest(request);
    BaragonResponse repeatResponse = requestManager.enqueueRequest(request);
    assertEquals(repeatResponse, response);
  }

  @Test
  public void testBasePathConflicts(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setBasePathServiceId(REAL_LB_GROUP, "/foo", "foo-service");
    final String requestId = "test-128";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(REAL_LB_GROUP);

    final BaragonService service = new BaragonService("testservice3", Collections.<String>emptyList(), "/foo", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Test
  public void testAdditionalPathConflicts(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setBasePathServiceId(REAL_LB_GROUP, "/some-other-path", "foo-service");
    final String requestId = "test-129";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(REAL_LB_GROUP);

    final BaragonService service = new BaragonService("testservice4", Collections.<String>emptyList(), "/foo", Collections.singletonList("/some-other-path"), lbGroup, Collections.<String, Object>emptyMap(), Optional.<String>absent(), Collections.<String>emptySet(), Optional.absent());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Test
  public void testDuplicateUpstreams(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) throws RequestAlreadyEnqueuedException, InvalidRequestActionException, InvalidUpstreamsException {
    final String requestId1 = "test-130";
    final String requestId2 = "test-131";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice3", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream1 = new UpstreamInfo("testhost:8080", Optional.of(requestId1), Optional.<String>absent());

    final BaragonRequest request1 = new BaragonRequest(requestId1, service, ImmutableList.of(upstream1), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    requestManager.enqueueRequest(request1);
    assertResponseStateExists(requestManager, requestId1, BaragonRequestState.WAITING);

    requestWorker.run();
    assertResponseStateExists(requestManager, requestId1, BaragonRequestState.INVALID_REQUEST_NOOP);

    final UpstreamInfo upstream2 = new UpstreamInfo("testhost:8080", Optional.of(requestId2), Optional.<String>absent());

    final BaragonRequest request2 = new BaragonRequest(requestId2, service, ImmutableList.of(upstream2), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    requestManager.enqueueRequest(request2);
    assertResponseStateExists(requestManager, requestId2, BaragonRequestState.WAITING);

    requestWorker.run();
    assertResponseStateExists(requestManager, requestId2, BaragonRequestState.INVALID_REQUEST_NOOP);


  }

  @Rule
  public ExpectedException expectedEx = ExpectedException.none();

  @Test
  public void testNoDuplicateUpstreamsInUpstreamsToBeAdded(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) throws RequestAlreadyEnqueuedException, InvalidRequestActionException, InvalidUpstreamsException {
    expectedEx.expect(MultipleFailureException.class);
    expectedEx.expectMessage("If noDuplicateUpstreams is specified, you cannot have duplicate upstreams. Found these duplicate upstreams: [testhost:8080])");
    final String requestId = "test-132";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice3", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream, upstream), ImmutableList.<UpstreamInfo>of(), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent(),
                                                      Optional.of(RequestAction.UPDATE), false, false, false, true);

    requestManager.enqueueRequest(request);
    assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

    requestWorker.run();
    assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
  }
<commit_msg>testing<commit_after>package com.hubspot.baragon.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.model.MultipleFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.hubspot.baragon.BaragonServiceTestModule;
import com.hubspot.baragon.data.BaragonLoadBalancerDatastore;
import com.hubspot.baragon.exceptions.InvalidRequestActionException;
import com.hubspot.baragon.exceptions.InvalidUpstreamsException;
import com.hubspot.baragon.exceptions.RequestAlreadyEnqueuedException;
import com.hubspot.baragon.models.BaragonAgentMetadata;
import com.hubspot.baragon.models.BaragonRequest;
import com.hubspot.baragon.models.BaragonRequestState;
import com.hubspot.baragon.models.BaragonResponse;
import com.hubspot.baragon.models.BaragonService;
import com.hubspot.baragon.models.RequestAction;
import com.hubspot.baragon.models.UpstreamInfo;
import com.hubspot.baragon.service.managers.RequestManager;
import com.hubspot.baragon.service.worker.BaragonRequestWorker;

@RunWith(JukitoRunner.class)
public class RequestTest {
  private static final Logger LOG = LoggerFactory.getLogger(RequestTest.class);

  public static final String REAL_LB_GROUP = "real";
  public static final String FAKE_LB_GROUP = "fake";

  public static class Module extends JukitoModule {
    @Override
    protected void configureTest() {
      install(new BaragonServiceTestModule());
    }
  }

  @Before
  public void setupLbGroups(BaragonLoadBalancerTestDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setLoadBalancerGroupsOverride(Optional.of(Collections.singleton(REAL_LB_GROUP)));
    BaragonAgentMetadata agent1 = BaragonAgentMetadata.fromString("http://127.0.0.1:8080/baragon-agent/v2");
    Collection<BaragonAgentMetadata> agents = new ArrayList<>();
    agents.add(agent1);
    loadBalancerDatastore.setLoadBalancerAgentsOverride(Optional.of(agents));
  }

  @After
  public void clearBasePaths(BaragonLoadBalancerDatastore loadBalancerDatastore) {
    LOG.debug("Clearing base paths...");
    for (String loadBalancerGroup : loadBalancerDatastore.getLoadBalancerGroupNames()) {
      for (String basePath : loadBalancerDatastore.getBasePaths(loadBalancerGroup)) {
        LOG.debug(String.format("  Clearing %s on %s", basePath, loadBalancerGroup));
        loadBalancerDatastore.clearBasePath(loadBalancerGroup, basePath);
      }
    }
  }

  private Optional<BaragonResponse> assertResponseStateExists(RequestManager requestManager, String requestId, BaragonRequestState expected) {
    final Optional<BaragonResponse> maybeResponse = requestManager.getResponse(requestId);

    assertTrue(String.format("Response for request %s exists", requestId), maybeResponse.isPresent());
    assertEquals(expected, maybeResponse.get().getLoadBalancerState());

    return maybeResponse;
  }

  private Optional<BaragonResponse> assertResponseStateAbsent(RequestManager requestManager, String requestId) {
    final Optional<BaragonResponse> maybeResponse = requestManager.getResponse(requestId);

    assertTrue(String.format("Response for request %s does not exist", requestId), !maybeResponse.isPresent());

    return maybeResponse;
  }

  @Test
  public void testNonExistentLoadBalancerGroup(RequestManager requestManager, BaragonRequestWorker requestWorker) {
    final String requestId = "test-126";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice1", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Test
  public void testPreexistingResponse(RequestManager requestManager) throws RequestAlreadyEnqueuedException, InvalidRequestActionException, InvalidUpstreamsException {
    final String requestId = "test-127";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice2", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    BaragonResponse response = requestManager.enqueueRequest(request);
    BaragonResponse repeatResponse = requestManager.enqueueRequest(request);
    assertEquals(repeatResponse, response);
  }

  @Test
  public void testBasePathConflicts(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setBasePathServiceId(REAL_LB_GROUP, "/foo", "foo-service");
    final String requestId = "test-128";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(REAL_LB_GROUP);

    final BaragonService service = new BaragonService("testservice3", Collections.<String>emptyList(), "/foo", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Test
  public void testAdditionalPathConflicts(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) {
    loadBalancerDatastore.setBasePathServiceId(REAL_LB_GROUP, "/some-other-path", "foo-service");
    final String requestId = "test-129";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(REAL_LB_GROUP);

    final BaragonService service = new BaragonService("testservice4", Collections.<String>emptyList(), "/foo", Collections.singletonList("/some-other-path"), lbGroup, Collections.<String, Object>emptyMap(), Optional.<String>absent(), Collections.<String>emptySet(), Optional.absent());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent());

    try {
      assertResponseStateAbsent(requestManager, requestId);

      LOG.info("Going to enqueue request: {}", request);
      requestManager.enqueueRequest(request);

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

      requestWorker.run();

      assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  @Rule
  public ExpectedException expectedEx = ExpectedException.none();

  @Test
  public void testNoDuplicateUpstreamsInUpstreamsToBeAdded(RequestManager requestManager, BaragonRequestWorker requestWorker, BaragonLoadBalancerDatastore loadBalancerDatastore) throws RequestAlreadyEnqueuedException, InvalidRequestActionException, InvalidUpstreamsException {
    expectedEx.expect(MultipleFailureException.class);
    expectedEx.expectMessage("If noDuplicateUpstreams is specified, you cannot have duplicate upstreams. Found these duplicate upstreams: [testhost:8080])");
    final String requestId = "test-132";
    Set<String> lbGroup = new HashSet<>();
    lbGroup.add(FAKE_LB_GROUP);
    final BaragonService service = new BaragonService("testservice3", Collections.<String>emptyList(), "/test", lbGroup, Collections.<String, Object>emptyMap());

    final UpstreamInfo upstream = new UpstreamInfo("testhost:8080", Optional.of(requestId), Optional.<String>absent());

    final BaragonRequest request = new BaragonRequest(requestId, service, ImmutableList.of(upstream, upstream), ImmutableList.<UpstreamInfo>of(), ImmutableList.<UpstreamInfo>of(), Optional.<String>absent(),
                                                      Optional.of(RequestAction.UPDATE), false, false, false, true);

    requestManager.enqueueRequest(request);
    assertResponseStateExists(requestManager, requestId, BaragonRequestState.WAITING);

    requestWorker.run();
    assertResponseStateExists(requestManager, requestId, BaragonRequestState.INVALID_REQUEST_NOOP);
  }

{'fraction_non_alphanumeric': 0.10278674364686094, 'fraction_numerical': 0.007046415420284584, 'mean_word_length': 6.028115015974441, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6441846', 'n_tokens_mistral': 6230, 'n_tokens_neox': 5979, 'n_words': 1152}