Search in sources :

Example 11 with ImportRecord

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

the class ImporterTest method testRecordImportNoActiveSubsFound.

@Test
public void testRecordImportNoActiveSubsFound() {
    String expectedOwnerKey = "TEST_OWNER";
    Owner owner = new Owner(expectedOwnerKey);
    EventSink eventSinkMock = mock(EventSink.class);
    ImportRecordCurator importRecordCurator = mock(ImportRecordCurator.class);
    Importer importer = new Importer(null, null, null, null, null, null, null, null, config, null, null, eventSinkMock, i18n, null, null, su, importRecordCurator, this.mockSubReconciler, this.ec, this.translator);
    Map<String, Object> data = new HashMap<>();
    data.put("subscriptions", new ArrayList<Subscription>());
    ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip");
    assertEquals(ImportRecord.Status.SUCCESS_WITH_WARNING, record.getStatus());
    assertEquals(owner.getKey() + " file imported successfully." + "No active subscriptions found in the file.", record.getStatusMessage());
    verify(eventSinkMock, never()).emitSubscriptionExpired(any(Subscription.class));
    verify(importRecordCurator).create(eq(record));
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) ImportRecordCurator(org.candlepin.model.ImportRecordCurator) EventSink(org.candlepin.audit.EventSink) Subscription(org.candlepin.model.dto.Subscription) ImportRecord(org.candlepin.model.ImportRecord) Test(org.junit.Test)

Example 12 with ImportRecord

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

the class ImporterTest method testRecordImportExpiredSubsFound.

@Test
public void testRecordImportExpiredSubsFound() {
    String expectedOwnerKey = "TEST_OWNER";
    Owner owner = new Owner(expectedOwnerKey);
    EventSink eventSinkMock = mock(EventSink.class);
    ImportRecordCurator importRecordCurator = mock(ImportRecordCurator.class);
    Importer importer = new Importer(null, null, null, null, null, null, null, null, config, null, null, eventSinkMock, i18n, null, null, su, importRecordCurator, this.mockSubReconciler, this.ec, this.translator);
    Map<String, Object> data = new HashMap<>();
    List<Subscription> subscriptions = new ArrayList<>();
    Subscription subscription1 = new Subscription();
    // expires tomorrow
    subscription1.setEndDate(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24)));
    subscriptions.add(subscription1);
    Subscription subscription2 = new Subscription();
    // expires yesterday
    subscription2.setEndDate(new Date((new Date()).getTime() - (1000 * 60 * 60 * 24)));
    subscriptions.add(subscription2);
    data.put("subscriptions", subscriptions);
    ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip");
    assertEquals(ImportRecord.Status.SUCCESS_WITH_WARNING, record.getStatus());
    assertEquals(owner.getKey() + " file imported successfully." + "One or more inactive subscriptions found in the file.", record.getStatusMessage());
    verify(eventSinkMock, never()).emitSubscriptionExpired(subscription1);
    verify(eventSinkMock).emitSubscriptionExpired(subscription2);
    verify(importRecordCurator).create(eq(record));
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ImportRecord(org.candlepin.model.ImportRecord) Date(java.util.Date) ImportRecordCurator(org.candlepin.model.ImportRecordCurator) EventSink(org.candlepin.audit.EventSink) Subscription(org.candlepin.model.dto.Subscription) Test(org.junit.Test)

Example 13 with ImportRecord

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

the class ImporterTest method testRecordImportSetsUpstreamConsumerFromOwner.

@Test
public void testRecordImportSetsUpstreamConsumerFromOwner() {
    String expectedOwnerKey = "TEST_OWNER";
    Owner owner = new Owner(expectedOwnerKey);
    UpstreamConsumer uc = new UpstreamConsumer("uc", owner, new ConsumerType(ConsumerType.ConsumerTypeEnum.CANDLEPIN), "uuid");
    uc.setContentAccessMode("mode");
    owner.setUpstreamConsumer(uc);
    EventSink eventSinkMock = mock(EventSink.class);
    ImportRecordCurator importRecordCurator = mock(ImportRecordCurator.class);
    Importer importer = new Importer(null, null, null, null, null, null, null, null, config, null, null, eventSinkMock, i18n, null, null, su, importRecordCurator, this.mockSubReconciler, this.ec, this.translator);
    Meta meta = new Meta("1.0", new Date(), "test-user", "candlepin", "testcdn");
    Map<String, Object> data = new HashMap<>();
    data.put("meta", meta);
    data.put("subscriptions", new ArrayList<Subscription>());
    ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip");
    ImportUpstreamConsumer iuc = record.getUpstreamConsumer();
    assertNotNull(iuc);
    assertEquals(uc.getOwnerId(), iuc.getOwnerId());
    assertEquals(uc.getName(), iuc.getName());
    assertEquals(uc.getUuid(), iuc.getUuid());
    assertEquals(uc.getType(), iuc.getType());
    assertEquals(uc.getWebUrl(), iuc.getWebUrl());
    assertEquals(uc.getApiUrl(), iuc.getApiUrl());
    assertEquals(uc.getContentAccessMode(), iuc.getContentAccessMode());
    verify(importRecordCurator).create(eq(record));
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) ImportUpstreamConsumer(org.candlepin.model.ImportUpstreamConsumer) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ImportUpstreamConsumer(org.candlepin.model.ImportUpstreamConsumer) ImportRecord(org.candlepin.model.ImportRecord) Date(java.util.Date) ImportRecordCurator(org.candlepin.model.ImportRecordCurator) ConsumerType(org.candlepin.model.ConsumerType) EventSink(org.candlepin.audit.EventSink) Subscription(org.candlepin.model.dto.Subscription) Test(org.junit.Test)

