@Test(expected = BadRequestException.class)
    public void shouldVerifySharedVipLbs() throws Exception {

      List<LoadBalancer> lbs = new ArrayList<LoadBalancer>();
      LoadBalancer loadBalancer = new LoadBalancer();
      loadBalancer.setId(3333);
      loadBalancer.setAccountId(55555);

      Set<LoadBalancerJoinVip> jvips = new HashSet<LoadBalancerJoinVip>();
      LoadBalancerJoinVip jvip = new LoadBalancerJoinVip();

      jvip.setVirtualIp(new VirtualIp());
      jvip.setId(new LoadBalancerJoinVip.Id(loadBalancer.getId(), 676));
      jvip.setLoadBalancer(lb);
      jvips.add(jvip);
      loadBalancer.setLoadBalancerJoinVipSet(jvips);

      List<LoadBalancer> sharedlbs = new ArrayList<LoadBalancer>();
      LoadBalancer sharedlb = new LoadBalancer();
      sharedlb.setId(9844);
      sharedlbs.add(sharedlb);
      when(virtualIpRepository.getLoadBalancersByVipId(Matchers.anyInt())).thenReturn(sharedlbs);

      lbs.add(loadBalancer);

      when(lbRepository.getById(Matchers.anyInt())).thenReturn(loadBalancer);
      List<LoadBalancer> newLbs;
      newLbs = lbService.reassignLoadBalancerHost(lbs);

      LoadBalancer newLb;
      newLb = newLbs.get(0);

      Assert.assertEquals((Object) 55555, newLb.getAccountId());
    }
    @Test(expected = ImmutableEntityException.class)
    public void shouldThrowExceptionWhenLoaBalancerNotActive() throws Exception {
      loadBalancerService.setStatus(loadBalancer, LoadBalancerStatus.ERROR);

      LoadBalancer newLoadBalancer = new LoadBalancer();
      newLoadBalancer.setId(loadBalancer.getId());
      newLoadBalancer.setAccountId(loadBalancer.getAccountId());

      newLoadBalancer.addAccessList(accessList);

      accessListService.updateAccessList(newLoadBalancer);
    }
    @Test(expected = BadRequestException.class)
    public void shouldThrowExceptionWhenAccessListLimitExceeded() throws Exception {
      LoadBalancer newLoadBalancer = new LoadBalancer();
      newLoadBalancer.setId(loadBalancer.getId());
      newLoadBalancer.setAccountId(loadBalancer.getAccountId());

      accessList.setLoadbalancer(loadBalancer);
      for (int i = 0; i < 101; i++) {
        accessList = new AccessList();
        accessList.setIpAddress("new ip " + i);
        accessList.setType(AccessListType.ALLOW);
        newLoadBalancer.addAccessList(accessList);
      }

      accessListService.updateAccessList(newLoadBalancer);
    }
    @Test(expected = BadRequestException.class)
    public void shouldThrowExceptionWhenDuplicateAccessLists() throws Exception {
      LoadBalancer newLoadBalancer = new LoadBalancer();
      newLoadBalancer.setId(loadBalancer.getId());
      newLoadBalancer.setAccountId(loadBalancer.getAccountId());

      accessList.setLoadbalancer(loadBalancer);
      newLoadBalancer.addAccessList(accessList);

      accessListService.updateAccessList(newLoadBalancer);

      loadBalancerService.setStatus(loadBalancer, LoadBalancerStatus.ACTIVE);

      newLoadBalancer.addAccessList(accessList);
      accessListService.updateAccessList(newLoadBalancer);
    }
    @Test(expected = BadRequestException.class)
    public void shouldFailIfLbisSticky() throws Exception {
      when(lbRepository.getById(Matchers.<Integer>any())).thenReturn(lb);

      List<LoadBalancer> lbs = new ArrayList<LoadBalancer>();
      LoadBalancer loadBalancer = new LoadBalancer();
      loadBalancer.setId(3333);
      lb.setSticky(true);
      lbs.add(loadBalancer);

      List<LoadBalancer> newLbs;
      newLbs = lbService.reassignLoadBalancerHost(lbs);

      LoadBalancer newLb;
      newLb = newLbs.get(0);

      Assert.assertEquals((Object) 555555, newLb.getAccountId());
    }
  protected static void setupIvars() {
    Set<LoadBalancerJoinVip> vipList = new HashSet<LoadBalancerJoinVip>();
    vip1 = new VirtualIp();
    vip1.setId(TEST_VIP_ID);
    vip1.setIpAddress("10.69.0.59");
    vip2 = new VirtualIp();
    vip2.setId(TEST_VIP_ID + 1);
    vip2.setIpAddress("10.69.0.60");
    LoadBalancerJoinVip loadBalancerJoinVip = new LoadBalancerJoinVip();
    loadBalancerJoinVip.setVirtualIp(vip1);
    vipList.add(loadBalancerJoinVip);
    loadBalancerJoinVip = new LoadBalancerJoinVip();
    loadBalancerJoinVip.setVirtualIp(vip2);
    vipList.add(loadBalancerJoinVip);

    Set<Node> nodeList = new HashSet<Node>();
    node1 = new Node();
    node2 = new Node();
    node1.setIpAddress("127.0.0.1");
    node2.setIpAddress("127.0.0.2");
    node1.setPort(80);
    node2.setPort(80);
    node1.setCondition(ENABLED);
    node2.setCondition(DISABLED);
    node1.setWeight(1);
    node2.setWeight(1);
    nodeList.add(node1);
    nodeList.add(node2);

    LoadBalancer lb = new LoadBalancer();
    lb.setId(TEST_LOADBALANCER_ID);
    lb.setAccountId(TEST_ACCOUNT_ID);
    lb.setPort(80);
    lb.setAlgorithm(ROUND_ROBIN);
    lb.setName("STM-TESTER");
    lb.setProtocol(HTTP);
    lb.setNodes(nodeList);
    lb.setLoadBalancerJoinVipSet(vipList);

    lb.setUserPages(new UserPages());

    STMTestBase.lb = lb;
  }
    @Test
    public void shouldAddNewAccessListLoadBalancerWhenOperationSucceeds() throws Exception {
      List<AccessList> accessListsBefore =
          accessListService.getAccessListByAccountIdLoadBalancerId(
              loadBalancer.getAccountId(), loadBalancer.getId());

      LoadBalancer newLoadBalancer = new LoadBalancer();
      newLoadBalancer.setId(loadBalancer.getId());
      newLoadBalancer.setAccountId(loadBalancer.getAccountId());

      accessList.setLoadbalancer(loadBalancer);
      newLoadBalancer.addAccessList(accessList);

      accessListService.updateAccessList(newLoadBalancer);

      List<AccessList> accessListsAfter =
          accessListService.getAccessListByAccountIdLoadBalancerId(
              loadBalancer.getAccountId(), loadBalancer.getId());
      Assert.assertEquals(accessListsBefore.size() + 1, accessListsAfter.size());
    }
    @Before
    public void standUp()
        throws EntityNotFoundException, UnprocessableEntityException, ClusterStatusException,
            NoAvailableClusterException {
      lb = new LoadBalancer();
      lbRepository = mock(LoadBalancerRepository.class);
      lbService = new LoadBalancerServiceImpl();
      lbService.setLoadBalancerRepository(lbRepository);

      hostService = new HostServiceImpl();
      hostService.setLoadBalancerRepository(lbRepository);
      hostRepository = mock(HostRepository.class);
      hostService.setHostRepository(hostRepository);

      clusterService = new ClusterServiceImpl();
      clusterService.setLoadBalancerRepository(lbRepository);
      clusterRepository = mock(ClusterRepository.class);
      clusterService.setClusterRepository(clusterRepository);

      virtualIpRepository = mock(VirtualIpRepository.class);

      loadBalancerStatusHistoryRepository = mock(LoadBalancerStatusHistoryRepository.class);
      loadBalancerStatusHistoryService = new LoadBalancerStatusHistoryServiceImpl();
      loadBalancerStatusHistoryService.setLoadBalancerStatusHistoryRepository(
          loadBalancerStatusHistoryRepository);

      hostService.setClusterRepository(clusterRepository);
      //            lbService.setHostService(hostService);
      //            lbService.setLoadBalancerStatusHistoryService(loadBalancerStatusHistoryService);
      lbService.setVirtualIpRepository(virtualIpRepository);

      lb.setStatus(LoadBalancerStatus.ACTIVE);

      lb.setAccountId(555555);
      lb.setId(3333);
      lb.setPort(33);
      lb.setProtocol(LoadBalancerProtocol.HTTP);

      Host host = new Host();
      host.setId(2);
      host.setHostStatus(HostStatus.ACTIVE);

      Cluster cluster = new Cluster();
      cluster.setId(3);

      lb.setHost(host);

      when(hostRepository.getById(Matchers.<Integer>any())).thenReturn(host);
      when(hostRepository.getDefaultActiveHost(Matchers.<Integer>any())).thenReturn(host);
      when(clusterRepository.getActiveCluster(null, false)).thenReturn(cluster);
      when(hostService.getById(Matchers.<Integer>any())).thenReturn(host);
      when(loadBalancerStatusHistoryRepository.save(
              Matchers.<LoadBalancerStatusHistory>anyObject()))
          .thenReturn(new LoadBalancerStatusHistory());

      //            when(loadBalancerStatusHistoryService.save(lb.getAccountId(), lb.getId(),
      // status);)
      //            when(lbRepository.testAndSetStatus(Matchers.<Integer>any(),
      // Matchers.<Integer>any(),Matchers.<LoadBalancerStatus>any(),
      // Matchers.<Boolean>any())).thenReturn(true);

    }
 public static LoadBalancer generateLoadBalancer() {
   LoadBalancer loadBalancer = new LoadBalancer();
   loadBalancer.setPort(port);
   Set<AccessList> accessLists = new HashSet<AccessList>();
   AccessList item = new AccessList();
   item.setUserName(username);
   item.setId(id);
   item.setIpAddress(ipv42);
   item.setType(AccessListType.DENY);
   item.setLoadbalancer(loadBalancer);
   accessLists.add(item);
   loadBalancer.setAccessLists(accessLists);
   loadBalancer.setAccountId(accountId);
   loadBalancer.setAlgorithm(LoadBalancerAlgorithm.ROUND_ROBIN);
   ConnectionLimit limit = new ConnectionLimit();
   limit.setId(id);
   limit.setUserName(username);
   limit.setLoadBalancer(loadBalancer);
   limit.setMaxConnectionRate(maxConnectRate);
   limit.setMaxConnections(maxConnections);
   limit.setMinConnections(minConnections);
   limit.setRateInterval(rateInterval);
   loadBalancer.setConnectionLimit(limit);
   loadBalancer.setConnectionLogging(active);
   loadBalancer.setContentCaching(active);
   loadBalancer.setCreated(Calendar.getInstance());
   loadBalancer.setUpdated(Calendar.getInstance());
   loadBalancer.setHalfClosed(active);
   HealthMonitor monitor = new HealthMonitor();
   monitor.setUserName(username);
   monitor.setId(id);
   monitor.setAttemptsBeforeDeactivation(numAttempts);
   monitor.setBodyRegex(regex);
   monitor.setDelay(delay);
   monitor.setHostHeader(header);
   monitor.setLoadbalancer(loadBalancer);
   monitor.setStatusRegex(regex);
   monitor.setPath(path);
   monitor.setTimeout(timeout);
   monitor.setType(HealthMonitorType.CONNECT);
   loadBalancer.setHealthMonitor(monitor);
   loadBalancer.setHost(new Host());
   loadBalancer.setName(name);
   Set<Node> nodes = new HashSet<Node>();
   Node node = new Node();
   node.setId(id);
   node.setPort(port);
   node.setLoadbalancer(loadBalancer);
   node.setCondition(NodeCondition.ENABLED);
   node.setIpAddress(ipv43);
   List<NodeMeta> nodeMetadata = new ArrayList<NodeMeta>();
   NodeMeta nodeMeta = new NodeMeta();
   nodeMeta.setKey(metaKey);
   nodeMeta.setNode(node);
   nodeMeta.setValue(metaValue);
   nodeMeta.setId(id);
   nodeMeta.setUserName(username);
   nodeMetadata.add(nodeMeta);
   node.setNodeMetadata(nodeMetadata);
   node.setStatus(NodeStatus.ONLINE);
   node.setType(NodeType.PRIMARY);
   node.setWeight(weight);
   nodes.add(node);
   node = new Node();
   node.setId(id + 1);
   node.setPort(port);
   node.setLoadbalancer(loadBalancer);
   node.setCondition(NodeCondition.ENABLED);
   node.setIpAddress(ipv44);
   nodeMetadata = new ArrayList<NodeMeta>();
   nodeMeta = new NodeMeta();
   nodeMeta.setKey(metaKey);
   nodeMeta.setNode(node);
   nodeMeta.setValue(metaValue);
   nodeMeta.setId(id + 1);
   nodeMeta.setUserName(username);
   nodeMetadata.add(nodeMeta);
   node.setNodeMetadata(nodeMetadata);
   node.setStatus(NodeStatus.ONLINE);
   node.setType(NodeType.PRIMARY);
   node.setWeight(weight);
   nodes.add(node);
   loadBalancer.setNodes(nodes);
   Set<LoadbalancerMeta> lbMetadata = new HashSet<LoadbalancerMeta>();
   LoadbalancerMeta lbMeta = new LoadbalancerMeta();
   lbMeta.setUserName(username);
   lbMeta.setId(id);
   lbMeta.setKey(metaKey);
   lbMeta.setValue(metaValue);
   lbMeta.setLoadbalancer(loadBalancer);
   lbMetadata.add(lbMeta);
   loadBalancer.setLoadbalancerMetadata(lbMetadata);
   loadBalancer.setProtocol(LoadBalancerProtocol.HTTP);
   RateLimit limits = new RateLimit();
   limits.setLoadbalancer(loadBalancer);
   limits.setId(id);
   limits.setUserName(username);
   limits.setExpirationTime(Calendar.getInstance());
   limits.setMaxRequestsPerSecond(maxRequests);
   Ticket ticket = new Ticket();
   ticket.setUserName(username);
   ticket.setId(id);
   ticket.setLoadbalancer(loadBalancer);
   ticket.setComment(comment);
   ticket.setTicketId(ticketId);
   limits.setTicket(ticket);
   loadBalancer.setRateLimit(limits);
   loadBalancer.setSessionPersistence(SessionPersistence.HTTP_COOKIE);
   SslTermination termination = new SslTermination();
   termination.setId(id);
   termination.setEnabled(active);
   termination.setUserName(username);
   termination.setSecurePort(securePort);
   termination.setCertificate(cert);
   termination.setPrivatekey(key);
   termination.setSecureTrafficOnly(inactive);
   termination.setLoadbalancer(loadBalancer);
   loadBalancer.setSslTermination(termination);
   loadBalancer.setStatus(LoadBalancerStatus.ACTIVE);
   loadBalancer.setSticky(inactive);
   Suspension suspension = new Suspension();
   suspension.setUserName(username);
   suspension.setId(id);
   suspension.setLoadbalancer(loadBalancer);
   suspension.setUser(user);
   suspension.setReason(reason);
   suspension.setTicket(ticket);
   loadBalancer.setSuspension(suspension);
   Set<Ticket> tickets = new HashSet<Ticket>();
   tickets.add(ticket);
   loadBalancer.setTickets(tickets);
   loadBalancer.setTimeout(timeout);
   UserPages pages = new UserPages();
   pages.setLoadbalancer(loadBalancer);
   pages.setId(id);
   pages.setUserName(username);
   pages.setErrorpage(errorPage);
   loadBalancer.setUserPages(pages);
   loadBalancer.setId(id);
   loadBalancer.setUserName(username);
   Set<LoadBalancerJoinVip> vipList = spy(new HashSet<LoadBalancerJoinVip>());
   VirtualIp vip = new VirtualIp();
   vip.setId(1234);
   vip.setIpAddress("10.69.0.60");
   LoadBalancerJoinVip loadBalancerJoinVip = new LoadBalancerJoinVip();
   loadBalancerJoinVip.setVirtualIp(vip);
   vipList.add(loadBalancerJoinVip);
   loadBalancer.setLoadBalancerJoinVipSet(vipList);
   return loadBalancer;
 }