Search in sources :

Example 6 with ManifestFile

use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.

the class ManifestManagerTest method testWriteStoredExportToResponseFailsWhenManifestFileNotFound.

@Test(expected = NotFoundException.class)
public void testWriteStoredExportToResponseFailsWhenManifestFileNotFound() throws Exception {
    HttpServletResponse response = mock(HttpServletResponse.class);
    Consumer exportedConsumer = this.createMockConsumer(true);
    String manifestId = "124";
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getTargetId()).thenReturn(exportedConsumer.getUuid());
    when(fileService.get(eq(manifestId))).thenReturn(null);
    manager.writeStoredExportToResponse(manifestId, exportedConsumer.getUuid(), response);
}
Also used : Consumer(org.candlepin.model.Consumer) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Example 7 with ManifestFile

use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.

the class ManifestManagerTest method testImportStoredManifest.

@Test
public void testImportStoredManifest() throws Exception {
    Owner owner = TestUtil.createOwner();
    String fileId = "1234";
    String filename = "manifest.zip";
    ConflictOverrides overrides = new ConflictOverrides(Conflict.DISTRIBUTOR_CONFLICT);
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getId()).thenReturn(fileId);
    when(fileService.get(eq(fileId))).thenReturn(manifest);
    manager.importStoredManifest(owner, fileId, overrides, filename);
    verify(importer).loadStoredExport(eq(manifest), eq(owner), eq(overrides), eq(filename));
    verify(fileService).delete(fileId);
}
Also used : ConflictOverrides(org.candlepin.sync.ConflictOverrides) Owner(org.candlepin.model.Owner) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Example 8 with ManifestFile

use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.

the class ManifestManagerTest method testWriteStoredExportToResponseFailsWhenConsumerIdDoesntMatchManifest.

@Test(expected = BadRequestException.class)
public void testWriteStoredExportToResponseFailsWhenConsumerIdDoesntMatchManifest() throws Exception {
    HttpServletResponse response = mock(HttpServletResponse.class);
    Consumer exportedConsumer = this.createMockConsumer(true);
    String manifestId = "124";
    when(consumerCurator.verifyAndLookupConsumer(eq(exportedConsumer.getUuid()))).thenReturn(exportedConsumer);
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getTargetId()).thenReturn("another-consumer-uuid");
    when(fileService.get(eq(manifestId))).thenReturn(manifest);
    manager.writeStoredExportToResponse(manifestId, exportedConsumer.getUuid(), response);
}
Also used : Consumer(org.candlepin.model.Consumer) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Example 9 with ManifestFile

use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.

the class ManifestManager method generateAndStoreManifest.

/**
 * Generates a manifest for the specifed consumer and stores the resulting file via the
 * {@link ManifestFileService}.
 *
 * @param consumerUuid the target consumer's UUID.
 * @param cdnLabel the CDN label to store in the meta file.
 * @param webUrl the URL pointing to the manifest's originating web application.
 * @param apiUrl the API URL pointing to the manifest's originating candlepin API.
 * @param extensionData data to be passed to the {@link ExportExtensionAdapter} when creating
 *                      a new export of the target consumer.
 * @return an {@link ExportResult} containing the details of the stored file.
 * @throws ExportCreationException if there are any issues generating the manifest.
 */
public ExportResult generateAndStoreManifest(String consumerUuid, String cdnLabel, String webUrl, String apiUrl, Map<String, String> extensionData) throws ExportCreationException {
    Consumer consumer = validateConsumerForExport(consumerUuid, cdnLabel);
    File export = null;
    try {
        poolManager.regenerateDirtyEntitlements(entitlementCurator.listByConsumer(consumer));
        export = exporter.getFullExport(consumer, cdnLabel, webUrl, apiUrl, extensionData);
        ManifestFile manifestFile = storeExport(export, consumer);
        sink.queueEvent(eventFactory.exportCreated(consumer));
        return new ExportResult(consumer.getUuid(), manifestFile.getId());
    } catch (ManifestFileServiceException e) {
        throw new ExportCreationException("Unable to create export archive", e);
    } finally {
        // We no longer need the export work directory since the archive has been saved in the DB.
        if (export != null) {
            File workDir = export.getParentFile();
            try {
                FileUtils.deleteDirectory(workDir);
            } catch (IOException ioe) {
            // It'll get cleaned up by the ManifestCleanerJob if it couldn't
            // be deleted for some reason.
            }
        }
    }
}
Also used : Consumer(org.candlepin.model.Consumer) ExportCreationException(org.candlepin.sync.ExportCreationException) IOException(java.io.IOException) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ManifestFile(org.candlepin.sync.file.ManifestFile) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) ExportResult(org.candlepin.sync.ExportResult)

Example 10 with ManifestFile

use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.

the class ManifestManagerTest method testGenerateAndStoreManifest.

@Test
public void testGenerateAndStoreManifest() throws Exception {
    Consumer consumer = this.createMockConsumer(true);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    Event event = mock(Event.class);
    when(eventFactory.exportCreated(eq(consumer))).thenReturn(event);
    UserPrincipal principal = TestUtil.createOwnerPrincipal();
    when(principalProvider.get()).thenReturn(principal);
    String exportId = "export-id";
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getId()).thenReturn(exportId);
    when(fileService.store(eq(ManifestFileType.EXPORT), any(File.class), eq(principal.getName()), any(String.class))).thenReturn(manifest);
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    List<Entitlement> ents = new ArrayList<>();
    when(entitlementCurator.listByConsumer(eq(consumer))).thenReturn(ents);
    ExportResult result = manager.generateAndStoreManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
    assertEquals(consumer.getUuid(), result.getExportedConsumer());
    assertEquals(exportId, result.getExportId());
    verify(entitlementCurator).listByConsumer(eq(consumer));
    verify(exporter).getFullExport(eq(consumer), eq(cdn.getLabel()), eq(webAppPrefix), eq(apiUrl), eq(extData));
    verify(eventFactory).exportCreated(eq(consumer));
    verify(eventSink).queueEvent(eq(event));
    verify(fileService).delete(eq(ManifestFileType.EXPORT), eq(consumer.getUuid()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cdn(org.candlepin.model.Cdn) UserPrincipal(org.candlepin.auth.UserPrincipal) ManifestFile(org.candlepin.sync.file.ManifestFile) Consumer(org.candlepin.model.Consumer) Event(org.candlepin.audit.Event) Entitlement(org.candlepin.model.Entitlement) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ExportResult(org.candlepin.sync.ExportResult) Test(org.junit.Test)

Aggregations

ManifestFile (org.candlepin.sync.file.ManifestFile)11 Consumer (org.candlepin.model.Consumer)8 Test (org.junit.Test)8 File (java.io.File)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Event (org.candlepin.audit.Event)3 UserPrincipal (org.candlepin.auth.UserPrincipal)3 Cdn (org.candlepin.model.Cdn)3 Transactional (com.google.inject.persist.Transactional)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2 Entitlement (org.candlepin.model.Entitlement)2 Owner (org.candlepin.model.Owner)2 ConflictOverrides (org.candlepin.sync.ConflictOverrides)2 ExportCreationException (org.candlepin.sync.ExportCreationException)2 ExportResult (org.candlepin.sync.ExportResult)2 ManifestFileServiceException (org.candlepin.sync.file.ManifestFileServiceException)2