Interfacing With Commerce Engine
Interfacing With Commerce Engine
This tutorial assumes you have created the terms and conditions resource in Writing Your First Resource.
Commerce Engine is the heart of Elastic Path's e-commerce system and is responsible for executing e-commerce operations, such as adding to cart, updating customer profiles, and so on. To call down into Commerce Engine to run operations, Cortex resources use lookups to read data, and writers to create, update, and delete data. For an example of a call progressing through Cortex architecture to Commerce Engine, see Call Stack.
- Add a lookup strategy interface to the terms and conditions resource you created in Writing Your First Resource.
- Create a resource integration to implement the lookup strategy and call into Commerce Engine to retrieve data.
After completing this tutorial, you can retrieve the terms and conditions resource and see data returned from Commerce Engine.
Implement a Lookup Strategy in the Terms and Conditions Resource
In this section, you'll define a lookup strategy interface in the terms and conditions resource and change the resource operator implementation to call that lookup strategy. In a later section, you will create a resource integration that implements the lookup strategy interface in order to return data from Commerce Engine.
Use the resource-integration generator to create the strategy interface and implementation:
- Open a command line and navigate to the extensions/tutorials directory.
- Run the following command to add the lookup strategy interface to the Cortex resource
implementation project and create the Cortex resource integration project:
mvn clean package -Dtheme=cortex -Dpatch=generators/resource-integration -Dproperties="resourceFamily=terms,resourceEntity=termsAndConditions"
- In your IDE, open the extensions/cortex/resources/terms-and-conditions project.
- Open TermsResourceOperatorImpl and change the
processRead method to call the TermsLookupStrategy
interface in order to retrieve the terms-and-conditions
entity:
public OperationResult processRead( @ResourceId final String termsId, final ResourceOperation operation) { return new OperationResultChain() { @Override protected OperationResult build() { TermsAndConditionsEntity entity = Assign.ifSuccessful(termsLookupStrategy.getTerms(termsId)); ResourceState<TermsAndConditionsEntity> state = ResourceState.Builder .create(entity) .withSelf(createSelf(operation.getUri())) .build(); return createReadOK(state, operation); } }.execute(operation); }
- In TermsResourceOperatorImplTest, delete the tests
shouldBuildStateWithId() and
shouldBuildStateWithValue(), and replace them with a test that
verifies the entity from the lookup strategy is
used:
@Test public void shouldUseEntityFromLookup() { TermsAndConditionsEntity expected = TermsAndConditionsEntity.Builder.builder() .withTermsId(TERMS_ID) .build(); ExecutionResult<TermsAndConditionsEntity> executionResult = ExecutionResultFactory.createReadOK(expected); when(termsLookupStrategy.getTerms(TERMS_ID)).thenReturn(executionResult); OperationResult result = resourceOperator.processRead(TERMS_ID, resourceOperation); ResourceState<TermsAndConditionsEntity> state = (ResourceState<TermsAndConditionsEntity>) result.getResourceState(); assertEquals(expected, state.getEntity()); }
- Add any missing imports and then run the following command to rebuild the
terms and conditions resource JAR file and execute the unit
tests:
mvn clean install
Creating the Resource Integration
- In your IDE, open the extensions/cortex/integrations/terms-and-conditions-resource-epcommerce project.
- In the lookup strategy implementation class,
TermsLookupStrategyImpl:
- Add a reference to the SettingsService and inject it via the
constructor:
private final SettingsService settingsService; /** * Constructor. * * @param settingsService the tag definition reader */ @Inject public TermsLookupStrategyImpl( @Named("settingsService") final SettingsService settingsService) { this.settingsService = settingsService; }
- Add a private method to wrap the service call in an
ExecutionResult:
private ExecutionResult<String> findTermsAndConditionMessage() { ExecutionResult<String> result; Set<SettingDefinition> definitions = settingsService.getAllSettingDefinitions(); if (definitions == null || definitions.isEmpty()) { result = ExecutionResultFactory.createNotFound(); } else { result = ExecutionResultFactory.createReadOK(definitions.iterator().next().getDescription()); } return result; }
- In the getTerms() method, change the
.withEntityValue(value) line to call your private
method:
.withMessage(Assign.ifSuccessful(findTermsAndConditionMessage()))
- Add a reference to the SettingsService and inject it via the
constructor:
- Make corresponding changes in the test class,
TermsLookupStrategyImplTest:
- Add a mock for the
SettingsService:
@Mock private SettingsService settingsService;
- Write a test to verify the strategy calls the settings service uses and to verify
a setting definition description as the message field of a new
terms-and-conditions
entity:
@Test public void testGetTermsWhenSuccessful() { final String expectedMessage = "EP Commerce Terms and Conditions"; Set<SettingDefinition> definitions = new HashSet<SettingDefinition>(); SettingDefinition definition = mock(SettingDefinition.class); definitions.add(definition); when(definition.getDescription()).thenReturn(expectedMessage); when(settingsService.getAllSettingDefinitions()).thenReturn(definitions); ExecutionResult<TermsAndConditionsEntity> result = termsLookupStrategy.getTerms(SCOPE); assertTrue("The call should have been successful", result.isSuccessful()); assertEquals("The expected message should have been returned", expectedMessage, result.getData().getMessage()); }
- Add a mock for the
SettingsService:
- Make sure the SettingsService is imported as a blueprint service by adding the
following to
OSGI-INF/blueprint/terms-integration-blueprint.xml:
<reference id="settingsService" interface="com.elasticpath.settings.SettingsService"/commerce-legacy/>
- Run the following command to build the terms and conditions resource
integration JAR file and execute the unit
tests:
mvn clean install
Testing the Integration
At this point, you've created all the necessary components for integrating the terms and conditions resource with Commerce Engine and the final result can be verified.
- Open a command line and navigate to the extensions/cortex/ext-cortex-webapp directory.
- Run the following command to build and run the Cortex web
application:
mvn clean tomcat7:run-war
- Open Cortex Studio in a browser
at http://localhost:9080/studio/ and
submit a GET Request
for:
/terms/12345=
Response:{ self: { type: "{vendor}.terms.terms-and-conditions", uri: "/commerce-legacy/terms/12345=", href: "http://localhost:9080/cortex/terms/12345=" }, message: "Set to true to enable search autocomplete in the storefront.", links: [ ] }
Congratulations, you’ve completed your first integration of a Cortex resource with Commerce Engine.