Beispiel #1
0
        public StateLRSResponse RetrieveState(String id, Activity activity, Agent agent, Nullable <Guid> registration = null)
        {
            var r = new StateLRSResponse();

            var queryParams = new Dictionary <String, String>();

            queryParams.Add("stateId", id);
            queryParams.Add("activityId", activity.id.ToString());
            queryParams.Add("agent", agent.ToJSON(version));

            var state = new StateDocument();

            state.id       = id;
            state.activity = activity;
            state.agent    = agent;

            if (registration != null)
            {
                queryParams.Add("registration", registration.ToString());
                state.registration = registration;
            }

            var resp = GetDocument("activities/state", queryParams, state);

            if (resp.status != HttpStatusCode.OK && resp.status != HttpStatusCode.NotFound)
            {
                r.success       = false;
                r.httpException = resp.ex;
                r.SetErrMsgFromBytes(resp.content);
                return(r);
            }
            r.success = true;

            return(r);
        }
Beispiel #2
0
        public async Task can_delete_existing_state()
        {
            // Arrange
            var state = new StateDocument <string>()
            {
                Content = "foo"
            };
            var request = DeleteStateRequest.Create(state);

            request.ActivityId = new Uri(ACTIVITY_ID);
            request.Agent      = new Agent()
            {
                Name = AGENT_NAME,
                MBox = new Uri(AGENT_MBOX)
            };
            request.Registration = REGISTRATION;
            request.StateId      = STATE_ID;
            this._mockHttp
            .When(HttpMethod.Delete, this.GetApiUrl("activities/state"))
            .WithQueryString("activityId", ACTIVITY_ID)
            .WithQueryString("agent", AGENT_QS)
            .WithQueryString("registration", REGISTRATION.ToString())
            .WithQueryString("stateId", STATE_ID)
            .With(x => x.Headers.IfNoneMatch.Count == 0 && x.Headers.IfMatch.Count == 0)
            .Respond(HttpStatusCode.NoContent);

            // Act
            bool result = await this._client.States.Delete(request);

            // Assert
            result.Should().BeTrue();
        }
Beispiel #3
0
        public async Task can_get_state_with_string_document()
        {
            // Arrange
            var request = new GetStateRequest()
            {
                ActivityId = new Uri(ACTIVITY_ID),
                Agent      = new Agent()
                {
                    Name = AGENT_NAME,
                    MBox = new Uri(AGENT_MBOX)
                },
                Registration = REGISTRATION,
                StateId      = STATE_ID
            };

            this._mockHttp
            .When(HttpMethod.Get, this.GetApiUrl("activities/state"))
            .WithQueryString("activityId", ACTIVITY_ID)
            .WithQueryString("agent", AGENT_QS)
            .WithQueryString("registration", REGISTRATION.ToString())
            .WithQueryString("stateId", STATE_ID)
            .Respond(this.GetStateResponseMessage());

            // Act
            StateDocument <string> state = await this._client.States.Get <string>(request);

            // Assert
            state.Should().NotBeNull();
            state.ETag.Should().Be(ETAG);
            state.LastModified.Should().Be(LAST_MODIFIED);
            state.Content.Should().NotBeNullOrEmpty();
        }
Beispiel #4
0
        public async Task cannot_delete_existing_state_with_invalid_etag()
        {
            // Arrange
            var state = new StateDocument <string>()
            {
                Content = "foo",
                ETag    = ETAG
            };
            var request = DeleteStateRequest.Create(state);

            request.ActivityId = new Uri(ACTIVITY_ID);
            request.Agent      = new Agent()
            {
                Name = AGENT_NAME,
                MBox = new Uri(AGENT_MBOX)
            };
            request.Registration = REGISTRATION;
            request.StateId      = STATE_ID;
            this._mockHttp
            .When(HttpMethod.Delete, this.GetApiUrl("activities/state"))
            .WithQueryString("activityId", ACTIVITY_ID)
            .WithQueryString("agent", AGENT_QS)
            .WithQueryString("registration", REGISTRATION.ToString())
            .WithQueryString("stateId", STATE_ID)
            .WithHeaders("If-Match", ETAG)
            .Respond(HttpStatusCode.PreconditionFailed);

            // Act
            bool result = await this._client.States.Delete(request);

            // Assert
            result.Should().BeFalse();
        }
        public async Task TestDeleteState()
        {
            var doc = new StateDocument();

            doc.activity = Support.activity;
            doc.agent    = Support.agent;
            doc.id       = "test";

            LRSResponse lrsRes = await lrs.DeleteState(doc);

            Assert.True(lrsRes.success);
        }
