Search in sources :

Example 1 with UpstreamConsumer

use of org.candlepin.model.UpstreamConsumer in project candlepin by candlepin.

the class ConsumerImporter method store.

public void store(Owner owner, ConsumerDTO consumer, ConflictOverrides forcedConflicts, IdentityCertificate idcert) throws SyncDataFormatException {
    if (consumer.getUuid() == null) {
        throw new SyncDataFormatException(i18n.tr("No ID for upstream subscription management application."));
    }
    // Make sure no other owner is already using this upstream UUID:
    Owner alreadyUsing = curator.lookupWithUpstreamUuid(consumer.getUuid());
    if (alreadyUsing != null && !alreadyUsing.getKey().equals(owner.getKey())) {
        log.error("Cannot import manifest for org: {}", owner.getKey());
        log.error("Upstream distributor {} already in used by org: {}", consumer.getUuid(), alreadyUsing.getKey());
        // delete their manifest after which it could be used elsewhere.
        throw new SyncDataFormatException(i18n.tr("This subscription management application has already been imported by another owner."));
    }
    if (owner.getUpstreamUuid() != null && !owner.getUpstreamUuid().equals(consumer.getUuid())) {
        if (!forcedConflicts.isForced(Importer.Conflict.DISTRIBUTOR_CONFLICT)) {
            throw new ImportConflictException(i18n.tr("Owner has already imported from another subscription management application."), Importer.Conflict.DISTRIBUTOR_CONFLICT);
        } else {
            log.warn("Forcing import from a new distributor for org: {}", owner.getKey());
            log.warn("Old distributor UUID: {}", owner.getUpstreamUuid());
            log.warn("New distributor UUID: {}", consumer.getUuid());
        }
    }
    /*
         * WARNING: Strange quirk here, we create a certificate serial object here which does not
         * match the actual serial of the identity certificate. Presumably this is to prevent
         * potential conflicts with a serial that came from somewhere else. This is consistent with
         * importing entitlement certs (as subscription certs).
         */
    if (idcert != null) {
        CertificateSerial cs = new CertificateSerial();
        cs.setCollected(idcert.getSerial().isCollected());
        cs.setExpiration(idcert.getSerial().getExpiration());
        cs.setUpdated(idcert.getSerial().getUpdated());
        cs.setCreated(idcert.getSerial().getCreated());
        serialCurator.create(cs);
        idcert.setId(null);
        idcert.setSerial(cs);
        idCertCurator.create(idcert);
    }
    // create an UpstreamConsumer from the imported ConsumerDto
    ConsumerType type = new ConsumerType();
    populateEntity(type, consumer.getType());
    Owner ownerToUse = new Owner();
    if (consumer.getOwner() != null) {
        populateEntity(ownerToUse, consumer.getOwner());
    }
    UpstreamConsumer uc = new UpstreamConsumer(consumer.getName(), ownerToUse, type, consumer.getUuid());
    uc.setWebUrl(consumer.getUrlWeb());
    uc.setApiUrl(consumer.getUrlApi());
    uc.setIdCert(idcert);
    uc.setContentAccessMode(consumer.getContentAccessMode());
    owner.setUpstreamConsumer(uc);
    curator.merge(owner);
}
Also used : Owner(org.candlepin.model.Owner) CertificateSerial(org.candlepin.model.CertificateSerial) ConsumerType(org.candlepin.model.ConsumerType) UpstreamConsumer(org.candlepin.model.UpstreamConsumer)

Example 2 with UpstreamConsumer

use of org.candlepin.model.UpstreamConsumer in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithNullIdCertShouldNotFail.

/*
     * BZ#966860
     */
@Test
public void importConsumerWithNullIdCertShouldNotFail() throws ImporterException {
    Owner owner = mock(Owner.class);
    OwnerDTO ownerDTO = mock(OwnerDTO.class);
    ConsumerDTO consumer = mock(ConsumerDTO.class);
    ConsumerTypeDTO type = mock(ConsumerTypeDTO.class);
    when(owner.getUpstreamUuid()).thenReturn("test-uuid");
    when(consumer.getUuid()).thenReturn("test-uuid");
    when(consumer.getOwner()).thenReturn(ownerDTO);
    when(consumer.getType()).thenReturn(type);
    importer.store(owner, consumer, new ConflictOverrides(), null);
    // now verify that the owner has the upstream consumer set
    ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class);
    verify(owner).setUpstreamConsumer(arg.capture());
    assertEquals("test-uuid", arg.getValue().getUuid());
    verify(curator).merge(owner);
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) Test(org.junit.Test)

