Check that the constraint is working as expected and introduces the Hybris Validation service in the code.
Prerequisites
The following acceptance test is included in your Hybris123Tests.java file.
public void testCustomConstraintIntegrationTest() {
assertTrue( checkTestSuiteXMLMatches("(.*)testsuite errors=\"0\" failures=\"0\" (.*) name=\"NotLoremIpsumConstraintTest\" package=\"concerttours.constraints\" tests=\"1\"(.*)" ));
}
Before you begin this step, run the test to confirm that it fails by executing this command:
cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testCustomConstraintIntegrationTest test
cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testCustomConstraintIntegrationTest test
Procedure
- Load the Constraint via Impex by adding the new Lorem Ipsum constraint to the essentialdata-constraints.impex file.
INSERT_UPDATE MinConstraint;id[unique=true];severity(code,itemtype(code));active;annotation;descriptor(enclosingType(code),qualifier);message[lang=de];message[lang=en];value
;AlbumSalesMustNotBeNegative;ERROR:Severity;true;javax.validation.constraints.Min;Band:albumSales;Albumverkäufe dürfen nicht negativ sein;Album sales must not be negative;0
INSERT_UPDATE NotLoremIpsumConstraint;id[unique=true];severity(code,itemtype(code));active;annotation;descriptor(enclosingType(code),qualifier);message[lang=de];message[lang=en]
;NotIpsum;ERROR:Severity;true;concerttours.constraints.NotLoremIpsum;Band:history;Band Geschichte sollte nicht Lorem ipsum Platzhalter Text.;Band history should not be Lorem ipsum placeholder text.;
SAP Commerce 123 Interactive Shortcut: If you are unable to add the constraint or you want to skip this step, replace <HYBRIS_HOME_DIR>/hybris/bin/custom/concerttours/resources/impex/essentialdata-constraints.impex with the contents of <HYBRIS_HOME_DIR>/hybris123/src/main/webapp/resources/impex/validation/essentialdata-constraints-2.impex.
cp $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/impex/validation/essentialdata-constraints-2.impex $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/resources/impex/essentialdata-constraints.impex
echo a | xcopy %HYBRIS_HOME_DIR%\hybris123\src\main\webapp\resources\impex\validation\essentialdata-constraints-2.impex %HYBRIS_HOME_DIR%\hybris\bin\custom\concerttours\resources\impex\essentialdata-constraints.impex
- Create the integration test called NotLoremIpsumConstraintTest in the concerttours.constraints package under the testsrc folder of the concerttours extension.
package concerttours.constraints;
import de.hybris.bootstrap.annotations.IntegrationTest;
import de.hybris.platform.servicelayer.ServicelayerTransactionalTest;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.validation.exceptions.HybrisConstraintViolation;
import de.hybris.platform.validation.services.ValidationService;
import java.util.Set;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import concerttours.model.BandModel;
import concerttours.model.NotLoremIpsumConstraintModel;
@IntegrationTest
public class NotLoremIpsumConstraintTest extends ServicelayerTransactionalTest
{
@Resource
private ModelService modelService;
@Resource
private ValidationService validationService;
@Before
public void setup() throws Exception
{
createCoreData();
importCsv("/impex/essentialdata-constraints.impex", "UTF-8");
validationService.reloadValidationEngine();
}
@Test
public void testLoremIpsumConstraint()
{
final BandModel band = modelService.create(BandModel.class);
band.setCode("LoremIpsumTest1");
band.setName("LoremIpsumBand");
band.setHistory("Lorem Ipsum is here");
final Set<HybrisConstraintViolation> violations = validationService.validate(band);
Assert.assertTrue("The violation set sould not be null or empty", violations != null && violations.size() > 0);
Assert.assertEquals("There should be one constraint violations", 1, violations.size());
for (final HybrisConstraintViolation hybrisConstraintViolation : violations)
{
Assert.assertEquals(NotLoremIpsumConstraintModel.class, hybrisConstraintViolation.getConstraintModel().getClass());
}
}
}
SAP Commerce 123 Interactive Shortcut: If you are unable to add the constraint or you want to skip this step, replace <HYBRIS_HOME_DIR>/hybris/bin/custom/concerttours/testsrc/concerttours/constraints/NotLoremIpsumConstraintTest.java with the contents of <HYBRIS_HOME_DIR>/hybris123/src/main/webapp/resources/concerttours/testsrc/concerttours/constraints/NotLoremIpsumConstraintTest.java.
ditto $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/concerttours/testsrc/concerttours/constraints/NotLoremIpsumConstraintTest.java $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/testsrc/concerttours/constraints/NotLoremIpsumConstraintTest.java
echo f | xcopy %HYBRIS_HOME_DIR%\hybris123\src\main\webapp\resources\concerttours\testsrc\concerttours\constraints\NotLoremIpsumConstraintTest.java %HYBRIS_HOME_DIR%\hybris\bin\custom\concerttours\testsrc\concerttours\constraints\NotLoremIpsumConstraintTest.java
- Stop SAP Commerce.
cd $HYBRIS_HOME_DIR/hybris/bin/platform; ./hybrisserver.sh stop
cd %HYBRIS_HOME_DIR%\hybris\bin\platform & hybrisserver.bat stop
- Initialize the system.
cd $HYBRIS_HOME_DIR/hybris/bin/platform; ant clean all yunitinit
cd %HYBRIS_HOME_DIR%\hybris\bin\platform & ant clean all yunitinit
- Run the integration tests.
cd $HYBRIS_HOME_DIR/hybris/bin/platform; ant integrationtests -Dtestclasses.packages="concerttours.*"
cd %HYBRIS_HOME_DIR%\hybris\bin\platform & ant integrationtests -Dtestclasses.packages=concerttours.*
- View the test results at <HYBRIS_HOME_DIR>/hybris/log/junit/index.html
- Run the testCustomConstraintIntegrationTest acceptance test again and confirm that it now passes.
cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testCustomConstraintIntegrationTest test
cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testCustomConstraintIntegrationTest test
- Commit the changes to your local Git repository.
cd $HYBRIS_HOME_DIR/hybris; git add . ; git commit -m "Write an Integration Test for the Custom Constraint"
cd %HYBRIS_HOME_DIR%\hybris & git add . & git commit -m "Write an Integration Test for the Custom Constraint"