Пример #1
0
        /// <summary>
        /// 根据契约编号得到契约
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MockContract Find(string id)
        {
            MockContract contract = null;

            if (_keyValues.TryGetValue(id, out contract))
            {
                return(contract);
            }
            return(null);
        }
Пример #2
0
        /// <summary>
        /// 触发事件
        /// </summary>
        public void RaiseEvent(MockContract sender)
        {
            var package = sender.Package;
            var target  = package?.Find(_contractId);

            if (target != null)
            {
                target.Response = _response;
            }
        }
Пример #3
0
        private void ParseJSON(string code)
        {
            var dto   = DTObject.CreateReusable(code);
            var items = dto.GetList();

            foreach (var item in items)
            {
                var contract = MockContract.Create(item, this);
                _contracts.Add(contract.ServiceName, contract);

                if (!string.IsNullOrEmpty(contract.Id))
                {
                    _keyValues.Add(contract.Id, contract);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 根据服务请求获取契约
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public MockContract Find(ServiceRequest request)
        {
            var contracts = _contracts.GetValues(request.Name);

            if (contracts == null)
            {
                throw new ServiceException("没有找到服务" + request.Name + "的mock契约");
            }
            MockContract target = BestMatch(contracts, request);

            if (target != null)
            {
                return(target);
            }

            target = ArgumentMatch(contracts, request);
            if (target != null)
            {
                return(target);
            }

            target = GetDefaultContract(contracts);
            if (target != null)
            {
                return(target);
            }

            if (target == null)
            {
                throw new ServiceException("没有找到服务"
                                           + request.Name + "下调用约定为"
                                           + request.Argument.GetCode() + "、身份信息为"
                                           + request.Identity.GetCode() + "的mock数据");
            }
            return(target);
        }