Example 3 with UpstreamConsumer

use of org.candlepin.model.UpstreamConsumer in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithMismatchedUuidShouldNotThrowExceptionIfForced.

@Test
public void importConsumerWithMismatchedUuidShouldNotThrowExceptionIfForced() throws ImporterException {
    Owner owner = mock(Owner.class);
    OwnerDTO ownerDTO = mock(OwnerDTO.class);
    ConsumerDTO consumer = mock(ConsumerDTO.class);
    ConsumerTypeDTO type = mock(ConsumerTypeDTO.class);
    when(owner.getUpstreamUuid()).thenReturn("another-test-uuid");
    when(consumer.getUuid()).thenReturn("test-uuid");
    when(consumer.getOwner()).thenReturn(ownerDTO);
    when(consumer.getType()).thenReturn(type);
    IdentityCertificate idCert = new IdentityCertificate();
    idCert.setSerial(new CertificateSerial());
    importer.store(owner, consumer, new ConflictOverrides(Importer.Conflict.DISTRIBUTOR_CONFLICT), idCert);
    // now verify that the owner has the upstream consumer set
    ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class);
    verify(owner).setUpstreamConsumer(arg.capture());
    assertEquals("test-uuid", arg.getValue().getUuid());
    verify(curator).merge(owner);
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) CertificateSerial(org.candlepin.model.CertificateSerial) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 4 with UpstreamConsumer

use of org.candlepin.model.UpstreamConsumer in project candlepin by candlepin.

the class OwnerResourceTest method upstreamConsumers.

@Test
public void upstreamConsumers() {
    Principal p = mock(Principal.class);
    OwnerCurator oc = mock(OwnerCurator.class);
    ProductCurator pc = mock(ProductCurator.class);
    UpstreamConsumer upstream = mock(UpstreamConsumer.class);
    Owner owner = mock(Owner.class);
    OwnerResource ownerres = new OwnerResource(oc, pc, null, null, i18n, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
    when(oc.lookupByKey(eq("admin"))).thenReturn(owner);
    when(owner.getUpstreamConsumer()).thenReturn(upstream);
    List<UpstreamConsumerDTO> results = ownerres.getUpstreamConsumers(p, "admin");
    assertNotNull(results);
    assertEquals(1, results.size());
}
Also used : OwnerCurator(org.candlepin.model.OwnerCurator) Owner(org.candlepin.model.Owner) UpstreamConsumerDTO(org.candlepin.dto.api.v1.UpstreamConsumerDTO) ProductCurator(org.candlepin.model.ProductCurator) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ConsumerPrincipal(org.candlepin.auth.ConsumerPrincipal) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) Test(org.junit.Test)

Example 5 with UpstreamConsumer

use of org.candlepin.model.UpstreamConsumer in project candlepin by candlepin.

the class UndoImportsJobTest method testUndoImport.

