Example #1
0
        public void InitializeExistingState_ThrowsIfAlreadyInitialized()
        {
            // Arrange
            var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());
            var existingState    = new Dictionary <string, byte[]>
            {
                ["MyState"] = new byte[] { 1, 2, 3, 4 }
            };

            applicationState.InitializeExistingState(existingState);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => applicationState.InitializeExistingState(existingState));
        }
Example #2
0
        public void PersistState_ThrowsForDuplicateKeys()
        {
            // Arrange
            var currentState     = new Dictionary <string, byte[]>();
            var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

            applicationState.PersistingState = true;
            var myState = new byte[] { 1, 2, 3, 4 };

            applicationState.PersistAsJson("MyState", myState);

            // Act & Assert
            Assert.Throws <ArgumentException>(() => applicationState.PersistAsJson("MyState", myState));
        }
Example #3
0
        public void PersistAsJson_NullValueAsync()
        {
            // Arrange
            var currentState     = new Dictionary <string, byte[]>();
            var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

            applicationState.PersistingState = true;

            // Act
            applicationState.PersistAsJson <byte[]>("MyState", null);

            // Assert
            Assert.True(currentState.TryGetValue("MyState", out var stored));
            Assert.Null(JsonSerializer.Deserialize <byte[]>(stored));
        }
Example #4
0
        public void PersistState_SavesDataToTheStoreAsync()
        {
            // Arrange
            var currentState     = new Dictionary <string, byte[]>();
            var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

            applicationState.PersistingState = true;
            var myState = new byte[] { 1, 2, 3, 4 };

            // Act
            applicationState.PersistAsJson("MyState", myState);

            // Assert
            Assert.True(currentState.TryGetValue("MyState", out var stored));
            Assert.Equal(myState, JsonSerializer.Deserialize <byte[]>(stored));
        }
Example #5
0
        public void InitializeExistingState_SetupsState()
        {
            // Arrange
            var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());
            var existingState    = new Dictionary <string, byte[]>
            {
                ["MyState"] = JsonSerializer.SerializeToUtf8Bytes(new byte[] { 1, 2, 3, 4 })
            };

            // Act
            applicationState.InitializeExistingState(existingState);

            // Assert
            Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var existing));
            Assert.Equal(new byte[] { 1, 2, 3, 4 }, existing);
        }
Example #6
0
        public void TryRetrieveFromJson_NullValue()
        {
            // Arrange
            var serialized    = JsonSerializer.SerializeToUtf8Bytes <byte[]>(null);
            var existingState = new Dictionary <string, byte[]>()
            {
                ["MyState"] = serialized
            };
            var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());

            applicationState.InitializeExistingState(existingState);

            // Act
            Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var stored));

            // Assert
            Assert.Null(stored);
            Assert.False(applicationState.TryTakeFromJson <byte[]>("MyState", out _));
        }
Example #7
0
        public void TryRetrieveFromJson_DeserializesTheDataFromJson()
        {
            // Arrange
            var myState       = new byte[] { 1, 2, 3, 4 };
            var serialized    = JsonSerializer.SerializeToUtf8Bytes(myState);
            var existingState = new Dictionary <string, byte[]>()
            {
                ["MyState"] = serialized
            };
            var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());

            applicationState.InitializeExistingState(existingState);

            // Act
            Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var stored));

            // Assert
            Assert.Equal(myState, stored);
            Assert.False(applicationState.TryTakeFromJson <byte[]>("MyState", out _));
        }