/** * Creates and persists a new attribute value list entity from the request information. * * @param request the request * @param namespaceEntity the namespace * * @return the newly created attribute value list entity */ private AttributeValueListEntity createAttributeValueListEntity(AttributeValueListCreateRequest request, NamespaceEntity namespaceEntity) { // Create a new entity. AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(request.getAttributeValueListKey().getAttributeValueListName()); // Persist and return the newly created entity. return attributeValueListDao.saveAndRefresh(attributeValueListEntity); } }
for (AttributeValueListEntity attributeValueListEntity : entityManager.createQuery(criteria).getResultList()) attributeValueListKeys.add(new AttributeValueListKey(attributeValueListEntity.getNamespace().getCode(), attributeValueListEntity.getName()));
getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues()); attributeValueListEntity.getName())); attributeValueListEntity.getAllowedAttributeValues().remove(allowedAttributeValueEntity);
/** * Creates the attribute value list from the persisted entity. * * @param attributeValueListEntity the attribute value list entity * * @return the attribute value list */ public AttributeValueList createAttributeValueListFromEntity(AttributeValueListEntity attributeValueListEntity) { // Create the attribute value list. AttributeValueList attributeValueList = new AttributeValueList(); AttributeValueListKey attributeValueListKey = new AttributeValueListKey(); attributeValueListKey.setNamespace(attributeValueListEntity.getNamespace().getCode()); attributeValueListKey.setAttributeValueListName(attributeValueListEntity.getName()); attributeValueList.setAttributeValueListKey(attributeValueListKey); attributeValueList.setId(attributeValueListEntity.getId()); return attributeValueList; } }
AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setId(ATTRIBUTE_VALUE_LIST_ID); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(ATTRIBUTE_VALUE_LIST_NAME); attributeValueListEntity.setAllowedAttributeValues(new ArrayList<>()); attributeValueListEntity.getName()), e.getMessage());
@Test public void testGetAttributeValueListEntity() { // Create an attribute value list key. AttributeValueListKey attributeValueListKey = new AttributeValueListKey(ATTRIBUTE_VALUE_LIST_NAMESPACE, ATTRIBUTE_VALUE_LIST_NAME); // Create an attribute value list entity. AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); // Mock calls to external methods. when(attributeValueListDao.getAttributeValueListByKey(attributeValueListKey)).thenReturn(attributeValueListEntity); // Call the method under test. AttributeValueListEntity result = attributeValueListDaoHelper.getAttributeValueListEntity(attributeValueListKey); // Verify the external calls. verify(attributeValueListDao).getAttributeValueListByKey(attributeValueListKey); verifyNoMoreInteractions(attributeValueListDao); // Validate the result. assertEquals(attributeValueListEntity, result); }
/** * Gets allowed attribute values for the global attribute definition * * @param globalAttributeDefinitionKey the global attribute definition key * * @return list of allowed attribute values, if the global attribute definition does not have attribute list returns null */ public List<String> getAllowedAttributeValues(GlobalAttributeDefinitionKey globalAttributeDefinitionKey) { List<String> allowedAttributeValues = null; GlobalAttributeDefinitionEntity globalAttributeDefinitionEntity = globalAttributeDefinitionDao.getGlobalAttributeDefinitionByKey(globalAttributeDefinitionKey); if (globalAttributeDefinitionEntity.getAttributeValueList() != null) { allowedAttributeValues = new ArrayList<>(); Collection<AllowedAttributeValueEntity> list = globalAttributeDefinitionEntity.getAttributeValueList().getAllowedAttributeValues(); for (AllowedAttributeValueEntity allowedAttributeValueEntity : list) { allowedAttributeValues.add(allowedAttributeValueEntity.getAllowedAttributeValue()); } } return allowedAttributeValues; } }
AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setId(ATTRIBUTE_VALUE_LIST_ID); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(ATTRIBUTE_VALUE_LIST_NAME); attributeValueListEntity.setAllowedAttributeValues(allowedAttributeValueEntities); attributeValueListEntity.getName()), e.getMessage());
/** * Creates the attribute value list from the persisted entity. * * @param attributeValueListEntity the attribute value list entity * * @return the attribute value list */ public AttributeValueList createAttributeValueListFromEntity(AttributeValueListEntity attributeValueListEntity) { // Create the attribute value list. AttributeValueList attributeValueList = new AttributeValueList(); AttributeValueListKey attributeValueListKey = new AttributeValueListKey(); attributeValueListKey.setNamespace(attributeValueListEntity.getNamespace().getCode()); attributeValueListKey.setAttributeValueListName(attributeValueListEntity.getName()); attributeValueList.setAttributeValueListKey(attributeValueListKey); attributeValueList.setId(attributeValueListEntity.getId()); return attributeValueList; } }
@Test public void createAttributeValueListAlreadyExists() { // Create an attribute value list key. AttributeValueListKey attributeValueListKey = new AttributeValueListKey(ATTRIBUTE_VALUE_LIST_NAMESPACE, ATTRIBUTE_VALUE_LIST_NAME); // Create an attribute value list create request. AttributeValueListCreateRequest request = new AttributeValueListCreateRequest(attributeValueListKey); // Mock calls to external methods. when(namespaceDaoHelper.getNamespaceEntity(ATTRIBUTE_VALUE_LIST_NAMESPACE)).thenReturn(new NamespaceEntity()); when(attributeValueListDao.getAttributeValueListByKey(attributeValueListKey)).thenReturn(new AttributeValueListEntity()); // Try to call the method under test. try { attributeValueListService.createAttributeValueList(request); fail(); } catch (AlreadyExistsException e) { assertEquals(String .format("Unable to create attribute value list with name \"%s\" because it already exists for namespace \"%s\".", ATTRIBUTE_VALUE_LIST_NAME, ATTRIBUTE_VALUE_LIST_NAMESPACE), e.getMessage()); } // Verify the external calls. verify(attributeValueListHelper).validateAttributeValueListCreateRequest(request); verify(namespaceDaoHelper).getNamespaceEntity(ATTRIBUTE_VALUE_LIST_NAMESPACE); verify(attributeValueListDao).getAttributeValueListByKey(attributeValueListKey); verifyNoMoreInteractionsHelper(); }
/** * Gets allowed attribute values for the global attribute definition * * @param globalAttributeDefinitionKey the global attribute definition key * * @return list of allowed attribute values, if the global attribute definition does not have attribute list returns null */ public List<String> getAllowedAttributeValues(GlobalAttributeDefinitionKey globalAttributeDefinitionKey) { List<String> allowedAttributeValues = null; GlobalAttributeDefinitionEntity globalAttributeDefinitionEntity = globalAttributeDefinitionDao.getGlobalAttributeDefinitionByKey(globalAttributeDefinitionKey); if (globalAttributeDefinitionEntity.getAttributeValueList() != null) { allowedAttributeValues = new ArrayList<>(); Collection<AllowedAttributeValueEntity> list = globalAttributeDefinitionEntity.getAttributeValueList().getAllowedAttributeValues(); for (AllowedAttributeValueEntity allowedAttributeValueEntity : list) { allowedAttributeValues.add(allowedAttributeValueEntity.getAllowedAttributeValue()); } } return allowedAttributeValues; } }
AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setId(ATTRIBUTE_VALUE_LIST_ID); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(ATTRIBUTE_VALUE_LIST_NAME); attributeValueListEntity.setAllowedAttributeValues(new ArrayList<>());
/** * Creates and persists a new attribute value list entity from the request information. * * @param request the request * @param namespaceEntity the namespace * * @return the newly created attribute value list entity */ private AttributeValueListEntity createAttributeValueListEntity(AttributeValueListCreateRequest request, NamespaceEntity namespaceEntity) { // Create a new entity. AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(request.getAttributeValueListKey().getAttributeValueListName()); // Persist and return the newly created entity. return attributeValueListDao.saveAndRefresh(attributeValueListEntity); } }
for (AttributeValueListEntity attributeValueListEntity : entityManager.createQuery(criteria).getResultList()) attributeValueListKeys.add(new AttributeValueListKey(attributeValueListEntity.getNamespace().getCode(), attributeValueListEntity.getName()));
getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues()); attributeValueListEntity.getName())); attributeValueListEntity.getAllowedAttributeValues().remove(allowedAttributeValueEntity);
private GlobalAttributeDefinitionEntity createGlobalAttributeDefinitionEntityWithAllowedAttributeValues() { // Create attribute value list key. AttributeValueListKey attributeValueListKey = new AttributeValueListKey(NAMESPACE_CODE, ATTRIBUTE_VALUE_LIST_NAME); List<String> allowedAttributeValueList = Arrays.asList(ALLOWED_ATTRIBUTE_VALUE, ALLOWED_ATTRIBUTE_VALUE_2); // Create and persist a attribute value list key entity. AttributeValueListEntity attributeValueListEntity = attributeValueListDaoTestHelper.createAttributeValueListEntity(NAMESPACE_CODE, ATTRIBUTE_VALUE_LIST_NAME); // Create and persist a list of allowed attribute values. List<AllowedAttributeValueEntity> allowedAttributeValueEntities = allowedAttributeValueDaoTestHelper.createAllowedAttributeValueEntities(attributeValueListKey, allowedAttributeValueList); attributeValueListEntity.getAllowedAttributeValues().addAll(allowedAttributeValueEntities); GlobalAttributeDefinitionEntity globalAttributeDefinitionEntity = globalAttributeDefinitionDaoTestHelper.createGlobalAttributeDefinitionEntity(GLOBAL_ATTRIBUTE_DEFINITON_LEVEL, GLOBAL_ATTRIBUTE_DEFINITON_NAME); globalAttributeDefinitionEntity.setAttributeValueList(attributeValueListEntity); return globalAttributeDefinitionEntity; } }
@Test public void testDeleteAttributeValueList() { // Create an attribute value list key. AttributeValueListKey attributeValueListKey = new AttributeValueListKey(ATTRIBUTE_VALUE_LIST_NAMESPACE, ATTRIBUTE_VALUE_LIST_NAME); // Create a namespace entity. NamespaceEntity namespaceEntity = new NamespaceEntity(); namespaceEntity.setCode(ATTRIBUTE_VALUE_LIST_NAMESPACE); // Create an attribute value list entity. AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setId(ATTRIBUTE_VALUE_LIST_ID); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(ATTRIBUTE_VALUE_LIST_NAME); // Mock calls to external methods. when(attributeValueListDaoHelper.getAttributeValueListEntity(attributeValueListKey)).thenReturn(attributeValueListEntity); when(attributeValueListDaoHelper.createAttributeValueListFromEntity(attributeValueListEntity)).thenCallRealMethod(); // Call the method under test. AttributeValueList result = attributeValueListService.deleteAttributeValueList(attributeValueListKey); // Verify the external calls. verify(attributeValueListHelper).validateAttributeValueListKey(attributeValueListKey); verify(attributeValueListDaoHelper).getAttributeValueListEntity(attributeValueListKey); verify(attributeValueListDao).delete(attributeValueListEntity); verify(attributeValueListDaoHelper).createAttributeValueListFromEntity(attributeValueListEntity); verifyNoMoreInteractionsHelper(); // Validate the result. assertEquals(new AttributeValueList(ATTRIBUTE_VALUE_LIST_ID, attributeValueListKey), result); }
/** * Creates attribute value list entity and save it in the database. * * @param namespace the namespace of the attribute value list * @param attributeValueListName the name of the attribute value list * * @return the attribute value list entity */ public AttributeValueListEntity createAttributeValueListEntity(String namespace, String attributeValueListName) { // Create a namespace entity if not exists. NamespaceEntity namespaceEntity = namespaceDao.getNamespaceByCd(namespace); if (namespaceEntity == null) { namespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(namespace); } // Create an attribute value list entity. AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity(); attributeValueListEntity.setNamespace(namespaceEntity); attributeValueListEntity.setName(attributeValueListName); return attributeValueListDao.saveAndRefresh(attributeValueListEntity); } }
/** * Creates the allowed attribute values information from the persisted entities. * * @param attributeValueListEntity the attribute value list entity * @param allowedAttributeValueEntities the list of allowed attribute value entities * * @return the allowed attribute values information */ private AllowedAttributeValuesInformation createAllowedAttributeValuesInformationFromEntities(AttributeValueListEntity attributeValueListEntity, Collection<AllowedAttributeValueEntity> allowedAttributeValueEntities) { // Create an allowed attribute values information instance. AllowedAttributeValuesInformation allowedAttributeValuesInformation = new AllowedAttributeValuesInformation(); // Add the attribute value list key. AttributeValueListKey attributeValueListKey = new AttributeValueListKey(); allowedAttributeValuesInformation.setAttributeValueListKey(attributeValueListKey); attributeValueListKey.setNamespace(attributeValueListEntity.getNamespace().getCode()); attributeValueListKey.setAttributeValueListName(attributeValueListEntity.getName()); // Add the allowed attribute values. List<String> allowedAttributeValues = new ArrayList<>(); allowedAttributeValuesInformation.setAllowedAttributeValues(allowedAttributeValues); allowedAttributeValueEntities.forEach(allowedAttributeValueEntity -> { allowedAttributeValues.add(allowedAttributeValueEntity.getAllowedAttributeValue()); }); return allowedAttributeValuesInformation; }
getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues()); attributeValueListEntity.getName()));