diff options
author | 2018-06-18 10:42:37 +0300 | |
---|---|---|
committer | 2018-06-18 15:44:32 +0300 | |
commit | 8f07b3287efedba992f037d499c7f55f71f823eb (patch) | |
tree | b02d452d224f9836c1ecc945409535e75835ce54 /workflow-designer-be/src/main/java/org | |
parent | 16c48c29687bef763299321057767ff1fdcb0d4d (diff) |
add unit tests to UniqueValueService
Change-Id: Ia4f2307971feb629220fdef66214d6be99c62750
Issue-ID: SDC-1396
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/main/java/org')
6 files changed, 53 insertions, 135 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/UniqueValueService.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/UniqueValueService.java index e6e88ed9..9a8eee2f 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/UniqueValueService.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/UniqueValueService.java @@ -1,6 +1,8 @@ package org.onap.sdc.workflow.services; +import com.google.common.annotations.VisibleForTesting; import java.util.Optional; +import org.apache.commons.lang.ArrayUtils; import org.onap.sdc.workflow.persistence.UniqueValueRepository; import org.onap.sdc.workflow.persistence.types.UniqueValueEntity; import org.onap.sdc.workflow.services.errors.UniqueValueViolationException; @@ -26,7 +28,7 @@ public class UniqueValueService { * @param type the type * @param uniqueCombination the unique combination */ - public void createUniqueValue(String type, String... uniqueCombination) { + public void createUniqueValue(String type, String[] uniqueCombination) { formatValue(uniqueCombination).ifPresent(formattedValue -> { validateUniqueValue(type, formattedValue, uniqueCombination); uniqueValueRepository.insert(new UniqueValueEntity(type, formattedValue)); @@ -39,7 +41,7 @@ public class UniqueValueService { * @param type the type * @param uniqueCombination the unique combination */ - public void deleteUniqueValue(String type, String... uniqueCombination) { + public void deleteUniqueValue(String type, String[] uniqueCombination) { formatValue(uniqueCombination) .ifPresent(formattedValue -> uniqueValueRepository.delete(new UniqueValueEntity(type, formattedValue))); @@ -53,7 +55,7 @@ public class UniqueValueService { * @param newValue the new value * @param uniqueContext the unique context */ - public void updateUniqueValue(String type, String oldValue, String newValue, String... uniqueContext) { + public void updateUniqueValue(String type, String oldValue, String newValue, String ... uniqueContext) { if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) { createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {newValue})); deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {oldValue})); @@ -66,7 +68,7 @@ public class UniqueValueService { * @param type the type * @param uniqueCombination the unique combination */ - public void validateUniqueValue(String type, String... uniqueCombination) { + public void validateUniqueValue(String type, String[] uniqueCombination) { formatValue(uniqueCombination) .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination)); } @@ -76,12 +78,12 @@ public class UniqueValueService { * * @return true if the unique value is occupied, false otherwise */ - public boolean isUniqueValueOccupied(String type, String... uniqueCombination) { + public boolean isUniqueValueOccupied(String type, String[] uniqueCombination) { return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue)) .orElse(false); } - private void validateUniqueValue(String type, String formattedValue, String... uniqueCombination) { + private void validateUniqueValue(String type, String formattedValue, String[] uniqueCombination) { if (isUniqueValueOccupied(type, formattedValue)) { throw new UniqueValueViolationException(type, getValueWithoutContext(uniqueCombination)); } @@ -91,9 +93,8 @@ public class UniqueValueService { return uniqueValueRepository.findById(new UniqueValueEntity(type, formattedValue)).isPresent(); } - private static Optional<String> formatValue(String[] uniqueCombination) { - if (uniqueCombination == null || uniqueCombination.length == 0 - || getValueWithoutContext(uniqueCombination) == null) { + private Optional<String> formatValue(String[] uniqueCombination) { + if (ArrayUtils.isEmpty(uniqueCombination) || getValueWithoutContext(uniqueCombination) == null) { return Optional.empty(); } @@ -101,7 +102,7 @@ public class UniqueValueService { return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR)); } - private static String getValueWithoutContext(String... uniqueCombination) { + private String getValueWithoutContext(String[] uniqueCombination) { return uniqueCombination[uniqueCombination.length - 1]; } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java index 40750f10..2afaaa0d 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java @@ -2,12 +2,11 @@ package org.onap.sdc.workflow.services.impl; import java.util.Collection; import java.util.stream.Collectors; +import org.onap.sdc.workflow.services.mappers.WorkflowMapper; import org.onap.sdc.workflow.persistence.types.Workflow; import org.onap.sdc.workflow.services.UniqueValueService; import org.onap.sdc.workflow.services.WorkflowManager; import org.onap.sdc.workflow.services.errors.WorkflowNotFoundException; -import org.onap.sdc.workflow.services.impl.mappers.ItemToWorkflowMapper; -import org.onap.sdc.workflow.services.impl.mappers.WorkflowToItemMapper; import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdc.versioning.types.ItemStatus; @@ -22,39 +21,39 @@ public class WorkflowManagerImpl implements WorkflowManager { private static final String WORKFLOW_NAME_UNIQUE_TYPE = "WORKFLOW_NAME"; private final ItemManager itemManager; private final UniqueValueService uniqueValueService; + private WorkflowMapper workflowMapper; @Autowired public WorkflowManagerImpl(ItemManager itemManager, - @Qualifier("uniqueValueService") UniqueValueService uniqueValueService) { + @Qualifier("uniqueValueService") UniqueValueService uniqueValueService, WorkflowMapper workflowMapper) { this.itemManager = itemManager; this.uniqueValueService = uniqueValueService; + this.workflowMapper = workflowMapper; } @Override public Collection<Workflow> list() { - ItemToWorkflowMapper mapper = new ItemToWorkflowMapper(); return itemManager.list(item -> WORKFLOW_TYPE.equals(item.getType())).stream() - .map(item -> mapper.applyMapping(item, Workflow.class)).collect(Collectors.toList()); + .map(item -> workflowMapper.itemToWorkflow(item)).collect(Collectors.toList()); } @Override public Workflow get(Workflow workflow) { - ItemToWorkflowMapper mapper = new ItemToWorkflowMapper(); Item retrievedItem = itemManager.get(workflow.getId()); if (retrievedItem == null) { throw new WorkflowNotFoundException(workflow.getId()); } - return mapper.applyMapping(retrievedItem, Workflow.class); + return this.workflowMapper.itemToWorkflow(retrievedItem); } @Override public void create(Workflow workflow) { - Item item = new WorkflowToItemMapper().applyMapping(workflow, Item.class); + Item item = workflowMapper.workflowToItem(workflow); item.setStatus(ItemStatus.ACTIVE); - uniqueValueService.validateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, workflow.getName()); + uniqueValueService.validateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, new String[]{workflow.getName()}); workflow.setId(itemManager.create(item).getId()); - uniqueValueService.createUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, workflow.getName()); + uniqueValueService.createUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, new String[]{workflow.getName()}); } @Override @@ -66,7 +65,7 @@ public class WorkflowManagerImpl implements WorkflowManager { uniqueValueService.updateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, retrievedItem.getName(), workflow.getName()); - Item item = new WorkflowToItemMapper().applyMapping(workflow, Item.class); + Item item = workflowMapper.workflowToItem(workflow); item.setId(workflow.getId()); item.setStatus(retrievedItem.getStatus()); item.setVersionStatusCounters(retrievedItem.getVersionStatusCounters()); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/ItemToWorkflowMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/ItemToWorkflowMapper.java deleted file mode 100644 index 09e3cbd6..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/ItemToWorkflowMapper.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.onap.sdc.workflow.services.impl.mappers; - -import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.persistence.types.WorkflowProperty; -import org.openecomp.sdc.versioning.types.Item; - -public class ItemToWorkflowMapper extends Mapper<Item, Workflow> { - - @Override - public void map(Item source, Workflow target) { - target.setId(source.getId()); - target.setName(source.getName()); - target.setDescription(source.getDescription()); - target.setCategory((String) source.getProperties().get(WorkflowProperty.CATEGORY)); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/Mapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/Mapper.java deleted file mode 100644 index a66f8608..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/Mapper.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.onap.sdc.workflow.services.impl.mappers; - -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCode; - -/** - * Base class for all mapping classes. Mapping classes will perform data mapping from source object - * to target object Base class provides following<br> <ol> <li>provides life cycle of - * mapping class , first mapSimpleProperties is called and then mapComplexProperties is - * called.</li> <li>methods mapSimpleProperties and mapComplexProperties with default - * implementation, these should be overridden by concrete mapping classes for writing mapping - * logic.</li> </ol> - */ - -public abstract class Mapper<S, T> { - - /** - * Method is called for starting mapping from source object to target object method sets context - * in the thread locale and than calls mapSimpleProperties and mapComplexProperties - * respectively. - * - * @param source : source object for mapping - * @param clazz : target <code>Class</code> for mapping - * @return <code>T</code> - instance of type <code>T</code> - */ - - public final T applyMapping(final S source, Class<T> clazz) { - T target = (T) instantiateTarget(clazz); - if (source != null && target != null) { - preMapping(source, target); - map(source, target); - postMapping(source, target); - - } - return target; - - } - - /** - * This method is called before the <code>map</code> method. - */ - protected void preMapping(final S source, T target) { - // extension point - } - - /** - * The actual method that does the mapping between the <code>source</code> to <code>target</code> - * objects. This method is being called automatically as part of the mapper class. This - * method must be override (it is abstract) by the mapper class. - * - * @param source - the source object. - * @param target - the target object. - */ - - public abstract void map(final S source, T target); - - /** - * This method is called after the <code>map</code> method. - */ - protected void postMapping(final S source, T target) { - // extension point - } - - /** - * Creates the instance of the input class. - * - * @return <code>Object</code> - */ - - private Object instantiateTarget(final Class<?> clazz) { - - try { - return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException exception) { - throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(exception.getMessage()).build(), - exception); - } - } -} - diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowToItemMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowToItemMapper.java deleted file mode 100644 index 0b569f09..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowToItemMapper.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.onap.sdc.workflow.services.impl.mappers; - -import static org.onap.sdc.workflow.services.impl.WorkflowManagerImpl.WORKFLOW_TYPE; - -import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.persistence.types.WorkflowProperty; -import org.openecomp.sdc.versioning.types.Item; - -public class WorkflowToItemMapper extends Mapper<Workflow, Item> { - - @Override - public void map(Workflow source, Item target) { - target.setType(WORKFLOW_TYPE); - target.setName(source.getName()); - target.setDescription(source.getDescription()); - target.addProperty(WorkflowProperty.CATEGORY, source.getCategory()); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/WorkflowMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/WorkflowMapper.java new file mode 100644 index 00000000..c603908b --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/WorkflowMapper.java @@ -0,0 +1,32 @@ +package org.onap.sdc.workflow.services.mappers; + +import java.util.Collections; +import java.util.Map; +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; +import org.onap.sdc.workflow.persistence.types.Workflow; +import org.onap.sdc.workflow.persistence.types.WorkflowProperty; +import org.openecomp.sdc.versioning.types.Item; + +@Mapper(componentModel = "spring") +public interface WorkflowMapper { + + @Mapping(source = "properties", target = "category", qualifiedByName = "propertiesToCategoryMapper") + Workflow itemToWorkflow(Item item); + + @Mapping(source = "category", target = "properties", qualifiedByName = "categoryToPropertiesMapper") + @InheritInverseConfiguration + Item workflowToItem(Workflow workflow); + + @Named("propertiesToCategoryMapper") + default String customPropertiesToCategoryMapper(Map<String, Object> properties) { + return String.class.cast(properties.get(WorkflowProperty.CATEGORY)); + } + + @Named("categoryToPropertiesMapper") + default Map<String, Object> customCategoryToPropertiesMapper(String category) { + return Collections.singletonMap(WorkflowProperty.CATEGORY, category); + } +} |