/*
 * Copyright (c) 2014 Spotify AB.
 *
 * 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.spotify.helios.agent;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import com.spotify.helios.common.descriptors.PortMapping;

import org.junit.Test;

import java.util.Map;
import java.util.Set;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public class PortAllocatorTest {

  @Test
  public void testAllocate() throws Exception {
    final PortAllocator sut = new PortAllocator(20000, 20010);
    final Map<String, PortMapping> mapping = ImmutableMap.of("p1", PortMapping.of(17),
                                                             "p2", PortMapping.of(18, 18));
    final Set<Integer> used = ImmutableSet.of(10, 11);
    final Map<String, Integer> allocation = sut.allocate(mapping, used);
    assertThat(allocation, hasEntry(is("p1"),
                                    allOf(greaterThanOrEqualTo(20000), lessThanOrEqualTo(20010))));
    assertThat(allocation, hasEntry("p2", 18));
  }

  @Test
  public void testInsufficientPortsFail1() throws Exception {
    final PortAllocator sut = new PortAllocator(10, 11);
    final Map<String, PortMapping> mapping = ImmutableMap.of("p1", PortMapping.of(17),
                                                             "p2", PortMapping.of(18, 18));
    final Set<Integer> used = ImmutableSet.of(10, 11);
    final Map<String, Integer> allocation = sut.allocate(mapping, used);
    assertNull(allocation);
  }

  @Test
  public void testInsufficientPortsFail2() throws Exception {
    final PortAllocator sut = new PortAllocator(10, 11);
    final Map<String, PortMapping> mapping = ImmutableMap.of("p1", PortMapping.of(1),
                                                             "p2", PortMapping.of(2),
                                                             "p3", PortMapping.of(4),
                                                             "p4", PortMapping.of(18, 18));
    final Set<Integer> used = ImmutableSet.of();
    final Map<String, Integer> allocation = sut.allocate(mapping, used);
    assertNull(allocation);
  }

  @Test
  public void verifyStaticPortsNotCheckedAgainstRange() throws Exception {
    final PortAllocator sut = new PortAllocator(20000, 20001);
    final Map<String, PortMapping> mapping = ImmutableMap.of("p1", PortMapping.of(1),
                                                             "p2", PortMapping.of(18, 18));
    final Set<Integer> used = ImmutableSet.of();
    final Map<String, Integer> allocation = sut.allocate(mapping, used);
    assertThat(allocation, hasEntry(is("p1"),
                                    allOf(greaterThanOrEqualTo(20000), lessThanOrEqualTo(20001))));
    assertThat(allocation, hasEntry("p2", 18));
  }
}

@@ -2942,652 +2942,7 @@
 %0A  %7D
-%0A%0A  @Test%0A  public void verifyStaticPortsNotCheckedAgainstRange() throws Exception %7B%0A    final PortAllocator sut = new PortAllocator(20000, 20001);%0A    final Map%3CString, PortMapping%3E mapping = ImmutableMap.of(%22p1%22, PortMapping.of(1),%0A                                                             %22p2%22, PortMapping.of(18, 18));%0A    final Set%3CInteger%3E used = ImmutableSet.of();%0A    final Map%3CString, Integer%3E allocation = sut.allocate(mapping, used);%0A    assertThat(allocation, hasEntry(is(%22p1%22),%0A                                    allOf(greaterThanOrEqualTo(20000), lessThanOrEqualTo(20001))));%0A    assertThat(allocation, hasEntry(%22p2%22, 18));%0A  %7D
 %0A%7D%0A

Remove redundant test