@Test public void shouldReceiveAttributesUsingExplicitConversion(boolean flag) throws JMSException { // Given String pattern = "dd.MM.YYYY"; LocalDate today = new LocalDate(); Mapping mapping = new MappingBuilder(OPERATION_FIELD_NAME) // .mapField("date", FieldMapping.map("date", new JodaLocalDateConverter(pattern))) // .mapField("flag", FieldMapping.map("flag", new StringToBooleanConverter("1", "0"))) // .build(); MapMessage message = createPayload( OPERATION_FIELD_NAME, "localDateCall", "date", today.toString(pattern), "flag", flag ? "1" : "0"); // When JodaTimeApi mock = mock(JodaTimeApi.class); MapMessageDecoder.of(JodaTimeApi.class, mock, mapping).onMessage(message); // Then verify(mock).localDateCall(today, flag); }
public static void updateFormFromModel( final Object model, ModelBindingMetaData modelBindingMetaData) { try { modelBindingMetaData.setFormIsBeingUpdatedFromModelRightNow(true); for (Map.Entry<Field, FieldMapping> binding : modelBindingMetaData.getFieldMapping().entrySet()) { final FieldMapping fieldMapping = binding.getValue(); final Object component = fieldMapping.getComponent(); allowOperationOnField( binding.getKey(), new ObjectUtil.OperationOnField() { @Override public void operate(Field field) throws ReflectiveOperationException { Object modelValue = field.get(model); Method setterMethod = fieldMapping.getSetterMethod(); if (setterMethod == null) return; Object currentValue = fieldMapping.getGetterMethod().invoke(component); if (modelValue != null && modelValue.getClass().isArray()) { if (Arrays.equals((Object[]) modelValue, (Object[]) currentValue)) return; } else if (modelValue == null && currentValue == null) { return; } else if (modelValue != null && modelValue.equals(currentValue)) { return; } if (fieldMapping.getBindingType().equals(FieldMapping.BindingType.BY_REFERENCE)) setterMethod.invoke(component, modelValue); else setterMethod.invoke(component, modelValue == null ? null : modelValue.toString()); } }); } } finally { modelBindingMetaData.setFormIsBeingUpdatedFromModelRightNow(false); } }
public FieldMapping getFieldMapping(String fieldName) { for (FieldMapping fm : fieldMappings) { if (fm.getFieldName().equals(fieldName)) { return fm; } } return null; }
/** * Get the column families required by this schema. * * @return The set of column families. */ public Set<String> getRequiredColumnFamilies() { Set<String> set = new HashSet<String>(); for (FieldMapping mapping : fieldMappings) { if (FieldMapping.MappingType.KEY != mapping.getMappingType()) set.add(mapping.getFamilyAsString()); } return set; }
/** * Validate that a {@link FieldMapping} is compatible with this builder's current set of * mappings and add it to the set of mappings. * * <p>A mapping is not compatible if it results in: * * <pre> * 1. Multiple occVersion mappings in the mapping set * 2. Both a counter and an occVersion mapping in the mapping set * </pre> * * @param fm a {@code FieldMapping} to add to this builder */ private void addField(FieldMapping fm) { // validate! if (fm.getMappingType() == FieldMapping.MappingType.OCC_VERSION) { ValidationException.check(!hasOCCVersion, "Cannot use multiple occVersion fields"); ValidationException.check(!hasCounter, "Cannot use both counter and occVersion fields"); hasOCCVersion = true; } else if (fm.getMappingType() == FieldMapping.MappingType.COUNTER) { ValidationException.check(!hasOCCVersion, "Cannot use both counter and occVersion fields"); hasCounter = true; } fieldMappings.add(fm); }
private Mapping partialMapping() { Mapping mapping = mock(Mapping.class); given(mapping.getOperationMessageAttibute()).willReturn(OPERATION_FIELD_NAME); given(mapping.getOperationForMethod("mappedCall")).willReturn("OP"); given(mapping.getMethodForOperation("OP")).willReturn("mappedCall"); givenFieldMapping(mapping, "s1", FieldMapping.map("a")); return new CheckedMapping(mapping); }
@Test public void shouldCallServiceWhenSendingAsMapMessage() { // Given MappedApi sendProxy = MessageSender.of(MappedApi.class); Mapping receiveMapping = new MappingBuilder(OPERATION_FIELD_NAME) // .mapField("s1", FieldMapping.map("A")) // .mapField("s2", FieldMapping.map("B")) // .build(); // When sendProxy.mappedCall("a", 0L); receive(captureMessage(), receiveMapping); // Then verify(serviceMock).mappedCall("a", 0L); }
private SavedMappings processISAFields(IsaFieldMappingsDocument isaDocument) { SavedMappings mappings = new SavedMappings(); IsaFieldMappings isaFields = isaDocument.getIsaFieldMappings(); for (IsaField field : isaFields.getIsaFieldArray()) { MappingField isaFieldBeingMappedTo = new MappingField(field.getColumnName()); ISAFieldMapping currentMapping = new ISAFieldMapping(); for (FieldMapping fieldMapping : field.getFieldMappingArray()) { processFieldMappings( fieldMapping.getMappedFieldArray(), ISAFieldMapping.FIELD, currentMapping); } if (field.sizeOfDateMappingArray() > 0) { for (DateMapping dateMapping : field.getDateMappingArray()) { processFieldMappings( dateMapping.getMappedFieldArray(), ISAFieldMapping.DATE, currentMapping); } } if (field.sizeOfProviderMappingArray() > 0) { for (ProviderMapping providerMapping : field.getProviderMappingArray()) { processFieldMappings( providerMapping.getMappedFieldArray(), ISAFieldMapping.PERFORMER, currentMapping); } } if (field.sizeOfUnitMappingArray() > 0) { for (UnitMapping unitMapping : field.getUnitMappingArray()) { processFieldMappings( unitMapping.getMappedFieldArray(), ISAFieldMapping.UNIT, currentMapping); } } mappings.addMapping(isaFieldBeingMappedTo, currentMapping); } return mappings; }
@Test public void shouldSendAttributesUsingExplicitConversion(boolean flag) throws JMSException { // Given LocalDate today = new LocalDate(); String pattern = "dd.MM.YYYY"; Mapping mapping = new MappingBuilder(OPERATION_FIELD_NAME) // .mapField("date", FieldMapping.map("date", new JodaLocalDateConverter(pattern))) // .mapField("flag", FieldMapping.map("flag", new StringToBooleanConverter("1", "0"))) // .build(); MapJmsPayloadHandler payloadHandler = new MapJmsPayloadHandler(mapping); JodaTimeApi service = JmsSenderFactory.create(CONFIG, payloadHandler).create(JodaTimeApi.class); // When service.localDateCall(today, flag); // Then MapMessage message = (MapMessage) captureMessage(); assertEquals(today.toString(pattern), message.getString("date")); assertEquals(flag ? "1" : "0", message.getString("flag")); }
@Test public void shouldCallServiceWithDefaultValueIfNotSendingMandatoryParameter() { // Given Mapping mapping = new MappingBuilder(OPERATION_FIELD_NAME) .mapField("s1", FieldMapping.mapWithDefault("s1", "default value")) .build(); MappedApi service = service(mapping); // When service.mappedCall(null, 1L); receive(captureMessage(), mapping); // Then verify(serviceMock).mappedCall("default value", 1L); }
/** * Get the columns required by this schema. * * @return The set of columns */ public Set<String> getRequiredColumns() { Set<String> set = new HashSet<String>(); for (FieldMapping fieldMapping : fieldMappings) { if (FieldMapping.MappingType.KEY == fieldMapping.getMappingType()) { continue; } else if (FieldMapping.MappingType.KEY_AS_COLUMN == fieldMapping.getMappingType()) { set.add(fieldMapping.getFamilyAsString() + ":"); } else { set.add(fieldMapping.getFamilyAsString() + ":" + fieldMapping.getQualifierAsString()); } } return set; }
private Converter<?> resolveConverter(final FieldMapping field) { Converter<?> converter; switch (field.getField()) { case AMOUNT: converter = mapping.getNumberConverter(); break; case NEGATE_AMOUNT: converter = mapping.getNegateAmountConverter(); break; case DATE: converter = mapping.getDateConverter(); break; case IGNORED: converter = null; break; default: converter = DEFAULT_CONVERTER; break; } return converter; }
@Test @Ignore // TODO reenable public void shouldMapReceiveBoxedPrimitiveAttributesIfWrittenAsStrings( /* @Values("BOXED_PRIMITIVE_CALLS") */ SimpleServiceCall call) throws Exception { // Given MappingBuilder mapping = new MappingBuilder(OPERATION_FIELD_NAME); String operation = call.operation; String argument = call.argument; Class<?> type = call.type; Object value = call.value; mapping.mapField(argument, FieldMapping.map(argument)); MapMessage message = createPayload(OPERATION_FIELD_NAME, operation, argument, String.valueOf(value)); // When MapMessageDecoder.of(BoxedPrimitivesTestApi.class, boxedPrimitivesServiceMock, mapping.build()) .onMessage(message); // Then invoke(verify(boxedPrimitivesServiceMock), operation, type, value); }
@Override public List<TransactionImportDTO> readTransactions(final Reader in) throws IllegalTransactionFileFormatException, IOException { int lineNumber = 0; // Ignore the header lines for (int i = 0; i < mapping.getHeaderLines(); i++) { ++lineNumber; readLine(in); } // Read each transaction final List<TransactionImportDTO> transactions = new LinkedList<TransactionImportDTO>(); List<String> line; while ((line = readLine(in)) != null) { ++lineNumber; if (line.isEmpty()) { continue; } final TransactionImportDTO transaction = new TransactionImportDTO(); int column = -1; final StringBuilder comments = new StringBuilder(); for (final FieldMapping field : mapping.getFields()) { ++column; final Converter<?> converter = resolveConverter(field); if (converter == null) { // An ignored field continue; } final String stringValue = StringUtils.trimToNull(line.get(column)); try { Object value = converter.valueOf(stringValue); final String property = field.getField().getDtoProperty(); // If this is a member_custom_field, the value would be a map if (field.getField().equals(FieldMapping.Field.MEMBER_CUSTOM_FIELD)) { final Map<String, String> map = new HashMap<String, String>(); map.put(field.getMemberField().getInternalName(), (String) value); value = map; } PropertyHelper.set(transaction, property, value); } catch (final Exception e) { if (comments.length() > 0) { comments.append("\n"); } comments.append( messageResolver.message( "externalTransferImport.error.importing.comments", field.getName(), column + 1, stringValue)); } } transaction.setLineNumber(lineNumber); transaction.setComments(StringUtils.trimToNull(comments.toString())); transactions.add(transaction); } return transactions; }
/** * Adds a mapping to store the record field {@code name} in the record key. * * <p>The underlying dataset's {@link PartitionStrategy} must have an identity partitioner for * the record field {@code name} so that the value can be recovered. * * @param name The name of a record field * @return This Builder for method chaining */ public Builder key(String name) { addField(FieldMapping.key(name)); return this; }
/** * Adds a key-as-column mapping to store the record field {@code name} in the given {@code * family}, using its keys or fields as column qualifiers. * * <p>The record field must be a map or a record. * * @param name The name of a record field * @param family The column family for storing the map or record values * @return This Builder for method chaining */ public Builder keyAsColumn(String name, String family) { addField(FieldMapping.keyAsColumn(name, family)); return this; }
public void order(Select sel, ClassMapping elem, Joins joins) { if (elem != null) sel.orderBy(elem.getPrimaryKeyColumns(), _asc, joins, false); else sel.orderBy(_fm.getElementMapping().getColumns(), _asc, joins, false); }
public boolean isInRelation() { return _fm.getElement().getTypeMetaData() != null; }
/** * Adds a key-as-column mapping to store the record field {@code name} in the given {@code * family}, using column qualifiers built from its keys or field names appended to the {@code * qualifierPrefix}. * * <p>The record field must be a map or a record. * * @param name The name of a record field * @param family The column family for storing the map or record values * @param qualifierPrefix A prefix to add when building column qualifiers * @return This Builder for method chaining */ public Builder keyAsColumn(String name, String family, String qualifierPrefix) { addField(FieldMapping.keyAsColumn(name, family, qualifierPrefix)); return this; }
/** * Adds a counter mapping to store record field {@code name} in a column using the {@code * family} and {@code qualifier}. The record field can be updated atomically using {@link * RandomAccessDataset#increment(Key, String, long)} * * <p>The record field must be an int or a long. * * <p>Counters cannot be used in combination with optimistic concurrency (OCC). * * @param name The name of a record field * @param family The column family for storing the record counter value * @param qualifier The column qualifier for storing the record field value * @return This Builder for method chaining */ public Builder counter(String name, String family, String qualifier) { addField(FieldMapping.counter(name, family, qualifier)); return this; }
/** * Adds an OCC version mapping for the record field {@code name}. The record field will contain * the current version number of the record. * * <p>Using this mapping enables optimistic concurrency (OCC), where the underlying {@link * RandomAccessDataset} will only persist changes to a record if it has not changed by another * process. * * <p>The record field must be an int or a long. * * <p>Optimistic concurrency (OCC) cannot be used in combination with counters. * * @param name The name of a record field to use for an OCC version * @return This Builder for method chaining */ public Builder occ(String name) { addField(FieldMapping.occ(name)); return this; }