Deploying and validating the TutorialStore Web service module with JUnit
In this step, you are going to use the generated JUnit test class, TutorialStoreFacadeClientTest, to validate your TutorialStore service module.
About this task
Procedure
-
Update the JUnit test client:
- Open the SOITutorialStore-UnitTests/src/com.mycompany.commerce.soitutorialstore.facade.SOITutorialStoreFacadeClientTest class.
- Add the following import: import junit.framework.Assert;
-
Replace the methods testGetTutorialStore() and testTutorialStore()
with the following JUnit test methods. Replace
user
and
password
with
your WebSphere Commerce administrator ID and password after the class
SOITutorialStoreFacadeClientTest
and
also inside the
TestTutorialStore
function.. This
method assumes that a WebSphere Commerce store with storeID of 10001
exists and is currently open. If this is not the case, replace "10001"
with your WebSphere Commerce storeID.
public void testGetTutorialStore() {> // Create the query expression using an XPath statement and access // profile. String storeID = "10001";> SelectionCriteriaHelper selectionCriteriaHelper = new SelectionCriteriaHelper( "/TutorialStore/TutorialStoreIdentifier[(UniqueID=" + storeID + ")]"); selectionCriteriaHelper.addAccessProfile("MyCompany_Summary"); ExpressionType queryExpression = selectionCriteriaHelper .getSelectionCriteriaExpression();> // Create the Get BOD from query expression and send it to the server. try { ShowTutorialStoreDataAreaType result = iClient .getTutorialStore(iClient.createGetVerb(queryExpression)); TutorialStoreType tutorialStore = (TutorialStoreType) result .getTutorialStore().get(0); assertEquals(storeID, tutorialStore.getStoreIdentifier() .getUniqueID()); } catch (TutorialStoreException e) { Assert.fail(e.getLocalizedMessage()); } catch (Exception e) { Assert.fail(e.getLocalizedMessage()): } }>
public void testTutorialStore() {> String[] storeID = { "10001" }; BusinessContextType businessContext = CommerceFoundationFactory.eINSTANCE .createBusinessContextType();> // Change username and password to match the server. javax.security.auth.callback.CallbackHandler callbackHandler = new SampleCallbackHandlerImpl( " user ", " password ");> // Create the client facade. SOITutorialStoreFacadeClient client = new SOITutorialStoreFacadeClient( businessContext, callbackHandler);> // Verify that the store is open. try { ShowTutorialStoreDataAreaType result = client .findStoreById( storeID, SOITutorialStoreFacadeConstants.ACCESS_PROFILE_DETAILS_INFORMATION); TutorialStoreType tutorialStore = (TutorialStoreType) result .getTutorialStore().get(0); assertEquals("Open", tutorialStore.getStoreState().getName()); } catch (TutorialStoreException tse) { Assert.fail(tse.getLocalizedMessage()); } catch (Exception e) { Assert.fail(e.getLocalizedMessage()); } }
-
Replace the method testProcessTutorialStore() with the
following JUnit test method:
public void testProcessTutorialStore() {> String[] storeID = { "10001" }; SOITutorialStoreFactory iFactory = SOITutorialStoreFactory.eINSTANCE;> // Construct the request BOD. ProcessTutorialStoreType processTutorialStore = iFactory .createProcessTutorialStoreType(); processTutorialStore.setDataArea(iFactory .createProcessTutorialStoreDataAreaType());> // Construct the TutorialStore noun and add it to the request. java.util.List TutorialStoreList = processTutorialStore.getDataArea() .getTutorialStore(); TutorialStoreType tutorialstore = iFactory.createTutorialStoreType(); StoreIdentifierType storeIdentifier = CommerceFoundationFactory.eINSTANCE .createStoreIdentifierType(); storeIdentifier.setUniqueID(storeID[0]); tutorialstore.setStoreIdentifier(storeIdentifier); TutorialStoreList.add(tutorialstore);> try { // Verify that the store is open. ShowTutorialStoreDataAreaType result = iClient .findStoreById( storeID, SOITutorialStoreFacadeConstants.ACCESS_PROFILE_DETAILS_INFORMATION); TutorialStoreType tutorialStore = (TutorialStoreType) result .getTutorialStore().get(0); assertEquals(StoreStateType.OPEN, tutorialStore.getStoreState() .getValue());> // Construct the open action expression. java.util.List actions = new java.util.ArrayList(); actions.add(AbstractBusinessObjectDocumentFacadeClient .createActionExpression( SOITutorialStoreFacadeConstants.PROCESS_ACTION_CLOSE, SelectionCriteriaHelper.STR_XPATH_LANG, SOITutorialStoreFacadeConstants.XPATH_STORE)); com.ibm.commerce.oagis9.datatypes.ProcessType processVerb = AbstractBusinessObjectDocumentFacadeClient .createProcessVerb(actions); processTutorialStore.getDataArea().setProcess(processVerb);> // send the request BOD and get back the response AcknowledgeTutorialStoreType acknowledgeTutorialStore = iClient .processTutorialStore(processTutorialStore);> // Pause while the store registry refreshes. try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); }> // Verify that the store is closed. result = iClient .findStoreById( storeID, SOITutorialStoreFacadeConstants.ACCESS_PROFILE_DETAILS_INFORMATION); tutorialStore = (TutorialStoreType) result.getTutorialStore().get(0); assertEquals(StoreStateType.CLOSED, tutorialStore.getStoreState() .getValue());> // Construct the close action expression. actions.clear(); actions.add(AbstractBusinessObjectDocumentFacadeClient .createActionExpression( SOITutorialStoreFacadeConstants.PROCESS_ACTION_OPEN, SelectionCriteriaHelper.STR_XPATH_LANG, SOITutorialStoreFacadeConstants.XPATH_STORE)); processVerb = AbstractBusinessObjectDocumentFacadeClient .createProcessVerb(actions); processTutorialStore.getDataArea().setProcess(processVerb);> // send the request BOD and get back the response acknowledgeTutorialStore = iClient .processTutorialStore(processTutorialStore);> // Pause while the store registry refreshes. try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } // Verify that the store is open. result = iClient .findStoreById( storeID, SOITutorialStoreFacadeConstants.ACCESS_PROFILE_DETAILS_INFORMATION); tutorialStore = (TutorialStoreType) result.getTutorialStore().get(0); assertEquals(StoreStateType.OPEN, tutorialStore.getStoreState() .getValue()); } catch (TutorialStoreException tse) { Assert.fail(tse.getLocalizedMessage()); } catch (Exception e) { Assert.fail(e.getLocalizedMessage()); } }
- Save your changes and close the file.
-
Organize the imports for the SOITutorialStore-UnitTests
project:
- Open the Java perspective in WebSphere Commerce Developer.
- Right-click the SOITutorialStore-UnitTests\src folder.
- Select Source > Organize Imports .
- Start the test server: On the Server page, right-click the WebSphere Commerce Test Server and select Start .
-
If the WC project is not already published to the WebSphere
Commerce Test Server, publish the WebSphere Commerce project:
- On the Server page, right-click the WebSphere Commerce Test Server and select Add and Remove Projects .
- Select the WC project.
- Click Add and click Finish .
If the WC project is already published, right-click the WebSphere Commerce Test Server and select Publish .
-
Set up a TCP/IP monitor in WebSphere Commerce Developer.
You will use this TCP/IP monitor to observe the request and response
documents going to and from the WebSphere Commerce service you created.
To create a TCP/IP Monitor to forward requests to WebSphere Commerce:
- Select Window > Preferences .
- From the Preferences panel select Run/Debug > TCP/IP Monitor .
- Click Add .
-
Type the following information:
- Local monitoring port: 81.
- Hostname: The hostname of the WebSphere Commerce Server where the SOITutorialStore service is running.
- Port: 80 for WebSphere Commerce Developer, 8007 for production.
- Click OK .
- Select the created TCP/IP monitor.
- Click Start .
- Click OK .
-
Configure the TutorialStore Unit Test to use the TCP/IP
monitor:
- Go to SOITutorialStore-UnitTests\src\config\com.mycompany.commerce.soitutorialstore
- Open wc-component-client.xml in a text editor.
- Change the value of the url property from localhost:8007 to localhost:81 .
- Save the file.
-
Run the JUnit test:
- Go to SOITutorialStore-UnitTests/src/com.mycompany.commerce.soitutorialstore.facade
- Right-click the SOITutorialStoreFacadeClientTest.java class and select Run as > JUnit Test . You should see the request and response XML documents transmitted between the client and server on the TCP/IP monitor.
- If the validation is successful, a green bar is displayed on the JUnit view.
-
From the TCP/IP Monitor, you can review the Get message
sent by the client library, and the Show response replied by the component
facade.
The following BODs are present:
- Process BOD.
- Acknowledge BOD.