Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EntitySetLinkConfigurationTest.cs « Builder « OData « System.Web.Http.OData.Test « test - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c24ba7a58101d8fbf973116fc8aedd5221b6960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Linq;
using System.Net.Http;
using System.Web.Http.Routing;
using Microsoft.Data.Edm;
using Microsoft.TestCommon;

namespace System.Web.Http.OData.Builder
{
    public class EntitySetLinkConfigurationTest
    {
        [Fact]
        public void CanConfigureAllLinksViaEditLink()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink = "http://server/service/Products(15)";

            var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
            products.HasEditLink(c => new Uri(
                string.Format(
                    "http://server/service/Products({0})",
                    c.EntityInstance.ID
                )
            ));

            var actor = builder.EntitySets.Single();
            var model = builder.GetEdmModel();
            var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
            var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
            var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
            var entityContext = new EntityInstanceContext { EdmModel = model, EntitySet = productsSet, EntityType = productType, EntityInstance = productInstance, UrlHelper = new UrlHelper(new HttpRequestMessage()) };
            var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);

            // Act
            var editLinkUri = entitySetLinkBuilderAnnotation.BuildEditLink(entityContext);
            var readLinkUri = entitySetLinkBuilderAnnotation.BuildReadLink(entityContext);
            var idLink = entitySetLinkBuilderAnnotation.BuildIdLink(entityContext);

            // Assert
            Assert.NotNull(editLinkUri);
            Assert.Equal(expectedEditLink, editLinkUri.ToString());
            Assert.NotNull(readLinkUri);
            Assert.Equal(expectedEditLink, readLinkUri.ToString());
            Assert.NotNull(idLink);
            Assert.Equal(expectedEditLink, idLink);
        }

        [Fact]
        public void CanConfigureLinksIndependently()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink = "http://server1/service/Products(15)";
            var expectedReadLink = "http://server2/service/Products/15";
            var expectedIdLink = "http://server3/service/Products(15)";

            var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
            products.HasEditLink(c => new Uri(
                string.Format(
                    "http://server1/service/Products({0})",
                    c.EntityInstance.ID
                )
            ));
            products.HasReadLink(c => new Uri(
                string.Format(
                    "http://server2/service/Products/15",
                    c.EntityInstance.ID
                )
            ));
            products.HasIdLink(c =>
                string.Format(
                    "http://server3/service/Products({0})",
                    c.EntityInstance.ID
                )
            );

            var actor = builder.EntitySets.Single();
            var model = builder.GetEdmModel();
            var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
            var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
            var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
            var entityContext = new EntityInstanceContext { EdmModel = model, EntitySet = productsSet, EntityType = productType, EntityInstance = productInstance, UrlHelper = new UrlHelper(new HttpRequestMessage()) };

            // Act
            var editLink = actor.GetEditLink()(entityContext);
            var readLink = actor.GetReadLink()(entityContext);
            var idLink = actor.GetIdLink()(entityContext);

            // Assert
            Assert.NotNull(editLink);
            Assert.Equal(expectedEditLink, editLink.ToString());
            Assert.NotNull(readLink);
            Assert.Equal(expectedReadLink, readLink.ToString());
            Assert.NotNull(idLink);
            Assert.Equal(expectedIdLink, idLink);
        }

        [Fact]
        public void FailingToConfigureLinksResultsInNullLinks()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var actor = builder.EntitySets.Single();
            var model = builder.GetEdmModel();

            // Act & Assert
            Assert.Null(actor.GetEditLink());
            Assert.Null(actor.GetReadLink());
            Assert.Null(actor.GetIdLink());
        }

        private ODataModelBuilder GetCommonModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
            var product = products.EntityType;
            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.Price);
            product.Property(p => p.Cost);
            return builder;
        }

        class EntitySetLinkConfigurationTest_Product
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public Decimal Price { get; set; }
            public Decimal Cost { get; set; }
        }
    }
}