ConsumerImporter { 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.")); } Owner alreadyUsing = curator.getByUpstreamUuid(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()); 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()); } } 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); } 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); } ConsumerImporter(OwnerCurator curator, IdentityCertificateCurator idCertCurator, I18n i18n,
        CertificateSerialCurator serialCurator); ConsumerDTO createObject(ObjectMapper mapper, Reader reader); void store(Owner owner, ConsumerDTO consumer, ConflictOverrides forcedConflicts,
        IdentityCertificate idcert);  }
@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); ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class); verify(owner).setUpstreamConsumer(arg.capture()); assertEquals("test-uuid", arg.getValue().getUuid()); verify(curator).merge(owner); }
@Test public void importConsumerWithSameUuidOnOwnerShouldDoNothing() 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); IdentityCertificate idCert = new IdentityCertificate(); idCert.setSerial(new CertificateSerial()); importer.store(owner, consumer, new ConflictOverrides(), idCert); ArgumentCaptor<Owner> arg = ArgumentCaptor.forClass(Owner.class); verify(curator).merge(arg.capture()); assertEquals("test-uuid", arg.getValue().getUpstreamUuid()); }
@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.getByUpstreamUuid(consumer.getUuid())).thenReturn(anotherOwner); importer.store(owner, consumer, new ConflictOverrides(), null); }
@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)); } }
@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); ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class); verify(owner).setUpstreamConsumer(arg.capture()); assertEquals("test-uuid", arg.getValue().getUuid()); verify(curator).merge(owner); }
@Test(expected = ImporterException.class) public void importConsumerWithNullUuidOnConsumerShouldThrowException() throws ImporterException { Owner owner = new Owner(); ConsumerDTO consumer = new ConsumerDTO(); consumer.setUuid(null); importer.store(owner, consumer, new ConflictOverrides(), null); }
@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); ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class); verify(owner).setUpstreamConsumer(arg.capture()); assertEquals("test-uuid", arg.getValue().getUuid()); verify(curator).merge(owner); }