@Test
  public void testDevPoolCreationAtBindNoFailMissingInstalledProduct() throws Exception {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<ProductData>();
    Product p = TestUtil.createProduct("test-product", "Test Product");
    Product ip1 = TestUtil.createProduct("test-product-installed-1", "Installed Test Product 1");
    Product ip2 = TestUtil.createProduct("test-product-installed-2", "Installed Test Product 2");
    devProdDTOs.add(p.toDTO());
    devProdDTOs.add(ip1.toDTO());

    Pool activePool = TestUtil.createPool(owner, p);
    List<Pool> activeList = new ArrayList<Pool>();
    activeList.add(activePool);

    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip1));
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip2));

    when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false);
    when(poolCurator.hasActiveEntitlementPools(eq(owner), any(Date.class))).thenReturn(true);
    when(productAdapter.getProductsByIds(any(Owner.class), any(List.class)))
        .thenReturn(devProdDTOs);

    this.mockProducts(owner, p, ip1, ip2);
    this.mockProductImport(owner, p, ip1, ip2);
    this.mockContentImport(owner, Collections.<String, Content>emptyMap());

    Pool expectedPool = entitler.assembleDevPool(devSystem, p.getId());
    when(pm.createPool(any(Pool.class))).thenReturn(expectedPool);
    AutobindData ad = new AutobindData(devSystem);
    entitler.bindByProducts(ad);
  }
  @Test
  public void testDevPoolCreationAtBind() throws Exception {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<ProductData>();
    Product p = TestUtil.createProduct("test-product", "Test Product");

    p.setAttribute("support_level", "Premium");
    devProdDTOs.add(p.toDTO());
    Pool activePool = TestUtil.createPool(owner, p);
    List<Pool> activeList = new ArrayList<Pool>();
    activeList.add(activePool);
    Pool devPool = mock(Pool.class);

    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p.getId());

    when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false);
    when(poolCurator.hasActiveEntitlementPools(eq(owner), any(Date.class))).thenReturn(true);
    when(productAdapter.getProductsByIds(eq(owner), any(List.class))).thenReturn(devProdDTOs);

    this.mockProducts(owner, p);
    this.mockProductImport(owner, p);
    this.mockContentImport(owner, Collections.<String, Content>emptyMap());

    when(pm.createPool(any(Pool.class))).thenReturn(devPool);
    when(devPool.getId()).thenReturn("test_pool_id");

    AutobindData ad = new AutobindData(devSystem);
    entitler.bindByProducts(ad);
    verify(pm).createPool(any(Pool.class));
  }
  @Test
  public void bindByPool() throws EntitlementRefusedException {
    String poolid = "pool10";
    Pool pool = mock(Pool.class);
    Entitlement ent = mock(Entitlement.class);
    List<Entitlement> eList = new ArrayList<Entitlement>();
    eList.add(ent);

    when(pm.find(eq(poolid))).thenReturn(pool);
    Map<String, Integer> pQs = new HashMap<String, Integer>();
    pQs.put(poolid, 1);
    when(pm.entitleByPools(eq(consumer), eq(pQs))).thenReturn(eList);

    List<Entitlement> ents = entitler.bindByPoolQuantity(consumer, poolid, 1);
    assertNotNull(ents);
    assertEquals(ent, ents.get(0));
  }
 @Test(expected = BadRequestException.class)
 public void nullPool() throws EntitlementRefusedException {
   String poolid = "foo";
   Consumer c = TestUtil.createConsumer(); // keeps me from casting null
   Map<String, Integer> pQs = new HashMap<String, Integer>();
   pQs.put(poolid, 1);
   when(cc.findByUuid(eq(c.getUuid()))).thenReturn(c);
   when(pm.entitleByPools(eq(c), eq(pQs))).thenThrow(new IllegalArgumentException());
   entitler.bindByPoolQuantities(c.getUuid(), pQs);
 }
 private void bindByProductErrorTest(String msg) throws Exception {
   try {
     String[] pids = {"prod1", "prod2", "prod3"};
     Map<String, ValidationResult> fakeResult = new HashMap<String, ValidationResult>();
     fakeResult.put("blah", fakeOutResult(msg));
     EntitlementRefusedException ere = new EntitlementRefusedException(fakeResult);
     AutobindData data = AutobindData.create(consumer).forProducts(pids);
     when(pm.entitleByProducts(data)).thenThrow(ere);
     entitler.bindByProducts(data);
   } catch (EntitlementRefusedException e) {
     fail(msg + ": threw unexpected error");
   }
 }
  private void bindByPoolErrorTest(String msg) {
    try {
      String poolid = "pool10";
      Pool pool = mock(Pool.class);
      Map<String, ValidationResult> fakeResult = new HashMap<String, ValidationResult>();
      fakeResult.put(poolid, fakeOutResult(msg));
      EntitlementRefusedException ere = new EntitlementRefusedException(fakeResult);

      when(pool.getId()).thenReturn(poolid);
      when(poolCurator.find(eq(poolid))).thenReturn(pool);
      Map<String, Integer> pQs = new HashMap<String, Integer>();
      pQs.put(poolid, 1);
      when(pm.entitleByPools(eq(consumer), eq(pQs))).thenThrow(ere);
      entitler.bindByPoolQuantity(consumer, poolid, 1);
    } catch (EntitlementRefusedException e) {
      fail(msg + ": threw unexpected error");
    }
  }