Fire up your applications with Jazz
RSS
Jazz CMS Wiki

About Us

CMS CMS Administration GUI Jazz Jazz Administration Jazz Web Parts Library misc Roles Solutions Training Web Parts Web Service Website Design Website Procedures Wiki conventions WordML Workflow Administration

Quick Search
»
Advanced Search »

Web Service: Unit Test Walk Through

RSS
Modified on 2011/10/11 19:22 by Charles Categorized as Web Service, Website Design

Unit Test Walk Through of Web Service Access

(return to Jetfire Web Service)

This page shows two C# .NET Unit test cases that runs in a Visual Studio environment. Both add a Member (wrestler) to a Club, set some properties for the member and then verify that the properties are set correctly.

The difference between the two test cases is:
  1. The first uses the Jetfire Web Service Utility
  2. The second connects directly to the Web Service.

Unit Test using Jetfire Web Service Utility

The following code shows a C# .NET Unit test case that runs in a Visual Studio environment with Jetfire Workflows and the JfWS utility. The specific situation is to add a Member (wrestler) to a Club, set some properties for the member, create an OawaRegistrationAudit and set some properties for the audit.

Some comments about the Unit Test Example:
  1. In reviewing this code, it should be clear that this is compact code.

[TestMethod]
public void WsOAWA_CreateWrestlerUsingJfWS()
{
       string error = "";
       string auditGuid = null;
       // Create the Workspace and Club
       OawaRegistrationTests tests = new OawaRegistrationTests();
       tests.OawaRegistration_CreateWorkspaceOawaClub();
       // Start the Web Service and create a wrestler
       using (JfWS ws = new JfWS("http://Web_Service_Domain/JetfireWebService.asmx"))
       {
              // login George, the administrator
              if (!ws.Login("George", "", "Registration", ref error))
                     Assert.Fail("George Login 1 Failed: " + error);
              // Find the Club
              Workflows workflows = ws.GetWorkflowBySubject("OawaClub", " New OAWA Club", ref error);
              // Verify that club is returned
              Assert.IsTrue(workflows.Count > 0, error);
              Workflow club = null;
              // Get the actual club.  'club' contains methods and properties that are part of the club.
              if (!workflows.GetFirstWorkflow(ref club, ref error))
                     Assert.Fail(error);
              // Add a wrestler to the club
              workflows = ws.ExecuteCommand(club, "AddWrestler", ref error);
              if (workflows.Count < 1)
                     Assert.Fail(error);
              Workflow wrestler = null;
              // The updated wrestler record is returned.  Verify that there are no errors.
              if (!workflows.GetFirstWorkflow(ref wrestler, ref error))
                     Assert.Fail(error);
              Assert.AreEqual("OawaContact", wrestler.WorkflowClass, "Not an OawaContact");
              // Save a property in the wrestler record
              TjDate date = new TjDate(new System.DateTime(1995, 12, 1));
              wrestler.SetValue("FirstName", "George");
              wrestler.SetValue("LastName", "Jones");
              wrestler.SetValue("DOB", date.ToString(C.DateFormat));
              if (!ws.SaveWorkflow(ref wrestler, ref error))
                     Assert.Fail(error);
              string dateValue = wrestler.GetValue("DOB");
              Assert.AreEqual(date.ToString(C.DateFormat), dateValue);
              // Create audit for the registration
              WorkflowClass wClass = null;
              if (!ws.GetWorkflowClass(ref wClass, "OawaRegistrationAudit", ref error))
                     Assert.Fail(error);
              Dictionary<string, object> parameters = new Dictionary<string, object>();
              parameters.Add("Workflow", wrestler.Guid);
              parameters.Add("Workspace", wrestler.Workspace);
              workflows = ws.ExecuteCommand(wClass, "Create", parameters, ref error);
              Workflow audit = null;
              if (!workflows.GetFirstWorkflow(ref audit, ref error))
                     Assert.Fail(error);
              // Note: Parent name and Parent Phone are required if applicant is under 18.
              audit.SetValue("ApplicantName", "George Jones");
              audit.SetValue("ParentName", "Sue Jones");
              audit.SetValue("ParentPhone", "613-555-1212");
              if (!ws.SaveWorkflow(ref audit, ref error))
                     Assert.Fail(error);
              auditGuid = audit.Guid;
       }
       // Verify that audit is saved to the database.  Login to the website to verify.
       JetfireProfile jp = Init.UserJetfireProfile("George", ref error);
       Assert.AreEqual(String.Empty, error, error);
       using (NexusLogin login = Init.StartLinqStorage(jp, false))
       {
              TjWorkflow audit = null;
              if (!Jf.FindWorkflow(login.Nexus, auditGuid, ref audit))
                     Assert.Fail("Cannot find audit workflow");
              Assert.AreEqual(Jf.GetValue(audit, "ApplicantName"), "George Jones", "ApplicantName does not match");
              Assert.AreEqual(Jf.GetValue(audit, "ParentName"), "Sue Jones", "ParentName does not match");
              Assert.AreEqual(Jf.GetValue(audit, "ParentPhone"), "613-555-1212", "ParentPhone does not match");
       }
}

Unit Test using the raw Web Service