Beispiel #6
0
        public void TestDeleteState()
        {
            var doc = new StateDocument
            {
                Activity = Support.Activity,
                Agent    = Support.Agent,
                ID       = "test"
            };

            var lrsRes = _lrs.DeleteState(doc);

            Assert.IsTrue(lrsRes.Success);
        }
Beispiel #7
0
        public async Task TestDeleteStateAsync()
        {
            var doc = new StateDocument
            {
                Activity = Support.Activity,
                Agent    = Support.Agent,
                Id       = "test"
            };

            var lrsRes = await _lrs.DeleteStateAsync(doc);

            Assert.IsTrue(lrsRes.Success);
        }
        public async Task TestSaveState()
        {
            var doc = new StateDocument();

            doc.activity = Support.activity;
            doc.agent    = Support.agent;
            doc.id       = "test";
            doc.content  = System.Text.Encoding.UTF8.GetBytes("Test value");

            LRSResponse lrsRes = await lrs.SaveState(doc);

            Assert.True(lrsRes.success);
        }
Beispiel #9
0
        public async Task TestSaveStateAsync()
        {
            var doc = new StateDocument
            {
                Activity = Support.Activity,
                Agent    = Support.Agent,
                Id       = "test",
                Content  = System.Text.Encoding.UTF8.GetBytes("Test value")
            };

            var lrsRes = await _lrs.SaveStateAsync(doc);

            Assert.IsTrue(lrsRes.Success);
        }
Beispiel #10
0
        public LRSResponse DeleteState(StateDocument state)
        {
            var queryParams = new Dictionary <String, String>();

            queryParams.Add("stateId", state.id);
            queryParams.Add("activityId", state.activity.id.ToString());
            queryParams.Add("agent", state.agent.ToJSON(version));
            if (state.registration != null)
            {
                queryParams.Add("registration", state.registration.ToString());
            }

            return(DeleteDocument("activities/state", queryParams));
        }
Beispiel #11
0
        public void TestSaveState()
        {
            var doc = new StateDocument
            {
                Activity = Support.Activity,
                Agent    = Support.Agent,
                ID       = "test",
                Content  = Encoding.UTF8.GetBytes("Test value")
            };

            var lrsRes = _lrs.SaveState(doc);

            Assert.IsTrue(lrsRes.Success);
        }
Beispiel #12
0
        public async Task <LRSResponse> SaveState(StateDocument state)
        {
            var queryParams = new Dictionary <string, string>();

            queryParams.Add("stateId", state.id);
            queryParams.Add("activityId", state.activity.id);
            queryParams.Add("agent", state.agent.ToJSON(version));

            if (state.registration != null)
            {
                queryParams.Add("registration", state.registration.ToString());
            }

            return(await SaveDocument("activities/state", queryParams, state));
        }
Beispiel #13
0
        public async Task <LrsResponse> DeleteStateAsync(StateDocument state)
        {
            var queryParams = new Dictionary <string, string>
            {
                { "stateId", state.Id },
                { "activityId", state.Activity.Id },
                { "agent", state.Agent.ToJson(Version) }
            };

            if (state.Registration != null)
            {
                queryParams.Add("registration", state.Registration.ToString());
            }

            return(await DeleteDocument("activities/state", queryParams));
        }
