use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testUniqueValidation.
@Test
public void testUniqueValidation() {
// prepare form definition a test saving form values
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
attribute.setCode(attributeName);
attribute.setName(attributeName);
attribute.setPersistentType(PersistentType.TEXT);
attribute.setUnique(Boolean.TRUE);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
IdmIdentityDto owner = getHelper().createIdentity((GuardedString) null);
IdmFormValueDto value = new IdmFormValueDto(attribute);
value.setValue("one");
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto(owner, formDefinitionOne, Lists.newArrayList(value));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
IdmFormInstanceDto savedFormInstance = formService.saveFormInstance(owner, formDefinitionOne, Lists.newArrayList(value));
IdmFormValueDto savedValue = savedFormInstance.toValueMap().get(attributeName).get(0);
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getUniqueValue().equals("one")));
//
value.setId(savedValue.getId());
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
value.setId(UUID.randomUUID());
validationErrors = formService.validate(formInstance);
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getUniqueValue().equals("one")));
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testMinMaxDateValidation.
@Test
public void testMinMaxDateValidation() {
ZonedDateTime now = ZonedDateTime.now();
// prepare form definition a test saving form values
IdmFormAttributeDto attributeDate = new IdmFormAttributeDto();
String attributeDateCode = getHelper().createName();
attributeDate.setCode(attributeDateCode);
attributeDate.setName(attributeDateCode);
attributeDate.setPersistentType(PersistentType.DATE);
attributeDate.setMin(new BigDecimal("3"));
attributeDate.setMax(new BigDecimal("5"));
IdmFormAttributeDto attributeDateTime = new IdmFormAttributeDto();
String attributeDateTimeCode = getHelper().createName();
attributeDateTime.setCode(attributeDateTimeCode);
attributeDateTime.setName(attributeDateTimeCode);
attributeDateTime.setPersistentType(PersistentType.DATETIME);
attributeDateTime.setMin(new BigDecimal("6"));
attributeDateTime.setMax(new BigDecimal("8"));
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attributeDate, attributeDateTime));
attributeDate = formDefinitionOne.getMappedAttributeByCode(attributeDate.getCode());
attributeDateTime = formDefinitionOne.getMappedAttributeByCode(attributeDateTime.getCode());
//
IdmFormValueDto valueDate = new IdmFormValueDto(attributeDate);
IdmFormValueDto valueDateTime = new IdmFormValueDto(attributeDateTime);
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setFormDefinition(formDefinitionOne);
formInstance.setValues(Lists.newArrayList(valueDate, valueDateTime));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
//
valueDate.setDateValue(now.plusDays(2));
valueDateTime.setDateValue(now.plusDays(5));
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(2, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getMinValue() != null));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMinValue().equals(new BigDecimal("3")) && e.getAttributeCode().equals(attributeDateCode)));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMinValue().equals(new BigDecimal("6")) && e.getAttributeCode().equals(attributeDateTimeCode)));
//
valueDate.setDateValue(now.plusDays(6));
valueDateTime.setDateValue(now.plusDays(9));
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(2, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getMaxValue() != null));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMaxValue().equals(new BigDecimal("5")) && e.getAttributeCode().equals(attributeDateCode)));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMaxValue().equals(new BigDecimal("8")) && e.getAttributeCode().equals(attributeDateTimeCode)));
//
valueDate.setDateValue(now.plusDays(5));
valueDateTime.setDateValue(now.plusDays(8));
//
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
valueDate.setDateValue(now.plusDays(3));
valueDateTime.setDateValue(now.plusDays(6).plusMinutes(2));
//
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
}
use of org.junit.Assert.assertTrue in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testRequiredValidation.
@Test
public void testRequiredValidation() {
// prepare form definition a test saving form values
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
attribute.setCode(attributeName);
attribute.setName(attributeName);
attribute.setPersistentType(PersistentType.SHORTTEXT);
attribute.setRequired(true);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
IdmFormValueDto valueOne = new IdmFormValueDto(attribute);
IdmFormValueDto valueTwo = new IdmFormValueDto(attribute);
valueTwo.setShortTextValue("value");
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setFormDefinition(formDefinitionOne);
formInstance.setValues(Lists.newArrayList(valueOne));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
//
valueOne.setShortTextValue("value");
validationErrors = formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
//
formInstance.setValues(Lists.newArrayList());
//
validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
//
formInstance.setValues(Lists.newArrayList(new IdmFormValueDto(attribute)));
//
validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
}
use of org.junit.Assert.assertTrue in project pravega by pravega.
the class StreamTransactionMetadataTasksTest method failOverTests.
@Test(timeout = 60000)
public void failOverTests() throws Exception {
// Create mock writer objects.
EventStreamWriterMock<CommitEvent> commitWriter = new EventStreamWriterMock<>();
EventStreamWriterMock<AbortEvent> abortWriter = new EventStreamWriterMock<>();
EventStreamReader<CommitEvent> commitReader = commitWriter.getReader();
EventStreamReader<AbortEvent> abortReader = abortWriter.getReader();
txnTasks = new StreamTransactionMetadataTasks(streamStore, segmentHelperMock, executor, "host", GrpcAuthHelper.getDisabledAuthHelper());
txnTasks.initializeStreamWriters(commitWriter, abortWriter);
consumer = new ControllerService(kvtStore, kvtMetadataTasks, streamStore, bucketStore, streamMetadataTasks, txnTasks, segmentHelperMock, executor, null, requestTracker);
// Create test scope and stream.
final ScalingPolicy policy1 = ScalingPolicy.fixed(2);
final StreamConfiguration configuration1 = StreamConfiguration.builder().scalingPolicy(policy1).build();
Assert.assertEquals(Controller.CreateScopeStatus.Status.SUCCESS, consumer.createScope(SCOPE, 0L).join().getStatus());
Assert.assertEquals(Controller.CreateStreamStatus.Status.SUCCESS, streamMetadataTasks.createStream(SCOPE, STREAM, configuration1, System.currentTimeMillis(), 0L).join());
// Set up txn task for creating transactions from a failedHost.
@Cleanup StreamTransactionMetadataTasks failedTxnTasks = new StreamTransactionMetadataTasks(streamStore, segmentHelperMock, executor, "failedHost", GrpcAuthHelper.getDisabledAuthHelper());
failedTxnTasks.initializeStreamWriters(new EventStreamWriterMock<>(), new EventStreamWriterMock<>());
// Create 3 transactions from failedHost.
VersionedTransactionData tx1 = failedTxnTasks.createTxn(SCOPE, STREAM, 10000, 0L, 0L).join().getKey();
VersionedTransactionData tx2 = failedTxnTasks.createTxn(SCOPE, STREAM, 10000, 0L, 0L).join().getKey();
VersionedTransactionData tx3 = failedTxnTasks.createTxn(SCOPE, STREAM, 10000, 0L, 0L).join().getKey();
VersionedTransactionData tx4 = failedTxnTasks.createTxn(SCOPE, STREAM, 10000, 0L, 0L).join().getKey();
// Ping another txn from failedHost.
PingTxnStatus pingStatus = failedTxnTasks.pingTxn(SCOPE, STREAM, tx4.getId(), 10000, 0L).join();
VersionedTransactionData tx4get = streamStore.getTransactionData(SCOPE, STREAM, tx4.getId(), null, executor).join();
// Validate versions of all txn
Assert.assertEquals(0, tx1.getVersion().asIntVersion().getIntValue());
Assert.assertEquals(0, tx2.getVersion().asIntVersion().getIntValue());
Assert.assertEquals(0, tx3.getVersion().asIntVersion().getIntValue());
Assert.assertEquals(1, tx4get.getVersion().asIntVersion().getIntValue());
Assert.assertEquals(PingTxnStatus.Status.OK, pingStatus.getStatus());
// Validate the txn index.
Assert.assertEquals(1, streamStore.listHostsOwningTxn().join().size());
// Change state of one txn to COMMITTING.
TxnStatus txnStatus2 = streamStore.sealTransaction(SCOPE, STREAM, tx2.getId(), true, Optional.empty(), "", Long.MIN_VALUE, null, executor).thenApply(AbstractMap.SimpleEntry::getKey).join();
Assert.assertEquals(TxnStatus.COMMITTING, txnStatus2);
// Change state of another txn to ABORTING.
TxnStatus txnStatus3 = streamStore.sealTransaction(SCOPE, STREAM, tx3.getId(), false, Optional.empty(), "", Long.MIN_VALUE, null, executor).thenApply(AbstractMap.SimpleEntry::getKey).join();
Assert.assertEquals(TxnStatus.ABORTING, txnStatus3);
// Create transaction tasks for sweeping txns from failedHost.
txnTasks = new StreamTransactionMetadataTasks(streamStore, segmentHelperMock, executor, "host", GrpcAuthHelper.getDisabledAuthHelper());
TxnSweeper txnSweeper = new TxnSweeper(streamStore, txnTasks, 100, executor);
// Before initializing, txnSweeper.sweepFailedHosts would throw an error
AssertExtensions.assertFutureThrows("IllegalStateException before initialization", txnSweeper.sweepFailedProcesses(() -> Collections.singleton("host")), ex -> ex instanceof IllegalStateException);
// Initialize stream writers.
txnTasks.initializeStreamWriters(commitWriter, abortWriter);
// Validate that txnTasks is ready.
assertTrue(txnTasks.isReady());
// Sweep txns that were being managed by failedHost.
txnSweeper.sweepFailedProcesses(() -> Collections.singleton("host")).join();
// Validate that sweeping completes correctly.
Set<String> listOfHosts = streamStore.listHostsOwningTxn().join();
Assert.assertEquals(1, listOfHosts.size());
Assert.assertTrue(listOfHosts.contains("host"));
Assert.assertEquals(TxnStatus.OPEN, streamStore.transactionStatus(SCOPE, STREAM, tx1.getId(), null, executor).join());
Assert.assertEquals(TxnStatus.COMMITTING, streamStore.transactionStatus(SCOPE, STREAM, tx2.getId(), null, executor).join());
Assert.assertEquals(TxnStatus.ABORTING, streamStore.transactionStatus(SCOPE, STREAM, tx3.getId(), null, executor).join());
Assert.assertEquals(TxnStatus.OPEN, streamStore.transactionStatus(SCOPE, STREAM, tx4.getId(), null, executor).join());
VersionedTransactionData txnData = streamStore.getTransactionData(SCOPE, STREAM, tx1.getId(), null, executor).join();
Assert.assertEquals(1, txnData.getVersion().asIntVersion().getIntValue());
txnData = streamStore.getTransactionData(SCOPE, STREAM, tx4.getId(), null, executor).join();
Assert.assertEquals(2, txnData.getVersion().asIntVersion().getIntValue());
// Create commit and abort event processors.
BlockingQueue<CommitEvent> processedCommitEvents = new LinkedBlockingQueue<>();
BlockingQueue<AbortEvent> processedAbortEvents = new LinkedBlockingQueue<>();
createEventProcessor("commitRG", "commitStream", commitReader, commitWriter, () -> new ConcurrentEventProcessor<>(new CommitRequestHandler(streamStore, streamMetadataTasks, txnTasks, bucketStore, executor, processedCommitEvents), executor));
createEventProcessor("abortRG", "abortStream", abortReader, abortWriter, () -> new ConcurrentEventProcessor<>(new AbortRequestHandler(streamStore, streamMetadataTasks, executor, processedAbortEvents), executor));
// Wait until the commit event is processed and ensure that the txn state is COMMITTED.
CommitEvent commitEvent = processedCommitEvents.take();
assertEquals(tx2.getEpoch(), commitEvent.getEpoch());
assertEquals(TxnStatus.COMMITTED, streamStore.transactionStatus(SCOPE, STREAM, tx2.getId(), null, executor).join());
// Wait until 3 abort events are processed and ensure that the txn state is ABORTED.
Predicate<AbortEvent> predicate = event -> event.getTxid().equals(tx1.getId()) || event.getTxid().equals(tx3.getId()) || event.getTxid().equals(tx4.getId());
AbortEvent abortEvent1 = processedAbortEvents.take();
assertTrue(predicate.test(abortEvent1));
AbortEvent abortEvent2 = processedAbortEvents.take();
assertTrue(predicate.test(abortEvent2));
AbortEvent abortEvent3 = processedAbortEvents.take();
assertTrue(predicate.test(abortEvent3));
assertEquals(TxnStatus.ABORTED, streamStore.transactionStatus(SCOPE, STREAM, tx1.getId(), null, executor).join());
assertEquals(TxnStatus.ABORTED, streamStore.transactionStatus(SCOPE, STREAM, tx3.getId(), null, executor).join());
assertEquals(TxnStatus.ABORTED, streamStore.transactionStatus(SCOPE, STREAM, tx4.getId(), null, executor).join());
}
use of org.junit.Assert.assertTrue in project pravega by pravega.
the class StreamSegmentContainerTests method testBasicConditionalMergeScenarios.
/**
* Test in detail the basic situations that a conditional segment merge can face.
*/
@Test
public void testBasicConditionalMergeScenarios() throws Exception {
@Cleanup TestContext context = createContext();
context.container.startAsync().awaitRunning();
final String parentSegment = "parentSegment";
// This will be the attribute update to execute against the parent segment.
Function<String, AttributeUpdateCollection> attributeUpdateForTxn = txnName -> AttributeUpdateCollection.from(new AttributeUpdate(AttributeId.fromUUID(UUID.nameUUIDFromBytes(txnName.getBytes())), AttributeUpdateType.ReplaceIfEquals, txnName.hashCode() + 1, txnName.hashCode()));
Function<String, Long> getAttributeValue = txnName -> {
AttributeId attributeId = AttributeId.fromUUID(UUID.nameUUIDFromBytes(txnName.getBytes()));
return context.container.getAttributes(parentSegment, Collections.singletonList(attributeId), true, TIMEOUT).join().get(attributeId);
};
// Create a parent Segment.
context.container.createStreamSegment(parentSegment, getSegmentType(parentSegment), null, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
SegmentType segmentType = getSegmentType(parentSegment);
// Case 1: Create and empty transaction that fails to merge conditionally due to bad attributes.
String txnName = NameUtils.getTransactionNameFromId(parentSegment, UUID.randomUUID());
AttributeId txnAttributeId = AttributeId.fromUUID(UUID.nameUUIDFromBytes(txnName.getBytes()));
context.container.createStreamSegment(txnName, segmentType, null, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
AttributeUpdateCollection attributeUpdates = attributeUpdateForTxn.apply(txnName);
AssertExtensions.assertFutureThrows("Transaction was expected to fail on attribute update", context.container.mergeStreamSegment(parentSegment, txnName, attributeUpdates, TIMEOUT), ex -> ex instanceof BadAttributeUpdateException);
Assert.assertEquals(Attributes.NULL_ATTRIBUTE_VALUE, (long) getAttributeValue.apply(txnName));
// Case 2: Now, we prepare the attributes in the parent segment so the merge of the empty transaction succeeds.
context.container.updateAttributes(parentSegment, AttributeUpdateCollection.from(new AttributeUpdate(txnAttributeId, AttributeUpdateType.Replace, txnName.hashCode())), TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// As the source segment is empty, the amount of merged data should be 0.
Assert.assertEquals(0L, context.container.mergeStreamSegment(parentSegment, txnName, attributeUpdates, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).getMergedDataLength());
// But the attribute related to that transaction merge on the parent segment should have been updated.
Assert.assertEquals(txnName.hashCode() + 1L, (long) context.container.getAttributes(parentSegment, Collections.singletonList(txnAttributeId), true, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).get(txnAttributeId));
// Case 3: Create a non-empty transaction that should fail due to a conditional attribute update failure.
txnName = NameUtils.getTransactionNameFromId(parentSegment, UUID.randomUUID());
txnAttributeId = AttributeId.fromUUID(UUID.nameUUIDFromBytes(txnName.getBytes()));
attributeUpdates = attributeUpdateForTxn.apply(txnName);
context.container.createStreamSegment(txnName, segmentType, null, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// Add some appends to the transaction.
RefCountByteArraySegment appendData = getAppendData(txnName, 1);
context.container.append(txnName, appendData, null, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// Attempt the conditional merge.
AssertExtensions.assertFutureThrows("Transaction was expected to fail on attribute update", context.container.mergeStreamSegment(parentSegment, txnName, attributeUpdates, TIMEOUT), ex -> ex instanceof BadAttributeUpdateException);
Assert.assertEquals(Attributes.NULL_ATTRIBUTE_VALUE, (long) getAttributeValue.apply(txnName));
// Case 4: Now, we prepare the attributes in the parent segment so the merge of the non-empty transaction succeeds.
context.container.updateAttributes(parentSegment, AttributeUpdateCollection.from(new AttributeUpdate(txnAttributeId, AttributeUpdateType.Replace, txnName.hashCode())), TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// As the source segment is non-empty, the amount of merged data should be greater than 0.
Assert.assertTrue(context.container.mergeStreamSegment(parentSegment, txnName, attributeUpdates, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).getMergedDataLength() > 0);
// The attribute related to that transaction merge on the parent segment should have been updated as well.
Assert.assertEquals(txnName.hashCode() + 1L, (long) context.container.getAttributes(parentSegment, Collections.singletonList(txnAttributeId), true, TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).get(txnAttributeId));
context.container.stopAsync().awaitTerminated();
}
Aggregations