@Test
  public void testIdCertGetsRegenerated() throws Exception {
    // using lconsumer simply to avoid hiding consumer. This should
    // get renamed once we refactor this test suite.
    IdentityCertServiceAdapter mockedIdSvc = Mockito.mock(IdentityCertServiceAdapter.class);

    EventSink sink = Mockito.mock(EventSinkImpl.class);

    SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
    ComplianceRules rules = Mockito.mock(ComplianceRules.class);

    Consumer consumer = createConsumer();
    ComplianceStatus status = new ComplianceStatus();
    when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
    // cert expires today which will trigger regen
    consumer.setIdCert(createIdCert());
    BigInteger origserial = consumer.getIdCert().getSerial().getSerial();

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
    when(mockedIdSvc.regenerateIdentityCert(consumer)).thenReturn(createIdCert());

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            ssa,
            null,
            mockedIdSvc,
            null,
            null,
            sink,
            eventFactory,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            rules,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    Consumer c = cr.getConsumer(consumer.getUuid());

    assertFalse(origserial.equals(c.getIdCert().getSerial().getSerial()));
  }
  @Test
  public void testRegenerateIdCerts() throws GeneralSecurityException, IOException {

    // using lconsumer simply to avoid hiding consumer. This should
    // get renamed once we refactor this test suite.
    IdentityCertServiceAdapter mockedIdSvc = Mockito.mock(IdentityCertServiceAdapter.class);

    EventSink sink = Mockito.mock(EventSinkImpl.class);

    Consumer consumer = createConsumer();
    consumer.setIdCert(createIdCert());
    IdentityCertificate ic = consumer.getIdCert();
    assertNotNull(ic);

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
    when(mockedIdSvc.regenerateIdentityCert(consumer)).thenReturn(createIdCert());

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            null,
            mockedIdSvc,
            null,
            null,
            sink,
            eventFactory,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    Consumer fooc = cr.regenerateIdentityCertificates(consumer.getUuid());

    assertNotNull(fooc);
    IdentityCertificate ic1 = fooc.getIdCert();
    assertNotNull(ic1);
    assertFalse(ic.equals(ic1));
  }
  @Test
  public void testAsyncExport() {
    CdnCurator mockedCdnCurator = mock(CdnCurator.class);
    ManifestManager manifestManager = mock(ManifestManager.class);
    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            mockedCdnCurator,
            null,
            null,
            productCurator,
            manifestManager);

    List<KeyValueParameter> extParams = new ArrayList<KeyValueParameter>();
    Owner owner = TestUtil.createOwner();
    Consumer consumer =
        TestUtil.createConsumer(new ConsumerType(ConsumerType.ConsumerTypeEnum.CANDLEPIN), owner);
    Cdn cdn = new Cdn("cdn-label", "test", "url");

    when(mockedConsumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid())))
        .thenReturn(consumer);
    when(mockedCdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);

    cr.exportDataAsync(null, consumer.getUuid(), cdn.getLabel(), "prefix", cdn.getUrl(), extParams);
    verify(manifestManager)
        .generateManifestAsync(
            eq(consumer.getUuid()),
            eq(cdn.getLabel()),
            eq("prefix"),
            eq(cdn.getUrl()),
            any(Map.class));
  }
  @Test
  public void testIdCertDoesNotRegenerate() throws Exception {
    SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
    ComplianceRules rules = Mockito.mock(ComplianceRules.class);

    Consumer consumer = createConsumer();
    ComplianceStatus status = new ComplianceStatus();
    when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
    consumer.setIdCert(createIdCert(TestUtil.createDate(2025, 6, 9)));
    BigInteger origserial = consumer.getIdCert().getSerial().getSerial();

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            ssa,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            rules,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    Consumer c = cr.getConsumer(consumer.getUuid());

    assertEquals(origserial, c.getIdCert().getSerial().getSerial());
  }
  @Test
  public void testGetCertSerials() {
    Consumer consumer = createConsumer();
    List<EntitlementCertificate> certificates = createEntitlementCertificates();

    when(mockedEntitlementCertServiceAdapter.listForConsumer(consumer)).thenReturn(certificates);
    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
    when(mockedEntitlementCurator.listByConsumer(consumer))
        .thenReturn(new ArrayList<Entitlement>());

    ConsumerResource consumerResource =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            mockedEntitlementCurator,
            null,
            mockedEntitlementCertServiceAdapter,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedPoolManager,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    List<CertificateSerialDto> serials =
        consumerResource.getEntitlementCertificateSerials(consumer.getUuid());

    verifyCertificateSerialNumbers(serials);
  }
  @Test
  public void testNoDryBindWhenAutobindDisabledForOwner() throws Exception {
    Consumer consumer = createConsumer();
    consumer.getOwner().setAutobindDisabled(true);
    when(mockedConsumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid())))
        .thenReturn(consumer);
    ManifestManager manifestManager = mock(ManifestManager.class);
    ConsumerResource consumerResource =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            null,
            productCurator,
            manifestManager);

    try {
      consumerResource.dryBind(consumer.getUuid(), "some-sla");
      fail("Should have thrown a BadRequestException.");
    } catch (BadRequestException e) {
      assertEquals("Owner has autobind disabled.", e.getMessage());
    }
  }
  /** Basic test. If invalid id is given, should throw {@link NotFoundException} */
  @Test(expected = NotFoundException.class)
  public void unbindByInvalidPoolIdShouldFail() {
    Consumer consumer = createConsumer();
    ConsumerCurator consumerCurator = mock(ConsumerCurator.class);
    when(consumerCurator.verifyAndLookupConsumer(eq("fake-uuid"))).thenReturn(consumer);
    EntitlementCurator entitlementCurator = mock(EntitlementCurator.class);

    when(entitlementCurator.listByConsumerAndPoolId(eq(consumer), any(String.class)))
        .thenReturn(new ArrayList<Entitlement>());

    ConsumerResource consumerResource =
        new ConsumerResource(
            consumerCurator,
            null,
            null,
            null,
            entitlementCurator,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    consumerResource.unbindByPool("fake-uuid", "Run Forest!");
  }
  @Test(expected = NotFoundException.class)
  public void unbindByInvalidSerialShouldFail() {
    Consumer consumer = createConsumer();
    ConsumerCurator consumerCurator = mock(ConsumerCurator.class);
    when(consumerCurator.verifyAndLookupConsumer(eq("fake uuid"))).thenReturn(consumer);

    EntitlementCurator entitlementCurator = mock(EntitlementCurator.class);
    when(entitlementCurator.find(any(Serializable.class))).thenReturn(null);

    ConsumerResource consumerResource =
        new ConsumerResource(
            consumerCurator,
            null,
            null,
            null,
            entitlementCurator,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    consumerResource.unbindBySerial("fake uuid", Long.valueOf(1234L));
  }
  /**
   * Test just verifies that entitler is called only once and it doesn't need any other object to
   * execute.
   */
  @Test
  public void testRegenerateEntitlementCertificateWithValidConsumer() {
    Consumer consumer = createConsumer();

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);

    CandlepinPoolManager mgr = mock(CandlepinPoolManager.class);
    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            mockedSubscriptionServiceAdapter,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mgr,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    cr.regenerateEntitlementCertificates(consumer.getUuid(), null, true);
    Mockito.verify(mgr, Mockito.times(1)).regenerateCertificatesOf(eq(consumer), eq(true));
  }
  /** Basic test. If invalid id is given, should throw {@link NotFoundException} */
  @Test(expected = NotFoundException.class)
  public void testRegenerateEntitlementCertificatesWithInvalidConsumerId() {
    ConsumerCurator consumerCurator = mock(ConsumerCurator.class);
    when(consumerCurator.verifyAndLookupConsumer(any(String.class)))
        .thenThrow(new NotFoundException(""));
    ConsumerResource consumerResource =
        new ConsumerResource(
            consumerCurator,
            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,
            consumerBindUtil,
            productCurator,
            null);

    consumerResource.regenerateEntitlementCertificates("xyz", null, true);
  }
  @Test(expected = RuntimeException.class)
  public void testExceptionFromCertGen() throws Exception {
    Consumer consumer = createConsumer();

    Entitlement e = Mockito.mock(Entitlement.class);
    Pool p = Mockito.mock(Pool.class);
    Subscription s = Mockito.mock(Subscription.class);
    when(e.getPool()).thenReturn(p);
    when(p.getSubscriptionId()).thenReturn("4444");

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
    when(mockedEntitlementCurator.find(eq("9999"))).thenReturn(e);
    when(mockedSubscriptionServiceAdapter.getSubscription(eq("4444"))).thenReturn(s);

    when(mockedEntitlementCertServiceAdapter.generateEntitlementCert(
            any(Entitlement.class), any(Product.class)))
        .thenThrow(new IOException());

    CandlepinPoolManager poolManager =
        new CandlepinPoolManager(
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            mockedEntitlementCurator,
            mockedConsumerCurator,
            null,
            null,
            null,
            null,
            mockedActivationKeyRules,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null);

    ConsumerResource consumerResource =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            mockedEntitlementCurator,
            null,
            mockedEntitlementCertServiceAdapter,
            null,
            null,
            null,
            null,
            null,
            null,
            poolManager,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    consumerResource.regenerateEntitlementCertificates(consumer.getUuid(), "9999", false);
  }