Example 14 with ImportRecord

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

the class Importer method recordImportFailure.

public void recordImportFailure(Owner owner, Throwable error, String filename) {
    ImportRecord record = new ImportRecord(owner);
    log.error("Recording import failure", error);
    if (error instanceof ImporterException) {
        Meta meta = (Meta) ((ImporterException) error).getCollectedData().get("meta");
        if (meta != null) {
            record.setGeneratedBy(meta.getPrincipalName());
            record.setGeneratedDate(meta.getCreated());
        }
    }
    record.setUpstreamConsumer(createImportUpstreamConsumer(owner, null));
    record.setFileName(filename);
    record.recordStatus(ImportRecord.Status.FAILURE, error.getMessage());
    this.importRecordCurator.create(record);
}
Also used : ImportRecord(org.candlepin.model.ImportRecord)

Example 15 with ImportRecord

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

the class Importer method recordImportSuccess.

/**
 * Records a successful import of a manifest.
 *
 * @param owner the owner that the manifest was imported into.
 * @param data the data to store in this record.
 * @param forcedConflicts the conflicts that were forced.
 * @param filename the name of the originally uploaded file.
 * @return the newly created {@link ImportRecord}.
 */
public ImportRecord recordImportSuccess(Owner owner, Map<String, Object> data, ConflictOverrides forcedConflicts, String filename) {
    ImportRecord record = new ImportRecord(owner);
    Meta meta = (Meta) data.get("meta");
    if (meta != null) {
        record.setGeneratedBy(meta.getPrincipalName());
        record.setGeneratedDate(meta.getCreated());
    }
    record.setUpstreamConsumer(createImportUpstreamConsumer(owner, null));
    record.setFileName(filename);
    List<Subscription> subscriptions = (List<Subscription>) data.get("subscriptions");
    boolean activeSubscriptionFound = false, expiredSubscriptionFound = false;
    Date currentDate = new Date();
    for (Subscription subscription : subscriptions) {
        if (subscription.getEndDate() == null || subscription.getEndDate().after(currentDate)) {
            activeSubscriptionFound = true;
        } else {
            expiredSubscriptionFound = true;
            sink.emitSubscriptionExpired(subscription);
        }
    }
    String msg = i18n.tr("{0} file imported successfully.", owner.getKey());
    if (!forcedConflicts.isEmpty()) {
        msg = i18n.tr("{0} file imported forcibly.", owner.getKey());
    }
    if (!activeSubscriptionFound) {
        msg += i18n.tr("No active subscriptions found in the file.");
        record.recordStatus(ImportRecord.Status.SUCCESS_WITH_WARNING, msg);
    } else if (expiredSubscriptionFound) {
        msg += i18n.tr("One or more inactive subscriptions found in the file.");
        record.recordStatus(ImportRecord.Status.SUCCESS_WITH_WARNING, msg);
    } else {
        record.recordStatus(ImportRecord.Status.SUCCESS, msg);
    }
    this.importRecordCurator.create(record);
    return record;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ImportRecord(org.candlepin.model.ImportRecord) Subscription(org.candlepin.model.dto.Subscription) Date(java.util.Date)

Aggregations

ImportRecord (org.candlepin.model.ImportRecord)17 Test (org.junit.Test)11 Owner (org.candlepin.model.Owner)10 Date (java.util.Date)7 EventSink (org.candlepin.audit.EventSink)6 Subscription (org.candlepin.model.dto.Subscription)6 HashMap (java.util.HashMap)5 ImportRecordCurator (org.candlepin.model.ImportRecordCurator)5 ArrayList (java.util.ArrayList)4 ConflictOverrides (org.candlepin.sync.ConflictOverrides)3 Transactional (com.google.inject.persist.Transactional)2 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2 ConsumerType (org.candlepin.model.ConsumerType)2 ExporterMetadata (org.candlepin.model.ExporterMetadata)2 Pool (org.candlepin.model.Pool)2 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Principal (org.candlepin.auth.Principal)1