<commit_before>/**
 * Copyright 2015 <NAME>, d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;
using System.Web.Http.Results;
using biz.dfch.CS.Entity.LifeCycleManager.Context;
﻿using biz.dfch.CS.Entity.LifeCycleManager.Controller;
using biz.dfch.CS.Entity.LifeCycleManager.Model;
using biz.dfch.CS.Entity.LifeCycleManager.UserData;
﻿using biz.dfch.CS.Entity.LifeCycleManager.Util;
﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
﻿using Telerik.JustMock;

namespace biz.dfch.CS.Entity.LifeCycleManager.Tests.Controller
{
    [TestClass]
    public class StateChangeLocksControllerTest
    {
        private StateChangeLocksController _stateChangeLocksController;
        private LifeCycleContext _lifeCycleContext;
        private const String STATE_CHANGE_LOCK_READ_PERMISSION = "CumulusCore:StateChangeLockCanRead";
        private const String STATE_CHANGE_LOCK_CREATE_PERMISSION = "CumulusCore:StateChangeLockCanCreate";
        private const String STATE_CHANGE_LOCK_DELETE_PERMISSION = "CumulusCore:StateChangeLockCanDelete";
        private const String CURRENT_USER_ID = "currentUser";
        private const String ANOTHER_USER_ID = "anotherUser";
        private const String ENTITY_TYPE = "TestEntity";

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {
            Mock.SetupStatic(typeof(ODataControllerHelper));
            Mock.SetupStatic(typeof(CurrentUserDataProvider));
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _stateChangeLocksController = new StateChangeLocksController();
            _lifeCycleContext = Mock.Create<LifeCycleContext>();
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithReadPermissionReturnsHisStateChangeLocks()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(true)
                .MustBeCalled();
            Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUserId())
                .Returns(CURRENT_USER_ID)
                .MustBeCalled();
            Mock.Arrange(() => _lifeCycleContext.StateChangeLocks)
                .IgnoreInstance()
                .ReturnsCollection(CreateSampleStateChangeLockDbSetForUser(CURRENT_USER_ID))
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(new ODataQueryOptions<StateChangeLock>(context, request)).Result;

            Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult<IEnumerable<StateChangeLock>>));

            var response = actionResult as OkNegotiatedContentResult<IEnumerable<StateChangeLock>>;
            Assert.IsNotNull(response);
            Assert.AreEqual(2, response.Content.Count());

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
            Mock.Assert(() => CurrentUserDataProvider.GetCurrentUserId());
            Mock.Assert(_lifeCycleContext);
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithoutReadPermissionReturnsForbidden()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(false)
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(new ODataQueryOptions<StateChangeLock>(context, request)).Result;

            Assert.IsTrue(actionResult.GetType() == typeof(StatusCodeResult));
            var response = (StatusCodeResult)actionResult;
            Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode);

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithoutStateChangeLocksReturnsEmptyList()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(true)
                .MustBeCalled();
            Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUserId())
                .Returns(CURRENT_USER_ID)
                .MustBeCalled();
            Mock.Arrange(() => _lifeCycleContext.StateChangeLocks)
                .IgnoreInstance()
                .ReturnsCollection(CreateSampleStateChangeLockDbSetForUser(ANOTHER_USER_ID))
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(new ODataQueryOptions<StateChangeLock>(context, request)).Result;

            Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult<IEnumerable<StateChangeLock>>));

            var response = actionResult as OkNegotiatedContentResult<IEnumerable<StateChangeLock>>;
            Assert.IsNotNull(response);
            Assert.AreEqual(0, response.Content.Count());

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
            Mock.Assert(() => CurrentUserDataProvider.GetCurrentUserId());
            Mock.Assert(_lifeCycleContext);
        }

        [TestMethod]
        public void PostStateChangeLockForUserWithoutCreatePermissionReturnsForbidden()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_CREATE_PERMISSION))
                .Returns(false)
                .MustBeCalled();

            var actionResult = _stateChangeLocksController.Post(
                new StateChangeLock
                {
                    Id = 1,<commit_msg>minor<commit_after>/**
 * Copyright 2015 Mar<NAME>, d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;
using System.Web.Http.Results;
using biz.dfch.CS.Entity.LifeCycleManager.Context;
﻿using biz.dfch.CS.Entity.LifeCycleManager.Controller;
using biz.dfch.CS.Entity.LifeCycleManager.Model;
using biz.dfch.CS.Entity.LifeCycleManager.UserData;
﻿using biz.dfch.CS.Entity.LifeCycleManager.Util;
﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
﻿using Telerik.JustMock;

namespace biz.dfch.CS.Entity.LifeCycleManager.Tests.Controller
{
    [TestClass]
    public class StateChangeLocksControllerTest
    {
        private StateChangeLocksController _stateChangeLocksController;
        private LifeCycleContext _lifeCycleContext;
        private const String STATE_CHANGE_LOCK_READ_PERMISSION = "CumulusCore:StateChangeLockCanRead";
        private const String STATE_CHANGE_LOCK_CREATE_PERMISSION = "CumulusCore:StateChangeLockCanCreate";
        private const String STATE_CHANGE_LOCK_DELETE_PERMISSION = "CumulusCore:StateChangeLockCanDelete";
        private const String CURRENT_USER_ID = "currentUser";
        private const String ANOTHER_USER_ID = "anotherUser";
        private const String ENTITY_TYPE = "TestEntity";

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {
            Mock.SetupStatic(typeof(ODataControllerHelper));
            Mock.SetupStatic(typeof(CurrentUserDataProvider));
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _stateChangeLocksController = new StateChangeLocksController();
            _lifeCycleContext = Mock.Create<LifeCycleContext>();
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithReadPermissionReturnsHisStateChangeLocks()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(true)
                .MustBeCalled();
            Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUserId())
                .Returns(CURRENT_USER_ID)
                .MustBeCalled();
            Mock.Arrange(() => _lifeCycleContext.StateChangeLocks)
                .IgnoreInstance()
                .ReturnsCollection(CreateSampleStateChangeLockDbSetForUser(CURRENT_USER_ID))
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(
                new ODataQueryOptions<StateChangeLock>(context, request))
                .Result;

            Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult<IEnumerable<StateChangeLock>>));

            var response = actionResult as OkNegotiatedContentResult<IEnumerable<StateChangeLock>>;
            Assert.IsNotNull(response);
            Assert.AreEqual(2, response.Content.Count());

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
            Mock.Assert(() => CurrentUserDataProvider.GetCurrentUserId());
            Mock.Assert(_lifeCycleContext);
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithoutReadPermissionReturnsForbidden()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(false)
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(
                new ODataQueryOptions<StateChangeLock>(context, request))
                .Result;

            Assert.IsTrue(actionResult.GetType() == typeof(StatusCodeResult));
            var response = (StatusCodeResult)actionResult;
            Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode);

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
        }

        [TestMethod]
        public void GetStateChangeLocksForUserWithoutStateChangeLocksReturnsEmptyList()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION))
                .Returns(true)
                .MustBeCalled();
            Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUserId())
                .Returns(CURRENT_USER_ID)
                .MustBeCalled();
            Mock.Arrange(() => _lifeCycleContext.StateChangeLocks)
                .IgnoreInstance()
                .ReturnsCollection(CreateSampleStateChangeLockDbSetForUser(ANOTHER_USER_ID))
                .MustBeCalled();

            var context = new ODataQueryContext(GetBuilder().GetEdmModel(), typeof(StateChangeLock));
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Core.svc/StateChangeLocks");

            var actionResult = _stateChangeLocksController.GetStateChangeLocks(
                new ODataQueryOptions<StateChangeLock>(context, request))
                .Result;

            Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult<IEnumerable<StateChangeLock>>));

            var response = actionResult as OkNegotiatedContentResult<IEnumerable<StateChangeLock>>;
            Assert.IsNotNull(response);
            Assert.AreEqual(0, response.Content.Count());

            Mock.Assert(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_READ_PERMISSION));
            Mock.Assert(() => CurrentUserDataProvider.GetCurrentUserId());
            Mock.Assert(_lifeCycleContext);
        }

        [TestMethod]
        public void PostStateChangeLockForUserWithoutCreatePermissionReturnsForbidden()
        {
            Mock.Arrange(() => CurrentUserDataProvider.HasCurrentUserPermission(STATE_CHANGE_LOCK_CREATE_PERMISSION))
                .Returns(false)
                .MustBeCalled();

            var actionResult = _stateChangeLocksController.Post(
                new StateChangeLock
                {
                    Id = 1,
{'fraction_non_alphanumeric': 0.08817971866470711, 'fraction_numerical': 0.001539645881447267, 'mean_word_length': 3.162540052432275, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6965701', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3666, 'n_words': 637}