The following code shows a C# .NET Unit test case that runs in a Visual Studio environment with Jetfire Workflows. The specific situation is to add a Member (wrestler) to a Club, set some properties for the member and verify that the wrestler data was saved correctly, i.e. no error received.

Some comments about the unit test example:
  1. There is more heavy lifting required in this example, specifically creating and decoding xml streams.
  2. The Workflow class to decode the xml (used in above example) is reused here. It is recommended that the designer create a 'Workflow' class that is reusable.
  3. The response from the server is overloaded, in the sense that if successful, a workflow is returned, but is not successful, an error message is returned.

[TestMethod]
public void WsOAWA_CreateWrestlerUsingWebService()
{
	string error = "";
	// Create the Workspace and Club
	OawaRegistrationTests tests = new OawaRegistrationTests();
	tests.OawaRegistration_CreateWorkspaceOawaClub();
	// Start the Web Service and create a wrestler
	using (TrackerRealm.Jetfire.WindowsUtil.Jetfire.JetfireWebService ws = new TrackerRealm.Jetfire.WindowsUtil.Jetfire.JetfireWebService())
	{
		// login George, the administrator
		string xml = ws.Login("George", "", "Registration");
		// Response should be:
		// Note: NexusKey is unique for every logon.
		// <?xml version='1.0' encoding='utf-8' ?>
		// <LoginResponse xmlns="http://schemas.Jetfire.ca/LoginResponse">
		//  <LoginKey NexusKey="49c55cb8-908c-4d2b-ba0b-70cfcbe05f16" Subscription="Registration" Roles="Admin" Message="Successful Login"/>
		// </LoginResponse>
		// Dig out nexusKey - code to be written
		string nexusKey = "49c55cb8-908c-4d2b-ba0b-70cfcbe05f16";
		// Find the Club in the Jetfire Nexus
		xml = ws.GetWorkflowBySubject(nexusKey, "OawaClub", "My Club");
		// Response is:
		// <?xml version='1.0' encoding='utf-8' ?>
		// <Workflows xmlns="http://schemas.Jetfire.ca/Workflows">
		//	<Workflow>
		//		<Properties>
		//			<Property Name="Subject" Value="My Club" />
		//			<Property Name="Guid" Value="12c55cb8-128c-122b-1a0b-12cfcbe05f16"/>
		// <-- snip (more properties) -->
		//		</Properties>
		//		<Methods></Methods>
		//	</Workflow>
		// </Workflows>	
		// Dig out club - code to be written (this is a Windows Util class that is used)
		Workflow club = new Workflow(xml, ref error);
		if (club == null)
			Assert.Fail("Error in getting OAWA Club: " + error);
		// Create xml to execute a command against a workflow identified by the Guid
		string command = @"<?xml version='1.0' encoding='utf-8' ?>
			<Workflows xmlns=""http://schemas.Jetfire.ca/Workflows\"">
				<Workflow>
					<Properties>
						<Property Name=""Guid"" Value=""12c55cb8-128c-122b-1a0b-12cfcbe05f16"" />
					</Properties>
					<Methods>
						<Method Name=""AddWrestler"" Value=""AddWrestler"" />
					</Methods>
				</Workflow>
			</Workflows>";
		// Add a wrestler to the club
		xml = ws.ExecuteCommand(nexusKey, command);
		// Response should be:
		// <?xml version='1.0' encoding='utf-8' ?>
		// <Workflows xmlns="http://schemas.Jetfire.ca/Workflows">
		//	<Workflow>
		//		<Properties>
		//			<Property Name="Subject" Value=" OAWA Contact" />
		//			<Property Name="Guid" Value="67c55cb8-678c-672b-1a0b-67cfcbe05f16"/>
		// <-- snip (more properties) -->
		//		</Properties>
		//		<Methods></Methods>
		//	</Workflow>
		// </Workflows>	
		Workflow wrestler = new Workflow(xml, ref error);
		if (wrestler == null)
			Assert.Fail("Error in retrieving OAWA Contact: " + error);
		// Save a property in the wrestler record
		System.DateTime date = new System.DateTime(1995, 12, 1);
		// Create xml for Workflow with Properties being saved. 
		// In this case, update FirstName, LastName, and DOB - the Guid identifies the workflow.
		wrestler.SetValue("DOB", date.ToString(C.DateFormat));
		// Create the xml for updating the properties
		xml = @"<?xml version='1.0' encoding='utf-8' ?>
			<Workflows xmlns=""http://schemas.Jetfire.ca/Workflows"">
				<Workflow>
					<Properties>
						<Property Name=""FirstName"" Value=""George"" />
						<Property Name=""LastName"" Value=""Jones"" />
						<Property Name=""DOB"" Value=""" + date.ToString(C.DateFormat) + @""" />
						<Property Name=""Guid"" Value=""67c55cb8-678c-672b-1a0b-67cfcbe05f16"" />
					</Properties>
				</Workflow>
			</Workflows>";
		xml = ws.SaveWorkflow(nexusKey, xml);
		wrestler = new Workflow(xml, ref error);
		if (wrestler == null)
			Assert.Fail("Error updating the wrestler data: " + error);
		// Verify the DOB is saved correctly.
		string dateValue = wrestler.GetValue("DOB");
		Assert.AreEqual(date.ToString(C.DateFormat), dateValue);
	}
}

http://www.wiebeworks.com - Wiki version 3.0.4.560.