@Test
public void testUndoImport() throws JobExecutionException, IOException, ImporterException {
    // We need proper curators for this test
    this.poolManager = this.poolManagerBase;
    this.ownerCurator = super.ownerCurator;
    this.exportCurator = this.exportCuratorBase;
    this.undoImportsJob = new UndoImportsJob(this.i18n, this.ownerCurator, this.poolManager, this.subAdapter, this.exportCurator, this.importRecordCurator);
    // Create owner w/upstream consumer
    Owner owner1 = TestUtil.createOwner();
    Owner owner2 = TestUtil.createOwner();
    ConsumerType type = this.createConsumerType();
    UpstreamConsumer uc1 = new UpstreamConsumer("uc1", null, type, "uc1");
    UpstreamConsumer uc2 = new UpstreamConsumer("uc2", null, type, "uc2");
    this.ownerCurator.create(owner1);
    this.ownerCurator.create(owner2);
    owner1.setUpstreamConsumer(uc1);
    owner1.setUpstreamConsumer(uc2);
    this.ownerCurator.merge(owner1);
    this.ownerCurator.merge(owner2);
    // Create metadata
    ExporterMetadata metadata1 = new ExporterMetadata(ExporterMetadata.TYPE_PER_USER, new Date(), owner1);
    ExporterMetadata metadata2 = new ExporterMetadata(ExporterMetadata.TYPE_PER_USER, new Date(), owner2);
    this.exportCurator.create(metadata1);
    this.exportCurator.create(metadata2);
    // Create pools w/upstream pool IDs
    Pool pool1 = this.createPool("pool1", owner1, true, PoolType.NORMAL);
    Pool pool2 = this.createPool("pool2", owner1, true, PoolType.BONUS);
    Pool pool3 = this.createPool("pool3", owner1, false, PoolType.NORMAL);
    Pool pool4 = this.createPool("pool4", owner1, false, PoolType.BONUS);
    Pool pool5 = this.createPool("pool5", owner1, true, PoolType.ENTITLEMENT_DERIVED);
    Pool pool6 = this.createPool("pool6", owner1, false, PoolType.ENTITLEMENT_DERIVED);
    Pool pool7 = this.createPool("pool7", owner2, true, PoolType.NORMAL);
    Pool pool8 = this.createPool("pool8", owner2, true, PoolType.BONUS);
    Pool pool9 = this.createPool("pool9", owner2, true, PoolType.ENTITLEMENT_DERIVED);
    // Create an ueber certificate for the owner.
    UeberCertificate uebercert = ueberCertGenerator.generate(owner1.getKey(), this.setupAdminPrincipal("test_admin"));
    assertNotNull(uebercert);
    // Verify initial state
    assertEquals(Arrays.asList(pool1, pool2, pool3, pool4, pool5, pool6), this.poolManager.listPoolsByOwner(owner1).list());
    assertEquals(Arrays.asList(pool7, pool8, pool9), this.poolManager.listPoolsByOwner(owner2).list());
    assertEquals(metadata1, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner1));
    assertEquals(metadata2, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner2));
    assertEquals(0, this.importRecordCurator.findRecords(owner1).list().size());
    assertEquals(0, this.importRecordCurator.findRecords(owner2).list().size());
    // Execute job
    Principal principal = new UserPrincipal("JarJarBinks", null, true);
    this.jobDataMap.put(JobStatus.TARGET_TYPE, JobStatus.TargetType.OWNER);
    this.jobDataMap.put(JobStatus.TARGET_ID, owner1.getId());
    this.jobDataMap.put(UndoImportsJob.OWNER_KEY, owner1.getKey());
    this.jobDataMap.put(PinsetterJobListener.PRINCIPAL_KEY, principal);
    // since we locking owner we need start transaction
    beginTransaction();
    this.undoImportsJob.toExecute(this.jobContext);
    commitTransaction();
    // Verify deletions -- Ueber pools should not get deleted.
    assertEquals(Arrays.asList(pool3, pool4, pool5, pool6), this.poolManager.listPoolsByOwner(owner1).list());
    assertEquals(Arrays.asList(pool7, pool8, pool9), this.poolManager.listPoolsByOwner(owner2).list());
    assertNull(exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner1));
    assertEquals(metadata2, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner2));
    assertNull(owner1.getUpstreamConsumer());
    List<ImportRecord> records = this.importRecordCurator.findRecords(owner1).list();
    assertEquals(1, records.size());
    assertEquals(ImportRecord.Status.DELETE, records.get(0).getStatus());
    assertEquals(0, this.importRecordCurator.findRecords(owner2).list().size());
}
Also used : Owner(org.candlepin.model.Owner) UeberCertificate(org.candlepin.model.UeberCertificate) ExporterMetadata(org.candlepin.model.ExporterMetadata) Pool(org.candlepin.model.Pool) ConsumerType(org.candlepin.model.ConsumerType) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ImportRecord(org.candlepin.model.ImportRecord) Date(java.util.Date) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Aggregations

UpstreamConsumer (org.candlepin.model.UpstreamConsumer)14 Owner (org.candlepin.model.Owner)12 Test (org.junit.Test)7 ConsumerDTO (org.candlepin.dto.manifest.v1.ConsumerDTO)4 Principal (org.candlepin.auth.Principal)3 ConsumerTypeDTO (org.candlepin.dto.manifest.v1.ConsumerTypeDTO)3 OwnerDTO (org.candlepin.dto.manifest.v1.OwnerDTO)3 CertificateSerial (org.candlepin.model.CertificateSerial)3 ConsumerType (org.candlepin.model.ConsumerType)3 Date (java.util.Date)2 UserPrincipal (org.candlepin.auth.UserPrincipal)2 UpstreamConsumerDTO (org.candlepin.dto.api.v1.UpstreamConsumerDTO)2 ExporterMetadata (org.candlepin.model.ExporterMetadata)2 IdentityCertificate (org.candlepin.model.IdentityCertificate)2 ImportRecord (org.candlepin.model.ImportRecord)2 ImportUpstreamConsumer (org.candlepin.model.ImportUpstreamConsumer)2 Pool (org.candlepin.model.Pool)2 Transactional (com.google.inject.persist.Transactional)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1