/** * Stores the items in the cache that will later be asserted * @param cache */ private void storeAssertedRequests(RequestCache cache) { SIF_QueryObject obj = new SIF_QueryObject(""); SIF_Query query = new SIF_Query(obj); SIF_Request request = new SIF_Request(); request.SIF_Query = query; Query q; TestState ts; fMsgIds = new String[10]; fStateObjects = new String[10]; // Add 10 entries to the cache, interspersed with other entries that are removed for (int i = 0; i < 10; i++) { ts = new TestState(); ts.State = Adk.MakeGuid(); fStateObjects[i] = ts.State; q = new Query(StudentDTD.STUDENTPERSONAL); q.UserData = ts; String phantom1 = Adk.MakeGuid(); String phantom2 = Adk.MakeGuid(); storeRequest(cache, request, q, phantom1, "foo"); fMsgIds[i] = Adk.MakeGuid(); storeRequest(cache, request, q, fMsgIds[i], "Object_" + i.ToString()); storeRequest(cache, request, q, phantom2, "bar"); cache.GetRequestInfo(phantom1, null); cache.GetRequestInfo(phantom2, null); } }
public void testPersistenceWithRemoval() { fRC = RequestCache.GetInstance(fAgent); SIF_QueryObject obj = new SIF_QueryObject(""); SIF_Query query = new SIF_Query(obj); SIF_Request request = new SIF_Request(); request.SIF_Query = query; Query q = new Query(StudentDTD.STUDENTPERSONAL); String testStateItem = Adk.MakeGuid(); TestState ts = new TestState(); ts.State = testStateItem; q.UserData = ts; fMsgIds = new String[10]; // Add 10 entries to the cache, interspersed with other entries that are removed for (int i = 0; i < 10; i++) { String phantom1 = Adk.MakeGuid(); String phantom2 = Adk.MakeGuid(); storeRequest(fRC, request, q, phantom1, "foo"); fMsgIds[i] = Adk.MakeGuid(); storeRequest(fRC, request, q, fMsgIds[i], "Object_" + i); storeRequest(fRC, request, q, phantom2, "bar"); fRC.GetRequestInfo(phantom1, null); fRC.GetRequestInfo(phantom2, null); } // remove every other entry, close, re-open and assert that the correct entries are there for (int i = 0; i < 10; i += 2) { fRC.GetRequestInfo(fMsgIds[i], null); } Assertion.AssertEquals("Before closing Should have five objects", 5, fRC.ActiveRequestCount); fRC.Close(); // Create a new instance. This one should retrieve its settings from the persistence mechanism fRC = RequestCache.GetInstance(fAgent); Assertion.AssertEquals("After Re-Openeing Should have five objects", 5, fRC.ActiveRequestCount); for (int i = 1; i < 10; i += 2) { IRequestInfo cachedInfo = fRC.GetRequestInfo(fMsgIds[i], null); Assertion.AssertNotNull("No cachedID returned for " + i, cachedInfo); } Assertion.AssertEquals("Should have zero objects", 0, fRC.ActiveRequestCount); }
// TODO: Implement /* * [Test] * public void testReportPublishSIFExceptionAfterReportInfo() * { * ElementDef objType = SifDtd.SIF_REPORTOBJECT; * ErrorMessageHandler handler = new ErrorMessageHandler( ErrorMessageHandler.BVR_SET_REPORT_INFO_THROW_EXCEPTION ); * fZone.setReportPublisher( handler, ADKFlags.PROV_NONE); * * TestProtocolHandler testProto = new TestProtocolHandler(); * testProto.open(fZone); * MessageDispatcher testDispatcher = new MessageDispatcher( fZone ); * fZone.setDispatcher(testDispatcher); * fZone.setProto(testProto); * testDispatcher.dispatch( createSIF_Request( objType, ADK.makeGUID(), fZone ) ); * String msg = testProto.readMsg(); * assertNull(testProto.readMsg()); * fZone.log.info(msg); * * SIFParser parser = SIFParser.newInstance(); * SIFElement element = parser.parse(new StringReader(msg), fZone); * assertTrue(element instanceof SIF_Response); * SIF_Response response = (SIF_Response) element; * assertTrue(response.getSIF_Error() != null); * assertTrue(response.getSIF_Error().getSIF_Desc().startsWith("Blah")); * } * * public void testReportPublishSIFExceptionAfterReportInfo() throws ADKException, IOException * { * ElementDef objType = SifDtd.SIF_REPORTOBJECT; * ErrorMessageHandler handler = new ErrorMessageHandler( ErrorMessageHandler.BVR_SET_REPORT_INFO_THROW_EXCEPTION ); * fZone.setReportPublisher( handler, ADKFlags.PROV_NONE); * * TestProtocolHandler testProto = new TestProtocolHandler(); * testProto.open(fZone); * MessageDispatcher testDispatcher = new MessageDispatcher( fZone ); * fZone.setDispatcher(testDispatcher); * fZone.setProto(testProto); * testDispatcher.dispatch( createSIF_Request( objType, ADK.makeGUID(), fZone ) ); * String msg = testProto.readMsg(); * assertNull(testProto.readMsg()); * fZone.log.info(msg); * * SIFParser parser = SIFParser.newInstance(); * SIFElement element = parser.parse(new StringReader(msg), fZone); * assertTrue(element instanceof SIF_Response); * SIF_Response response = (SIF_Response) element; * assertTrue(response.getSIF_Error() != null); * assertTrue(response.getSIF_Error().getSIF_Desc().startsWith("Blah")); * } * */ private SIF_Request createSIF_Request(IElementDef objType) { SIF_Request request = new SIF_Request(); request.Header.SIF_MsgId = MSG_GUID; request.Header.SIF_SourceId = "foo"; request.SIF_MaxBufferSize = 32768; request.AddSIF_Version(new SIF_Version(Adk.SifVersion.ToString())); SIF_Query q = new SIF_Query(); SIF_QueryObject sqo = new SIF_QueryObject(); sqo.ObjectName = objType.Name; q.SIF_QueryObject = sqo; request.SIF_Query = q; return(request); }
public void testPersistenceWithBadState() { //create new cache for agent RequestCache cache = RequestCache.GetInstance(fAgent); //create new queryobject SIF_QueryObject obj = new SIF_QueryObject(""); //create query, telling it what type of query it is(passing it queryobj) SIF_Query query = new SIF_Query(obj); //create new sif request SIF_Request request = new SIF_Request(); //set query property request.SIF_Query = query; Query q = new Query(StudentDTD.STUDENTPERSONAL); String testStateItem = Adk.MakeGuid(); String requestMsgId = Adk.MakeGuid(); String testObjectType = Adk.MakeGuid(); TestState ts = new TestState(); ts.State = testStateItem; ts.setCreateErrorOnRead(true); q.UserData = ts; storeRequest(cache, request, q, requestMsgId, testObjectType); cache.Close(); // Create a new instance. This one should retrieve its settings from the persistence mechanism cache = RequestCache.GetInstance(fAgent); IRequestInfo ri = cache.GetRequestInfo(requestMsgId, null); //if state is null, should still return ri object Assertion.AssertNotNull("RequestInfo was null", ri); Assertion.AssertEquals("MessageId", requestMsgId, ri.MessageId); Assertion.AssertEquals("ObjectType", testObjectType, ri.ObjectType); ts = (TestState) ri.UserData; // In order for this to be a valid test, the TestState class should have thrown // an exception during deserialization and should be null here. Assertion.AssertNull("UserData should be null", ts); }
/** * Creates a SIF_Query element from the specified Adk query object using * the specified version of SIF * @param query The Query to convert to a SIF_Query * @param version The version of SIF to render the SIF_Query xml in * @param allowFieldRestrictions True if the field restrictions in the query should be rendered * @return a SIF_Query object */ public static SIF_Query CreateSIF_Query(Query query, SifVersion version, bool allowFieldRestrictions) { SIF_QueryObject sqo = new SIF_QueryObject(query.ObjectType.Tag(version)); SIF_Query sifQ = new SIF_Query(sqo); if (query.HasConditions) { sifQ.SIF_ConditionGroup = createConditionGroup(query, version); } if (allowFieldRestrictions && query.HasFieldRestrictions) { foreach (ElementRef elementRef in query.FieldRestrictionRefs) { String path = null; IElementDef field = elementRef.Field; if (field != null) { if (!field.IsSupported(version)) { continue; } path = field.GetSQPPath(version); } if (path == null) { path = elementRef.XPath; } if (path != null) { path = Adk.Dtd.TranslateSQP(query.ObjectType, path, version); sqo.AddSIF_Element(new SIF_Element(path)); } } } return(sifQ); }
/// <summary> Constructs a Query object from a SIF_QueryObject. /// /// This constructor is not typically called by agents but is used internally /// by the class framework. The other constructors can be used to safely /// create Query instances to request a specific SIF Data Object. Use the /// <c>addCondition</c> and <c>setFieldRestrictions</c> methods /// to further define the conditions and SIF elements specified by the query. /// /// </summary> /// <param name="query">A SIF_Query object received in a SIF_Request message /// </param> /// <exception cref="AdkUnknownOperatorException">If one of the operators in the SIF_Query is /// unrecognized by the ADK</exception> /// <exception cref="AdkSchemaException">If the object or elements defined in the query or /// not recognized by the ADK </exception> public Query(SIF_Query query) { SIF_QueryObject qo = query.SIF_QueryObject; if (qo == null) { throw new ArgumentException("SIF_Query must have a SIF_QueryObject element"); } fObjType = Adk.Dtd.LookupElementDef(qo.ObjectName); if (fObjType == null) { throw new AdkSchemaException (qo.ObjectName + " is not a recognized SIF Data Object, or the agent is not configured to support this object type"); } fRoot = null; SIF_ConditionGroup cg = query.SIF_ConditionGroup; if (cg != null && cg.GetSIF_Conditionses() != null) { GroupOperator grpOp; try { grpOp = Condition.ParseGroupOperator(cg.Type); } catch (AdkUnknownOperatorException) { grpOp = GroupOperator.None; } fRoot = new ConditionGroup(grpOp); SIF_Conditions[] sifConds = cg.GetSIF_Conditionses(); if (sifConds.Length == 1) { // There is one SIF_ConditionGroup with one SIF_Conditions, // so just add all of the conditions (no nested groups) string typ = sifConds[0].Type; if (typ == null) { throw new AdkSchemaException ("SIF_Conditions/@Type is a required attribute"); } fRoot.fOp = Condition.ParseGroupOperator(typ); SIF_Condition[] clist = sifConds[0].GetSIF_Conditions(); PopulateConditions(query, clist, fRoot); } else { // There are multiple SIF_Conditions, so add each as a nested // ConditionGroup of the fRoot for (int i = 0; i < sifConds.Length; i++) { ConditionGroup nested = new ConditionGroup(Condition.ParseGroupOperator(sifConds[i].Type)); PopulateConditions(query, sifConds[i].GetSIF_Conditions(), nested); fRoot.AddGroup(nested); } } } SifVersion[] reqVersions = null; // First, try to get the version from the SIF_Request Element parent = query.Parent; if (parent != null) { if (parent is SIF_Request) { SIF_Request request = (SIF_Request)parent; SifVersion[] versions = request.parseRequestVersions(Adk.Log); if (versions.Length > 0) { reqVersions = versions; } } } if (reqVersions == null) { SifVersion version = query.EffectiveSIFVersion; if (version != null) { reqVersions = new SifVersion[] { version }; } } if (reqVersions == null || reqVersions.Length == 0) { throw new ArgumentException( "SIF_Query is not contained in a SIF_Request that has a SIF_Version element; cannot determine version of SIF to associated with this Query object"); } else { fVersions = reqVersions; } SIF_Element[] fields = query.SIF_QueryObject.GetSIF_Elements(); if (fields != null && fields.Length > 0) { for (int i = 0; i < fields.Length; i++) { string xPath = fields[i].TextValue; if (xPath == null || xPath.Length == 0) { continue; } AddFieldRestriction(xPath); } } }
///<summary>Adds the value of the <c><SIF_Query></c> element.</summary> /// <param name="SifQueryObject">A SIF_QueryObject</param> ///<remarks> /// <para>This form of <c>setSIF_Query</c> is provided as a convenience method /// that is functionally equivalent to the method <c>AddSIF_Query</c></para> /// <para>Version: 2.6</para> /// <para>Since: 1.5r1</para> /// </remarks> public void AddSIF_Query(SIF_QueryObject SifQueryObject) { AddChild(ReportingDTD.SIF_QUERYGROUP_SIF_QUERY, new SIF_Query(SifQueryObject)); }
public void testWithLegacyFile() { //assertStoredRequests(fRC, true); // Copy the legacy requests.adk file to the agent work directory //FileInfo legacyFile = new FileInfo("requests.adk"); //Assertion.Assert("Saved legacy file does [not?] exist", legacyFile.Exists); //FileInfo copiedFile = new FileInfo(fAgent.HomeDir + Path.DirectorySeparatorChar + "work" + Path.DirectorySeparatorChar + "requests.adk"); //if (copiedFile.Exists) //{ // copiedFile.Delete(); //} //// Copy the file //legacyFile.CopyTo(copiedFile.FullName, true); // Now open up an instance of the request cache and verify that the contents are there fRC = RequestCache.GetInstance(fAgent); SIF_QueryObject obj = new SIF_QueryObject(""); SIF_Query query = new SIF_Query(obj); SIF_Request request = new SIF_Request(); request.SIF_Query = query; Query q; TestState ts; fMsgIds = new String[10]; fStateObjects = new String[10]; // Add 10 entries to the cache for (int i = 0; i < 10; i++) { ts = new TestState(); ts.State = Adk.MakeGuid(); fStateObjects[i] = (String) ts.State; q = new Query(StudentDTD.STUDENTPERSONAL); q.UserData = ts; fMsgIds[i] = Adk.MakeGuid(); storeRequest(fRC, request, q, fMsgIds[i], "Object_" + i.ToString()); } Assertion.AssertEquals("Active request count", 10, fRC.ActiveRequestCount); // Lookup each setting, for (int i = 0; i < 10; i++) { IRequestInfo reqInfo = fRC.LookupRequestInfo(fMsgIds[i], null); Assertion.AssertEquals("Initial lookup", "Object_" + i.ToString(), reqInfo.ObjectType); } // Lookup each setting, for (int i = 0; i < 10; i++) { IRequestInfo reqInfo = fRC.GetRequestInfo(fMsgIds[i], null); Assertion.AssertEquals("Initial lookup", "Object_" + i.ToString(), reqInfo.ObjectType); } // all messages should now be removed from the queue Assertion.AssertEquals("Cache should be empty", 0, fRC.ActiveRequestCount); // Now run one of our other tests testPersistence(); }