@Test
  public void testCreatedDevPoolAttributes() {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<ProductData>();
    Product p1 = TestUtil.createProduct("dev-product", "Dev Product");
    p1.setAttribute("support_level", "Premium");
    p1.setAttribute("expires_after", "47");
    Product p2 = TestUtil.createProduct("provided-product1", "Provided Product 1");
    Product p3 = TestUtil.createProduct("provided-product2", "Provided Product 2");
    devProdDTOs.add(p1.toDTO());
    devProdDTOs.add(p2.toDTO());
    devProdDTOs.add(p3.toDTO());
    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p1.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(p2));
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(p3));
    when(productAdapter.getProductsByIds(eq(owner), any(List.class))).thenReturn(devProdDTOs);

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

    Pool created = entitler.assembleDevPool(devSystem, devSystem.getFact("dev_sku"));
    Calendar cal = Calendar.getInstance();
    cal.setTime(created.getStartDate());
    cal.add(Calendar.DAY_OF_YEAR, 47);
    assertEquals(created.getEndDate(), cal.getTime());
    assertEquals("true", created.getAttributeValue(Pool.Attributes.DEVELOPMENT_POOL));
    assertEquals(devSystem.getUuid(), created.getAttributeValue(Pool.Attributes.REQUIRES_CONSUMER));
    assertEquals(p1.getId(), created.getProductId());
    assertEquals(2, created.getProvidedProducts().size());
    assertEquals("Premium", created.getProduct().getAttributeValue("support_level"));
    assertEquals(1L, created.getQuantity().longValue());
  }
  @Test
  public void hypervisorCheckInCreatesNewConsumer() throws Exception {
    Owner owner = new Owner("admin");

    Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>();
    hostGuestMap.put("test-host", Arrays.asList(new GuestId("GUEST_A"), new GuestId("GUEST_B")));

    when(consumerCurator.findByUuid(eq("test-host"))).thenReturn(null);
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(principal.canAccess(eq(owner), eq(Access.ALL))).thenReturn(true);
    when(consumerTypeCurator.lookupByLabel(eq(ConsumerTypeEnum.HYPERVISOR.getLabel())))
        .thenReturn(hypervisorType);
    when(idCertService.generateIdentityCert(any(Consumer.class)))
        .thenReturn(new IdentityCertificate());

    HypervisorCheckInResult result =
        hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey());

    Set<Consumer> created = result.getCreated();
    assertEquals(1, created.size());

    Consumer c1 = created.iterator().next();
    assertEquals("test-host", c1.getUuid());
    assertEquals(2, c1.getGuestIds().size());
    assertEquals("GUEST_A", c1.getGuestIds().get(0).getGuestId());
    assertEquals("GUEST_B", c1.getGuestIds().get(1).getGuestId());
    assertEquals("x86_64", c1.getFact("uname.machine"));
    assertEquals("hypervisor", c1.getType().getLabel());
  }
  @Test
  public void entIsCompliantIfSocketsNotSetOnEntPool() {
    Consumer c = mock(Consumer.class);
    when(c.hasFact("cpu.cpu_socket(s)")).thenReturn(true);
    when(c.getFact("cpu.cpu_socket(s)")).thenReturn("2");

    Entitlement ent = mockEntitlement(c, PRODUCT_1);
    assertTrue(compliance.isEntitlementCompliant(c, ent));
  }
  @Test
  public void entIsNotCompliantWhenSocketsAreNotCovered() {
    Consumer c = mock(Consumer.class);
    when(c.hasFact("cpu.cpu_socket(s)")).thenReturn(true);
    when(c.getFact("cpu.cpu_socket(s)")).thenReturn("8");

    Entitlement ent = mockEntitlement(c, PRODUCT_1);
    ent.getPool().setProductAttribute("sockets", "4", PRODUCT_1);
    assertFalse(compliance.isEntitlementCompliant(c, ent));
  }
  @Test
  public void stackIsCompliant() {
    Consumer c = mock(Consumer.class);
    when(c.hasFact("cpu.cpu_socket(s)")).thenReturn(true);
    when(c.getFact("cpu.cpu_socket(s)")).thenReturn("2");

    List<Entitlement> ents = new LinkedList<Entitlement>();
    ents.add(mockStackedEntitlement(c, STACK_ID_1, "Awesome Product", PRODUCT_1));
    assertTrue(compliance.isStackCompliant(c, STACK_ID_1, ents));
  }
  @Test
  public void testCreatedDevSkuWithNoSla() {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<ProductData>();
    final Product p1 = TestUtil.createProduct("dev-product", "Dev Product");
    devProdDTOs.add(p1.toDTO());
    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p1.getId());

    when(productAdapter.getProductsByIds(eq(owner), any(List.class))).thenReturn(devProdDTOs);
    mockUpdateProduct(p1, owner);

    this.mockContentImport(owner, Collections.<String, Content>emptyMap());
    when(productManager.importProducts(eq(owner), any(Map.class), any(Map.class)))
        .thenAnswer(
            new Answer<ImportResult<Product>>() {
              @Override
              public ImportResult<Product> answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Map<String, ProductData> productData = (Map<String, ProductData>) args[1];
                ImportResult<Product> importResult = new ImportResult<Product>();
                Map<String, Product> output = importResult.getCreatedEntities();

                // We need to copy the attributes from the product data to the product to
                // simulate a proper update.
                for (ProductData pdata : productData.values()) {
                  if (pdata != null) {
                    if (p1.getId().equals(pdata.getId())) {
                      p1.clearAttributes();
                      if (pdata.getAttributes() != null) {
                        for (ProductAttributeData attrib : pdata.getAttributes()) {
                          p1.setAttribute(attrib.getName(), attrib.getValue());
                        }
                      }

                      output.put(p1.getId(), p1);
                    } else {
                      Product product = new Product(pdata.getId(), pdata.getName());
                      // Do we care about this product? Probably not.
                      output.put(product.getId(), product);
                    }
                  }
                }

                return importResult;
              }
            });

    Pool created = entitler.assembleDevPool(devSystem, devSystem.getFact("dev_sku"));
    assertEquals(entitler.DEFAULT_DEV_SLA, created.getProduct().getAttributeValue("support_level"));
  }
  private boolean shouldGenerateV3(Entitlement entitlement) {
    Consumer consumer = entitlement.getConsumer();

    if (consumer.getType().isManifest()) {
      for (ConsumerCapability capability : consumer.getCapabilities()) {
        if ("cert_v3".equals(capability.getName())) {
          return true;
        }
      }
      return false;
    } else if (consumer.getType().getLabel().equals(ConsumerTypeEnum.HYPERVISOR.getLabel())) {
      // Hypervisors in this context don't use content, so allow v3
      return true;
    } else {
      String entitlementVersion = consumer.getFact("system.certificate_version");
      return entitlementVersion != null && entitlementVersion.startsWith("3.");
    }
  }
  @Test
  public void testcheckForGuestsMigration() {
    ConsumerResource cr =
        Mockito.spy(
            new ConsumerResource(
                mockedConsumerCurator,
                null,
                null,
                null,
                null,
                null,
                null,
                i18n,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                new CandlepinCommonTestConfig(),
                null,
                null,
                null,
                null,
                productCurator,
                null));
    List<GuestId> startGuests = new ArrayList<GuestId>();
    List<GuestId> updatedGuests = new ArrayList<GuestId>();
    VirtConsumerMap guestConsumerMap = new VirtConsumerMap();
    Owner o = mock(Owner.class);
    Consumer host = new Consumer();

    Consumer cOne = new Consumer();
    cOne.setFact("virt.is_guest", "true");
    cOne.setFact("virt.uuid", cOne.getUuid() + "-vuuid");
    cOne.setOwner(o);
    GuestId one = new GuestId(cOne.getFact("virt.uuid"));
    startGuests.add(one);
    guestConsumerMap.add(cOne.getFact("virt.uuid"), cOne);

    Consumer cTwo = new Consumer();
    cTwo.setFact("virt.is_guest", "true");
    cTwo.setFact("virt.uuid", cTwo.getUuid() + "-vuuid");
    cTwo.setOwner(o);
    GuestId two = new GuestId(cTwo.getFact("virt.uuid"));
    startGuests.add(two);
    updatedGuests.add(two);
    guestConsumerMap.add(cTwo.getFact("virt.uuid"), cTwo);

    Consumer cThree = new Consumer();
    cThree.setFact("virt.is_guest", "true");
    cThree.setFact("virt.uuid", cThree.getUuid() + "-vuuid");
    cThree.setOwner(o);
    GuestId three = new GuestId(cThree.getFact("virt.uuid"));
    updatedGuests.add(three);
    guestConsumerMap.add(cThree.getFact("virt.uuid"), cThree);

    cr.checkForGuestsMigration(host, startGuests, updatedGuests, guestConsumerMap);
    verify(cr).checkForGuestMigration(host, cOne);
    verify(cr).checkForGuestMigration(host, cTwo);
    verify(cr).checkForGuestMigration(host, cThree);
  }