Search in sources :

Example 1 with ImporterException

use of org.candlepin.sync.ImporterException in project candlepin by candlepin.

the class OwnerResourceTest method testImportManifestFailure.

@Test
public void testImportManifestFailure() throws IOException, ImporterException {
    ManifestManager manifestManager = mock(ManifestManager.class);
    EventSink es = mock(EventSink.class);
    OwnerResource thisOwnerResource = new OwnerResource(ownerCurator, productCurator, null, null, i18n, es, eventFactory, null, null, manifestManager, null, null, null, null, importRecordCurator, null, null, null, null, null, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
    MultipartInput input = mock(MultipartInput.class);
    InputPart part = mock(InputPart.class);
    File archive = mock(File.class);
    List<InputPart> parts = new ArrayList<>();
    parts.add(part);
    MultivaluedMap<String, String> mm = new MultivaluedMapImpl<>();
    List<String> contDis = new ArrayList<>();
    contDis.add("form-data; name=\"upload\"; filename=\"test_file.zip\"");
    mm.put("Content-Disposition", contDis);
    when(input.getParts()).thenReturn(parts);
    when(part.getHeaders()).thenReturn(mm);
    when(part.getBody(any(GenericType.class))).thenReturn(archive);
    ImporterException expectedException = new ImporterException("Bad import");
    when(manifestManager.importManifest(eq(owner), any(File.class), any(String.class), any(ConflictOverrides.class))).thenThrow(expectedException);
    try {
        thisOwnerResource.importManifest(owner.getKey(), new String[] {}, input);
        fail("Expected IseException was not thrown");
    } catch (IseException ise) {
    // expected, so we catch and go on.
    }
    verify(manifestManager).recordImportFailure(eq(owner), eq(expectedException), eq("test_file.zip"));
}
Also used : ImporterException(org.candlepin.sync.ImporterException) ConflictOverrides(org.candlepin.sync.ConflictOverrides) GenericType(org.jboss.resteasy.util.GenericType) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Matchers.anyString(org.mockito.Matchers.anyString) ManifestManager(org.candlepin.controller.ManifestManager) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) IseException(org.candlepin.common.exceptions.IseException) EventSink(org.candlepin.audit.EventSink) File(java.io.File) Test(org.junit.Test)

Example 2 with ImporterException

use of org.candlepin.sync.ImporterException in project candlepin by candlepin.

the class OwnerResource method importManifest.

/**
 * Imports a manifest zip file for the given organization.
 *
 * This will bring in any products, content, and subscriptions that were assigned to
 * the distributor who generated the manifest.
 *
 * @deprecated use GET /owners/:owner_key/imports/async
 *
 * @return a ImportRecord object if the import is successful.
 * @httpcode 400
 * @httpcode 404
 * @httpcode 500
 * @httpcode 200
 * @httpcode 409
 */
