コード例 #1
0
        public async Task CreateAsync_WithEmptyNameShouldThrowArgumentNullException()
        {
            MapperInitializer.InitializeMapper();
            var context                      = ApplicationDbContextInMemoryFactory.InitializeContext();
            var roadObjectService            = new RoadObjectService(context);
            var createRoadObjectServiceModel = new CreateRoadObjectServiceModel();
            var roadObjectName               = "  ";

            createRoadObjectServiceModel.Name = roadObjectName;

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await roadObjectService.CreateAsync(createRoadObjectServiceModel);
            });
        }
コード例 #2
0
        public async Task CreateAsync_WithOverMaxNameLengthShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context                      = ApplicationDbContextInMemoryFactory.InitializeContext();
            var roadObjectService            = new RoadObjectService(context);
            var createRoadObjectServiceModel = new CreateRoadObjectServiceModel();
            var roadObjectName               = "qwertyuiop qwertyuiop qwertyuiop qwertyuiop qwertyuiop";

            createRoadObjectServiceModel.Name = roadObjectName;
            var message = "Road object's name cannot be more than 50 characters.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await roadObjectService.CreateAsync(createRoadObjectServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
コード例 #3
0
        public async Task CreateAsync_ShouldSuccessfullyCreate()
        {
            MapperInitializer.InitializeMapper();
            var context                      = ApplicationDbContextInMemoryFactory.InitializeContext();
            var roadObjectService            = new RoadObjectService(context);
            var createRoadObjectServiceModel = new CreateRoadObjectServiceModel();
            var roadObjectName               = "RON 1";

            createRoadObjectServiceModel.Name = roadObjectName;

            await roadObjectService.CreateAsync(createRoadObjectServiceModel);

            var expectedResult = roadObjectName;
            var actualResult   = roadObjectService
                                 .All()
                                 .First()
                                 .Name;

            Assert.True(expectedResult == actualResult);
        }
コード例 #4
0
        public async Task CreateAsync_WithExistingNameShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var roadObjectService            = new RoadObjectService(context);
            var createRoadObjectServiceModel = new CreateRoadObjectServiceModel();
            var roadObjectName = "RON 1";

            createRoadObjectServiceModel.Name = roadObjectName;
            var message = "Road object's name already exists.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await roadObjectService.CreateAsync(createRoadObjectServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
コード例 #5
0
        public async Task CreateAsync(CreateRoadObjectServiceModel createRoadObjectServiceModel)
        {
            var roadObject = AutoMapperConfig.MapperInstance.Map <RoadObject>(createRoadObjectServiceModel);

            if (string.IsNullOrWhiteSpace(roadObject.Name))
            {
                throw new ArgumentNullException(EmptyRoadObjectErrorMessage);
            }

            if (await this.context.RoadObjects.AnyAsync(ro => ro.Name == roadObject.Name))
            {
                throw new InvalidOperationException(RoadObjectExistErrorMessage);
            }

            if (roadObject.Name.Length > AttributesConstraints.RoadObjectNameMaxLength)
            {
                throw new InvalidOperationException(string.Format(RoadObjectNameMaxLengthErrorMessage, AttributesConstraints.RoadObjectNameMaxLength));
            }

            await this.context.RoadObjects.AddAsync(roadObject);

            await this.context.SaveChangesAsync();
        }