@Test public void shouldNotCallAdditinanalFieldValidatorsIfPluginIsSystemDisabled() throws Exception { // given DataDefinition machineDao = dataDefinitionService.get(PLUGIN_MACHINES_NAME, ENTITY_NAME_MACHINE); machineDao.save(createMachine("asd")); pluginManager.disablePlugin(PLUGIN_MACHINES_NAME); DataDefinition productDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PRODUCT); DataDefinition componentDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_COMPONENT); Entity product = createProduct("asd", "asd"); product = productDao.save(product); Entity component = createComponent("name", product, null); component.setField("machineDescription", "as"); // when component = componentDao.save(component); // then Assert.assertTrue(component.isValid()); }
@Test public void shouldNotCallAdditinanalFieldValidatorsIfPluginIsDisabledForCurrentUser() throws Exception { // given DataDefinition machineDao = dataDefinitionService.get(PLUGIN_MACHINES_NAME, ENTITY_NAME_MACHINE); machineDao.save(createMachine("asd")); pluginManager.enablePlugin(PLUGIN_MACHINES_NAME); // This should be considered as an anti-pattern. Replacing static fields with mocks in an // integration test suite weren't // my smartest idea ever.. PluginStateResolver pluginStateResolver = mock(PluginStateResolver.class); PluginUtilsService pluginUtil = new PluginUtilsService(pluginStateResolver); pluginUtil.init(); given(pluginStateResolver.isEnabled(PLUGIN_MACHINES_NAME)).willReturn(false); DataDefinition productDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PRODUCT); DataDefinition componentDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_COMPONENT); Entity product = createProduct("asd", "asd"); product = productDao.save(product); Entity component = createComponent("name", product, null); component.setField("machineDescription", "as"); // when component = componentDao.save(component); // then Assert.assertTrue(component.isValid()); }
@Before public void init() { super.init(); progressPerShiftViewSaver = new ProgressPerShiftViewSaver(); ReflectionTestUtils.setField( progressPerShiftViewSaver, "progressForDayDataProvider", progressForDayDataProvider); given(dataDefinition.save(any(Entity.class))) .willAnswer( new Answer<Entity>() { @Override public Entity answer(final InvocationOnMock invocation) throws Throwable { return (Entity) invocation.getArguments()[0]; } }); ppsEntity = mockEntity(dataDefinition); stubHasManyField( ppsEntity, ProductionPerShiftFields.PLANNED_PROGRESS_CORRECTION_TYPES, Collections.<Entity>emptyList()); form = mockForm(ppsEntity); stubViewComponent("progressForDays", progressForDaysAdl); stubViewComponent("form", form); stubTransactionStatus(); }
@Test public final void shouldNotSaveFormBecauseOfDeviationCausesValidationErrors() { // given stubProgressType(ProgressType.PLANNED); stubTechnologyOperation(null); Entity deviationCauseEntity = mockEntity(dataDefinition); Entity invalidDeviationCauseEntity = mockEntity(dataDefinition); given(invalidDeviationCauseEntity.isValid()).willReturn(false); given(dataDefinition.save(deviationCauseEntity)).willReturn(invalidDeviationCauseEntity); AwesomeDynamicListComponent deviationCausesAdl = mock(AwesomeDynamicListComponent.class); stubViewComponent(CORRECTION_CAUSE_TYPES_ADL_REF, deviationCausesAdl); stubHasManyField( ppsEntity, ProductionPerShiftFields.PLANNED_PROGRESS_CORRECTION_TYPES, Lists.newArrayList(deviationCauseEntity)); // when progressPerShiftViewSaver.save(view); // then verify(txStatus, never()).setRollbackOnly(); verify(dataDefinition).save(deviationCauseEntity); verify(dataDefinition, never()).save(ppsEntity); verify(deviationCausesAdl).setFieldValue(Lists.newArrayList(invalidDeviationCauseEntity)); verify(form) .addMessage("qcadooView.message.saveFailedMessage", ComponentState.MessageType.FAILURE); }
public Entity createResource( final Entity position, final Entity warehouse, final Entity resource, final BigDecimal quantity, Object date) { DataDefinition resourceDD = dataDefinitionService.get( MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_RESOURCE); Entity newResource = resourceDD.create(); if (position != null) { Entity document = position.getBelongsToField(PositionFields.DOCUMENT); if (document != null) { Entity user = document.getBelongsToField(DocumentFields.USER); newResource.setField( ResourceFields.USER_NAME, user.getStringField(_FIRST_NAME) + " " + user.getStringField(L_LAST_NAME)); } } newResource.setField(ResourceFields.TIME, date); newResource.setField(ResourceFields.LOCATION, warehouse); newResource.setField( ResourceFields.PRODUCT, resource.getBelongsToField(PositionFields.PRODUCT)); newResource.setField(ResourceFields.QUANTITY, quantity); newResource.setField(ResourceFields.AVAILABLE_QUANTITY, quantity); newResource.setField(ResourceFields.RESERVED_QUANTITY, BigDecimal.ZERO); newResource.setField(ResourceFields.PRICE, resource.getField(PositionFields.PRICE)); newResource.setField(ResourceFields.BATCH, resource.getField(PositionFields.BATCH)); newResource.setField( ResourceFields.EXPIRATION_DATE, resource.getField(PositionFields.EXPIRATION_DATE)); newResource.setField( ResourceFields.PRODUCTION_DATE, resource.getField(PositionFields.PRODUCTION_DATE)); newResource.setField( ResourceFields.STORAGE_LOCATION, resource.getField(ResourceFields.STORAGE_LOCATION)); newResource.setField( ResourceFields.ADDITIONAL_CODE, resource.getField(ResourceFields.ADDITIONAL_CODE)); newResource.setField(ResourceFields.CONVERSION, resource.getField(ResourceFields.CONVERSION)); newResource.setField( ResourceFields.PALLET_NUMBER, resource.getField(ResourceFields.PALLET_NUMBER)); newResource.setField( ResourceFields.TYPE_OF_PALLET, resource.getField(ResourceFields.TYPE_OF_PALLET)); newResource.setField(ResourceFields.GIVEN_UNIT, resource.getField(ResourceFields.GIVEN_UNIT)); BigDecimal quantityInAdditionalUnit = numberService.setScale( quantity.multiply(resource.getDecimalField(ResourceFields.CONVERSION))); newResource.setField(ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT, quantityInAdditionalUnit); setResourceAttributesFromResource(newResource, resource); resourceStockService.addResourceStock(newResource); return resourceDD.save(newResource); }
public void setOrderAsConverted(final ImportedOrder order) { DataDefinition importedOrderDataDefinition = dataDefinitionService.get( SfcSimpleConstants.PLUGIN_IDENTIFIER, SfcSimpleConstants.MODEL_IMPORTED_ORDER); Entity importedOrder = importedOrderDataDefinition.get(order.getOriginalEntityId()); importedOrder.setField(FIELD_CONVERTED, "1"); importedOrderDataDefinition.save(importedOrder); }
public void setProductAsConverted(final ImportedProduct product) { DataDefinition importedProductDataDefinition = dataDefinitionService.get( SfcSimpleConstants.PLUGIN_IDENTIFIER, SfcSimpleConstants.MODEL_IMPORTED_PRODUCT); Entity importedProduct = importedProductDataDefinition.get(product.getOriginalEntityId()); importedProduct.setField(FIELD_CONVERTED, "1"); importedProductDataDefinition.save(importedProduct); }
public Entity createResource( final Entity document, final Entity warehouse, final Entity position, final Object date) { DataDefinition resourceDD = dataDefinitionService.get( MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_RESOURCE); Entity product = position.getBelongsToField(PositionFields.PRODUCT); Entity resource = resourceDD.create(); Entity user = document.getBelongsToField(DocumentFields.USER); resource.setField( ResourceFields.USER_NAME, user.getStringField(_FIRST_NAME) + " " + user.getStringField(L_LAST_NAME)); resource.setField(ResourceFields.TIME, date); resource.setField(ResourceFields.LOCATION, warehouse); resource.setField(ResourceFields.PRODUCT, position.getBelongsToField(PositionFields.PRODUCT)); resource.setField(ResourceFields.QUANTITY, position.getField(PositionFields.QUANTITY)); resource.setField(ResourceFields.RESERVED_QUANTITY, BigDecimal.ZERO); resource.setField( ResourceFields.AVAILABLE_QUANTITY, position.getDecimalField(PositionFields.QUANTITY)); resource.setField(ResourceFields.PRICE, position.getField(PositionFields.PRICE)); resource.setField(ResourceFields.BATCH, position.getField(PositionFields.BATCH)); resource.setField( ResourceFields.EXPIRATION_DATE, position.getField(PositionFields.EXPIRATION_DATE)); resource.setField( ResourceFields.PRODUCTION_DATE, position.getField(PositionFields.PRODUCTION_DATE)); resource.setField( ResourceFields.STORAGE_LOCATION, position.getField(PositionFields.STORAGE_LOCATION)); resource.setField( ResourceFields.ADDITIONAL_CODE, position.getField(PositionFields.ADDITIONAL_CODE)); resource.setField( ResourceFields.PALLET_NUMBER, position.getField(PositionFields.PALLET_NUMBER)); resource.setField( ResourceFields.TYPE_OF_PALLET, position.getField(PositionFields.TYPE_OF_PALLET)); resource.setField(ResourceFields.WASTE, position.getField(PositionFields.WASTE)); if (StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) { resource.setField(ResourceFields.GIVEN_UNIT, product.getField(ProductFields.UNIT)); resource.setField( ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT, position.getField(PositionFields.QUANTITY)); resource.setField(ResourceFields.CONVERSION, BigDecimal.ONE); } else { resource.setField(ResourceFields.GIVEN_UNIT, position.getField(PositionFields.GIVEN_UNIT)); resource.setField( ResourceFields.QUANTITY_IN_ADDITIONAL_UNIT, position.getField(PositionFields.GIVEN_QUANTITY)); resource.setField(ResourceFields.CONVERSION, position.getField(PositionFields.CONVERSION)); } setResourceAttributesFromPosition(resource, position); resourceStockService.addResourceStock(resource); return resourceDD.save(resource); }
@Test public final void shouldNotSaveFormAndCorrectedProgressesDueToDailyProgressesValidationErrors() { // given Long tocId = 1001L; Entity pfd1dp1 = mockDailyProgress(); Entity pfd1dp2 = mockDailyProgress(); Entity pfd1 = mockProgressForDayEntity(pfd1dp1, pfd1dp2); Entity pfd2dp1 = mockDailyProgress(); Entity pfd2 = mockProgressForDayEntity(pfd2dp1); given(pfd2.isValid()).willReturn(false); Entity invalidPfd2dp1 = mockDailyProgress(); given(invalidPfd2dp1.isValid()).willReturn(false); given(dataDefinition.save(pfd2dp1)).willReturn(invalidPfd2dp1); stubProgressForDaysAdlEntities(pfd1, pfd2); stubProgressType(ProgressType.PLANNED); Entity technologyOperation = mockEntity(tocId, dataDefinition); given(technologyOperation.isValid()).willReturn(true); stubTechnologyOperation(technologyOperation); given(ppsEntity.isValid()).willReturn(true); // when progressPerShiftViewSaver.save(view); // then verify(txStatus, times(4)) .setRollbackOnly(); // one rollback for each DailyProgress + 1 global rollback verifyTocSetupAbsence(technologyOperation); verify(dataDefinition).save(pfd1dp1); verify(dataDefinition).save(pfd1dp2); verify(dataDefinition, never()).save(pfd1); verify(pfd1).setField(ProgressForDayFields.CORRECTED, false); verify(pfd1).setField(ProgressForDayFields.TECHNOLOGY_OPERATION_COMPONENT, tocId); verify(dataDefinition).save(pfd2dp1); verify(pfd2).setNotValid(); verify(dataDefinition, never()).save(pfd2); verify(pfd2, never()).setField(ProgressForDayFields.CORRECTED, false); verify(pfd2, never()).setField(ProgressForDayFields.TECHNOLOGY_OPERATION_COMPONENT, tocId); verify(dataDefinition, never()).save(ppsEntity); verify(form) .addMessage("qcadooView.message.saveFailedMessage", ComponentState.MessageType.FAILURE); }
/** * Save document in database and creates resources if document is accepted. * * @return Created document entity. */ public Entity build() { DataDefinition documentDD = dataDefinitionService.get( MaterialFlowResourcesConstants.PLUGIN_IDENTIFIER, MaterialFlowResourcesConstants.MODEL_DOCUMENT); document.setField(DocumentFields.POSITIONS, positions); Entity savedDocument = documentDD.save(document); if (savedDocument.isValid() && DocumentState.of(document) == DocumentState.ACCEPTED) { createResources(savedDocument); if (!savedDocument.isValid()) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } } return savedDocument; }
private Entity performFieldValidationTestOnPart( final String fieldName, final Object fieldValue, final boolean shouldPass) { DataDefinition partDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PART); pluginManager.enablePlugin(PLUGIN_PRODUCTS_NAME); Entity part = createPart("somePart", null); part.setField(fieldName, fieldValue); // when Entity savedPart = partDao.save(part); // then Assert.assertEquals(shouldPass, savedPart.isValid()); return savedPart; }
@Test public void shouldCallAndPassMaxStringLenFieldValidators() throws Exception { // given String stringWith300Characters = StringUtils.repeat("a", 300); DataDefinition productDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PRODUCT); pluginManager.enablePlugin(PLUGIN_PRODUCTS_NAME); Entity product = createProduct(stringWith300Characters, "asd"); // when Entity savedProduct = productDao.save(product); // then Assert.assertTrue(savedProduct.isValid()); Assert.assertEquals(stringWith300Characters, savedProduct.getStringField("name")); }
@Test public final void shouldNotSaveFormBecauseOfValidationErrors() { // given stubProgressType(ProgressType.PLANNED); stubTechnologyOperation(null); Entity invalidPpsEntity = mockEntity(dataDefinition); given(invalidPpsEntity.isValid()).willReturn(false); given(dataDefinition.save(ppsEntity)).willReturn(invalidPpsEntity); // when progressPerShiftViewSaver.save(view); // then verify(txStatus, never()).setRollbackOnly(); verify(dataDefinition).save(ppsEntity); verify(form) .addMessage("qcadooView.message.saveFailedMessage", ComponentState.MessageType.FAILURE); }
@Test public void shouldCreatetTechnologyInstOperProductInComp() throws Exception { // given Long orderId = 1L; Long technologyId = 2L; BigDecimal nominalCost1 = BigDecimal.TEN; BigDecimal nominalCost2 = BigDecimal.TEN; when(productQuantities.entrySet()).thenReturn(entrySet); when(entrySet.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, false); when(iterator.next()).thenReturn(entry1, entry2); when(entry1.getKey()).thenReturn(prod1); when(entry2.getKey()).thenReturn(prod2); when(prod1.getDecimalField("nominalCost")).thenReturn(nominalCost1); when(prod2.getDecimalField("nominalCost")).thenReturn(nominalCost2); when(order.getBelongsToField("technology")).thenReturn(technology); when(order.getId()).thenReturn(orderId); when(orderDD.get(orderId)).thenReturn(orderFromDB); when(technology.getId()).thenReturn(technologyId); when(costNormsForMaterialsService.getProductQuantitiesFromTechnology(technologyId)) .thenReturn(productQuantities); when(techInsOperCompProdInDD.create()) .thenReturn(techInsOperCompProdIn1, techInsOperCompProdIn2); when(techInsOperCompProdIn1.getDataDefinition()).thenReturn(techInsOperCompProdInDD); when(techInsOperCompProdIn2.getDataDefinition()).thenReturn(techInsOperCompProdInDD); when(techInsOperCompProdInDD.save(techInsOperCompProdIn1)) .thenReturn(techInsOperCompProdIn1, techInsOperCompProdIn2); // when orderHooksCNFM.fillOrderOperationProductsInComponents(orderDD, order); // then Mockito.verify(techInsOperCompProdIn1).setField("order", order); Mockito.verify(techInsOperCompProdIn1).setField("product", prod1); Mockito.verify(techInsOperCompProdIn1).setField("nominalCost", nominalCost1); Mockito.verify(techInsOperCompProdIn2).setField("order", order); Mockito.verify(techInsOperCompProdIn2).setField("product", prod2); Mockito.verify(techInsOperCompProdIn2).setField("nominalCost", nominalCost2); }
@Test public final void shouldGenerateWorkPlanEntity() throws Exception { // given Entity emptyWorkPlan = mock(Entity.class); when(workPlanDD.create()).thenReturn(emptyWorkPlan); when(workPlanDD.save(emptyWorkPlan)).thenReturn(emptyWorkPlan); when(emptyWorkPlan.getDataDefinition()).thenReturn(workPlanDD); Entity order = mock(Entity.class); @SuppressWarnings("unchecked") Iterator<Entity> iterator = mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, true, true, false); when(iterator.next()).thenReturn(order); @SuppressWarnings("unchecked") List<Entity> orders = mock(List.class); when(orders.iterator()).thenReturn(iterator); when(orders.size()).thenReturn(3); when(orders.get(Mockito.anyInt())).thenReturn(order); @SuppressWarnings("rawtypes") ArgumentCaptor<List> listArgCaptor = ArgumentCaptor.forClass(List.class); ArgumentCaptor<String> stringArgCaptor = ArgumentCaptor.forClass(String.class); // when workPlanService.generateWorkPlanEntity(orders); // then verify(emptyWorkPlan, times(1)).setField(Mockito.eq("orders"), listArgCaptor.capture()); @SuppressWarnings("unchecked") List<Entity> resultOrders = listArgCaptor.getValue(); Assert.assertEquals(orders.size(), resultOrders.size()); Assert.assertSame(order, resultOrders.get(0)); Assert.assertSame(order, resultOrders.get(1)); Assert.assertSame(order, resultOrders.get(2)); verify(emptyWorkPlan, times(1)).setField(Mockito.eq("name"), stringArgCaptor.capture()); Assert.assertEquals(TRANSLATED_STRING, stringArgCaptor.getValue()); verify(emptyWorkPlan, times(1)).setField(Mockito.eq("type"), stringArgCaptor.capture()); Assert.assertEquals(WorkPlanType.NO_DISTINCTION.getStringValue(), stringArgCaptor.getValue()); }