@POST
@Path("{owner_key}/imports")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(notes = "Imports a manifest zip file for the given organization. " + "This will bring in any products, content, and subscriptions that were " + "assigned to the distributor who generated the manifest.", value = "Import Manifest")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 500, message = ""), @ApiResponse(code = 409, message = "") })
@Deprecated
public ImportRecord importManifest(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @QueryParam("force") String[] overrideConflicts, MultipartInput input) {
    ConflictOverrides overrides = processConflictOverrideParams(overrideConflicts);
    UploadMetadata fileData = new UploadMetadata();
    Owner owner = findOwnerByKey(ownerKey);
    try {
        fileData = getArchiveFromResponse(input);
        return manifestManager.importManifest(owner, fileData.getData(), fileData.getUploadedFilename(), overrides);
    } catch (IOException e) {
        log.error("Reading error during importing", e);
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw new IseException(i18n.tr("Error reading export archive"), e);
    }// These come back with internationalized messages, so we can transfer:
     catch (SyncDataFormatException e) {
        log.error("Format error of the data in a manifest", e);
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw new BadRequestException(e.getMessage(), e);
    } catch (ImporterException e) {
        log.error("Problem with archive", e);
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw new IseException(e.getMessage(), e);
    }// to pass on the http return code
     catch (CandlepinException e) {
        log.error("Recording import failure", e);
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw e;
    } finally {
        log.info("Import attempt completed for owner {}", owner.getDisplayName());
    }
}
Also used : ImporterException(org.candlepin.sync.ImporterException) ConflictOverrides(org.candlepin.sync.ConflictOverrides) CandlepinException(org.candlepin.common.exceptions.CandlepinException) Owner(org.candlepin.model.Owner) IseException(org.candlepin.common.exceptions.IseException) SyncDataFormatException(org.candlepin.sync.SyncDataFormatException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ImporterException

use of org.candlepin.sync.ImporterException in project candlepin by candlepin.

the class ImportJob method toExecute.

@Override
public void toExecute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap map = context.getMergedJobDataMap();
    String ownerKey = (String) map.get(JobStatus.TARGET_ID);
    ConflictOverrides overrides = new ConflictOverrides((String[]) map.get(CONFLICT_OVERRIDES));
    String storedFileId = (String) map.get(STORED_FILE_ID);
    String uploadedFileName = (String) map.get(UPLOADED_FILE_NAME);
    Throwable caught = null;
    Owner targetOwner = null;
    try {
        targetOwner = ownerCurator.lookupByKey(ownerKey);
        if (targetOwner == null) {
            throw new NotFoundException(String.format("Owner %s was not found.", ownerKey));
        }
        ImportRecord importRecord = manifestManager.importStoredManifest(targetOwner, storedFileId, overrides, uploadedFileName);
        context.setResult(importRecord);
    }// info about the exception that was thrown (CandlepinException).
     catch (SyncDataFormatException e) {
        caught = new BadRequestException(e.getMessage(), e);
    } catch (ImporterException e) {
        caught = new IseException(e.getMessage(), e);
    } catch (Exception e) {
        caught = e;
    }
    if (caught != null) {
        log.error("ImportJob encountered a problem.", caught);
        manifestManager.recordImportFailure(targetOwner, caught, uploadedFileName);
        context.setResult(caught.getMessage());
        // If an exception was thrown, the importer's transaction was rolled
        // back. We want to make sure that the file gets deleted so that it
        // doesn't take up disk space. It may be possible that the file was
        // already deleted, but we attempt it anyway.
        manifestManager.deleteStoredManifest(storedFileId);
        throw new JobExecutionException(caught.getMessage(), caught, false);
    }
}
Also used : ImporterException(org.candlepin.sync.ImporterException) ConflictOverrides(org.candlepin.sync.ConflictOverrides) JobDataMap(org.quartz.JobDataMap) Owner(org.candlepin.model.Owner) SyncDataFormatException(org.candlepin.sync.SyncDataFormatException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ImportRecord(org.candlepin.model.ImportRecord) NotFoundException(org.candlepin.common.exceptions.NotFoundException) SyncDataFormatException(org.candlepin.sync.SyncDataFormatException) JobExecutionException(org.quartz.JobExecutionException) IseException(org.candlepin.common.exceptions.IseException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ImporterException(org.candlepin.sync.ImporterException) JobExecutionException(org.quartz.JobExecutionException) IseException(org.candlepin.common.exceptions.IseException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 4 with ImporterException

use of org.candlepin.sync.ImporterException in project candlepin by candlepin.

the class ImportJobTest method ensureJobFailure.

@Test
public void ensureJobFailure() throws Exception {
    String archiveFilePath = "/path/to/some/file.zip";
    ConflictOverrides co = new ConflictOverrides();
    String uploadedFileName = "test.zip";
    String expectedMessage = "Expected Exception Message";
    JobDetail detail = job.scheduleImport(owner, archiveFilePath, uploadedFileName, co);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(manifestManager.importStoredManifest(eq(owner), any(String.class), any(ConflictOverrides.class), eq(uploadedFileName))).thenThrow(new ImporterException(expectedMessage));
    try {
        job.execute(ctx);
        fail("Expected exception to be thrown");
    } catch (JobExecutionException e) {
    // Expected
    }
    verify(ctx).setResult(eq(expectedMessage));
}
Also used : ImporterException(org.candlepin.sync.ImporterException) ConflictOverrides(org.candlepin.sync.ConflictOverrides) JobDetail(org.quartz.JobDetail) JobExecutionException(org.quartz.JobExecutionException) Test(org.junit.Test)

Aggregations

ConflictOverrides (org.candlepin.sync.ConflictOverrides)4 ImporterException (org.candlepin.sync.ImporterException)4 IseException (org.candlepin.common.exceptions.IseException)3 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2 Owner (org.candlepin.model.Owner)2 SyncDataFormatException (org.candlepin.sync.SyncDataFormatException)2 Test (org.junit.Test)2 JobExecutionException (org.quartz.JobExecutionException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 EventSink (org.candlepin.audit.EventSink)1 CandlepinException (org.candlepin.common.exceptions.CandlepinException)1 NotFoundException (org.candlepin.common.exceptions.NotFoundException)1