Beispiel #14
0
        /// <summary>
        /// Deletes the state.
        /// </summary>
        /// <param name="activityId">The activity identifier.</param>
        /// <param name="agent">The agent.</param>
        /// <param name="stateId">The state identifier.</param>
        /// <param name="registration">The registration.</param>
        /// <param name="stateVal">The state value.</param>
        /// <param name="matchHash">The match hash.</param>
        /// <param name="noneMatchHash">The none match hash.</param>
        /// <returns>Task&lt;LRSResponse&gt;.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task <LRSResponse> DeleteState(string activityId, Agent agent, string stateId, Guid?registration, string stateVal, string matchHash,
                                                    string noneMatchHash)
        {
            var activity = new Activity {
                ID = activityId
            };

            var doc = new StateDocument
            {
                Activity     = activity,
                Agent        = agent,
                ID           = stateId,
                Content      = Encoding.UTF8.GetBytes(stateVal),
                Registration = registration
            };

            return(await _lrs.DeleteStateAsync(doc));
        }
        public void TestExtendedParameters()
        {
            // RemoteLRS doesn't provide a helpful interface for testing
            // that we successfully altered the request URL, but this test
            // is helpful in manual testing and it at least ensures that
            // specifying values in extended doesn't cause errors.
            lrs.extended.Add("test", "param");
            var doc = new StateDocument();

            doc.activity = Support.activity;
            doc.agent    = Support.agent;
            doc.id       = "test";
            doc.content  = System.Text.Encoding.UTF8.GetBytes("Test value");

            LRSResponse lrsRes = lrs.SaveState(doc);

            Assert.IsTrue(lrsRes.success);
        }
Beispiel #16
0
        public async Task TestExtendedParameters()
        {
            // RemoteLRS doesn't provide a helpful interface for testing
            // that we successfully altered the request URL, but this test
            // is helpful in manual testing and it at least ensures that
            // specifying values in extended doesn't cause errors.
            _lrs.Extended.Add("test", "param");
            var doc = new StateDocument
            {
                Activity = Support.Activity,
                Agent    = Support.Agent,
                Id       = "test",
                Content  = System.Text.Encoding.UTF8.GetBytes("Test value")
            };

            var lrsRes = await _lrs.SaveStateAsync(doc);

            Assert.IsTrue(lrsRes.Success);
        }
Beispiel #17
0
        public async Task <StateLrsResponse> RetrieveStateAsync(string id, Activity activity, Agent agent,
                                                                Guid?registration = null)
        {
            var r = new StateLrsResponse();

            var queryParams = new Dictionary <string, string>
            {
                { "stateId", id },
                { "activityId", activity.Id },
                { "agent", agent.ToJson(Version) }
            };

            var state = new StateDocument
            {
                Id       = id,
                Activity = activity,
                Agent    = agent
            };

            if (registration != null)
            {
                queryParams.Add("registration", registration.ToString());
                state.Registration = registration;
            }

            var resp = await GetDocument("activities/state", queryParams, state);

            if (resp.Status != HttpStatusCode.OK && resp.Status != HttpStatusCode.NotFound)
            {
                r.Success       = false;
                r.HttpException = resp.Ex;
                r.SetErrMsgFromBytes(resp.Content);
                return(r);
            }
            r.Success = true;
            r.Content = state;

            return(r);
        }
Beispiel #18
0
        async Task <StateDocument <T> > IStatesApi.Get <T>(GetStateRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            request.Validate();

            var options = new RequestOptions(ENDPOINT);

            this.CompleteOptions(options, request);

            HttpResponseMessage response = await this._client.GetJson(options);

            T content = await response.Content.ReadAsAsync <T>(new[] { new StrictJsonMediaTypeFormatter() });

            var document = new StateDocument <T>();

            document.ETag         = response.Headers.ETag?.Tag;
            document.LastModified = response.Content.Headers.LastModified;
            document.Content      = content;

            return(document);
        }
Beispiel #19
0
 /// <summary>
 /// Creates a new request instance using an existing document
 /// to handle concurrency operations.
 /// </summary>
 /// <param name="existingState">The document used for concurrency comparisons.</param>
 /// <returns></returns>
 public static DeleteStateRequest Create <T>(StateDocument <T> existingState)
 {
     return(new DeleteStateRequest(existingState?.ETag));
 }
Beispiel #20
0
 /// <summary>
 /// Creates a new request instance using an existing document
 /// to handle concurrency operations.
 /// </summary>
 /// <typeparam name="T">
 /// The type of the document to be stored as JSON in the LRS.
 /// The type must support JSON serialization.
 /// </typeparam>
 /// <param name="state">The document used for concurrency comparisons.</param>
 /// <returns></returns>
 public static PutStateRequest <T> Create <T>(StateDocument <T> state)
 {
     return(new PutStateRequest <T>(state));
 }