Search in sources :

Example 46 with ConsumerDTO

use of org.candlepin.dto.rules.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithNullUuidOnOwnerShouldSetUuid.

@Test
public void importConsumerWithNullUuidOnOwnerShouldSetUuid() throws ImporterException {
    OwnerDTO ownerDTO = mock(OwnerDTO.class);
    Owner owner = mock(Owner.class);
    ConsumerDTO consumer = mock(ConsumerDTO.class);
    ConsumerTypeDTO type = mock(ConsumerTypeDTO.class);
    when(ownerDTO.getId()).thenReturn("test-owner-id");
    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(), 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 47 with ConsumerDTO

use of org.candlepin.dto.rules.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithSameUuidOnAnotherOwnerShouldThrowException.

@Test(expected = SyncDataFormatException.class)
public void importConsumerWithSameUuidOnAnotherOwnerShouldThrowException() throws ImporterException {
    Owner owner = new Owner();
    UpstreamConsumer uc = new UpstreamConsumer("test-uuid");
    owner.setUpstreamConsumer(uc);
    ConsumerDTO consumer = new ConsumerDTO();
    consumer.setUuid("test-uuid");
    Owner anotherOwner = new Owner("other", "Other");
    anotherOwner.setId("blah");
    anotherOwner.setUpstreamConsumer(uc);
    when(curator.lookupWithUpstreamUuid(consumer.getUuid())).thenReturn(anotherOwner);
    importer.store(owner, consumer, new ConflictOverrides(), null);
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) Test(org.junit.Test)

Example 48 with ConsumerDTO

use of org.candlepin.dto.rules.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithMismatchedUuidShouldThrowException.

@Test
public void importConsumerWithMismatchedUuidShouldThrowException() throws ImporterException {
    Owner owner = mock(Owner.class);
    OwnerDTO ownerDTO = mock(OwnerDTO.class);
    ConsumerDTO consumer = mock(ConsumerDTO.class);
    when(owner.getUpstreamUuid()).thenReturn("another-test-uuid");
    when(consumer.getUuid()).thenReturn("test-uuid");
    when(consumer.getOwner()).thenReturn(ownerDTO);
    try {
        importer.store(owner, consumer, new ConflictOverrides(), null);
        fail();
    } catch (ImportConflictException e) {
        assertFalse(e.message().getConflicts().isEmpty());
        assertTrue(e.message().getConflicts().contains(Importer.Conflict.DISTRIBUTOR_CONFLICT));
    }
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) Test(org.junit.Test)

Example 49 with ConsumerDTO

use of org.candlepin.dto.rules.v1.ConsumerDTO in project candlepin by candlepin.

the class Importer method importConsumer.

protected ConsumerDTO importConsumer(Owner owner, File consumerFile, File[] upstreamConsumer, ConflictOverrides forcedConflicts, Meta meta) throws IOException, SyncDataFormatException {
    IdentityCertificate idcert = null;
    for (File uc : upstreamConsumer) {
        if (uc.getName().endsWith(".json")) {
            log.debug("Import upstream consumeridentity certificate: {}", uc.getName());
            try (Reader reader = new FileReader(uc)) {
                CertificateDTO dtoCert = mapper.readValue(reader, CertificateDTO.class);
                idcert = new IdentityCertificate();
                populateEntity(idcert, dtoCert);
            }
        } else {
            log.warn("Extra file found in upstream_consumer directory: {}", uc.getName());
        }
    }
    ConsumerImporter importer = new ConsumerImporter(ownerCurator, idCertCurator, i18n, csCurator);
    Reader reader = null;
    ConsumerDTO consumer = null;
    try {
        reader = new FileReader(consumerFile);
        consumer = importer.createObject(mapper, reader);
        // we can not rely on the actual ConsumerType in the ConsumerDto
        // because it could have an id not in our database. We need to
        // stick with the label. Hence we need to lookup the ACTUAL type
        // by label here before attempting to store the UpstreamConsumer
        ConsumerType type = consumerTypeCurator.lookupByLabel(consumer.getType().getLabel());
        consumer.setType(this.translator.translate(type, ConsumerTypeDTO.class));
        // the metadata
        if (StringUtils.isEmpty(consumer.getUrlWeb())) {
            consumer.setUrlWeb(meta.getWebAppPrefix());
        }
        importer.store(owner, consumer, forcedConflicts, idcert);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return consumer;
}
Also used : CertificateDTO(org.candlepin.dto.manifest.v1.CertificateDTO) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) ConsumerType(org.candlepin.model.ConsumerType) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 50 with ConsumerDTO

use of org.candlepin.dto.rules.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerExporter method export.

void export(ObjectMapper mapper, Writer writer, Consumer consumer, String weburl, String apiurl) throws IOException {
    ConsumerDTO consumerDTO = this.translator.translate(consumer, ConsumerDTO.class);
    consumerDTO.setUrlApi(apiurl);
    consumerDTO.setUrlWeb(weburl);
    mapper.writeValue(writer, consumerDTO);
}
Also used : ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO)

Aggregations

Test (org.junit.Test)70 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)64 Consumer (org.candlepin.model.Consumer)37 UserPrincipal (org.candlepin.auth.UserPrincipal)19 Owner (org.candlepin.model.Owner)19 ConsumerType (org.candlepin.model.ConsumerType)16 HashSet (java.util.HashSet)15 ConsumerDTO (org.candlepin.dto.manifest.v1.ConsumerDTO)15 Principal (org.candlepin.auth.Principal)14 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)13 TrustedUserPrincipal (org.candlepin.auth.TrustedUserPrincipal)12 Set (java.util.Set)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 TestUtil.createConsumerDTO (org.candlepin.test.TestUtil.createConsumerDTO)8 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)7 ConsumerTypeDTO (org.candlepin.dto.manifest.v1.ConsumerTypeDTO)7 OwnerDTO (org.candlepin.dto.manifest.v1.OwnerDTO)7 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)7 Entitlement (org.candlepin.model.Entitlement)5