Validation Constraints in ImpEx

You can define validation constraints in ImpEx files, making it easy to reload constraints after initializing the database.

Validation constraints are persisted to the database. If you frequently reinitialize the database during the development and testing phase of your project, there is a danger that you could erase any stored constraints. To ease the effort of restoring validation constraints, it is a good idea to define and load them using ImpEx files.

Define a Validation Constraint Using ImpEx

Define and load validation constraints using ImpEx files.

Prerequisites

The following acceptance test is included in your Hybris123Tests.java file.
public void testValidationConstraintAfterImpex() {
	loginToBackOffice();
	modifyABandToHaveNegativeAlbumSales();
	if (VersionHelper.getVersion().equals(Version.V2205) || VersionHelper.getVersion().equals(Version.V2211)) 
		waitFor("div","You have 1 Validation Messages");
	else
		waitFor("div","You have 1 Validation Errors");
	assertTrue( waitFor("span","Album sales must not be negative"));
}
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#testValidationConstraintAfterImpex test
cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testValidationConstraintAfterImpex test

Procedure

  1. Create a new ImpEx file, called essentialdata-constraints.impex, in the resources/impex folder of the concerttours extension.
    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

    SAP Commerce 123 Interactive Shortcut: If you are unable to create the ImpEx file or you want to skip this step, copy /hybris123/src/main/webapp/resources/impex/validation/essentialdata-constraints.impex to /hybris/bin/custom/concerttours/resources/impex/

    cp $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/impex/validation/essentialdata-constraints.impex $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/resources/impex/
    copy /y %HYBRIS_HOME_DIR%\hybris123\src\main\webapp\resources\impex\validation\essentialdata-constraints.impex %HYBRIS_HOME_DIR%\hybris\bin\custom\concerttours\resources\impex\
    
  2. Stop SAP Commerce.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ./hybrisserver.sh stop
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & hybrisserver.bat stop
  3. Initialize the database.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ant initialize
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & ant initialize
  4. Start SAP Commerce.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ./hybrisserver.sh start
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & hybrisserver.bat start
  5. Confirm that you are unable to save a band with negative album sales.
    1. Navigate to Start of the navigation pathSystem Next navigation step TypesEnd of the navigation path in the Explorer pane.
    2. Search for the Band item type.
    3. Click the Search by type toolbar button to list the bands in the database.
    4. Pick one of the bands.
    5. Try to set the album sales attribute to a negative number in the details pane and click Save.
    6. Confirm that the correct error message is displayed in the list of validation errors.
  6. Run the testValidationConstraintAfterImpex acceptance test again and confirm that it now passes.
    cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testValidationConstraintAfterImpex test
    cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testValidationConstraintAfterImpex test
  7. Commit the changes to your local Git repository.
    cd $HYBRIS_HOME_DIR/hybris; git add . ; git commit -m "Define a Validation Constraint Using ImpEx"
    cd %HYBRIS_HOME_DIR%\hybris & git add . & git commit -m "Define a Validation Constraint Using ImpEx"