public void ValidateBeContractCall(BeContract contract, BeContractCall call) { if (!contract.Id.Equals(call.Id)) { throw new BeContractException("Contract's do not have the same ID") { BeContract = contract, BeContractCall = call }; } if (string.IsNullOrWhiteSpace(call.ISName)) { //TODO: test if the ISName exists in the DB ? throw new BeContractException("The BeContractCall has no ISName"); } //Test inputs contract.Inputs?.ForEach(input => { if (call.Inputs == null) { throw new BeContractException("The BeContracts needs inputs but the call hasn't inputs !") { BeContract = contract, BeContractCall = call } } ; if (call.Inputs.TryGetValue(input.Key, out dynamic value)) { //Check if the dynamic is a int64 if (value.GetType() == typeof(Int64)) { value = (int)value; call.Inputs[input.Key] = value; } //Check the types if (value.GetType().Name != input.Type) { throw new BeContractException($"The contract expects {input.Type} but {value.GetType().Name} was found") { BeContract = contract, BeContractCall = call }; } } else if (input.Required) { //Key isn't find and is required throw new BeContractException($"No key was found for {input.Key} and it is required") { BeContract = contract, BeContractCall = call }; } }); }
private async Task <BeContract> FindSingleBeContractByIdAsync(string id) { BeContract ret = null; using (var client = new HttpClient()) { try { client.BaseAddress = new Uri(ConfigHelper.GetServiceUrl("centralserver")); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync($"api/central/contracts?id={id}"); if (response.IsSuccessStatusCode) { ret = await response.Content.ReadAsAsync <BeContract>(); } } catch (Exception ex) { throw new BeContractException("Error with Central Service when finding the BeContract: " + ex.Message); } } return(ret); }
public static BeContract GetOwnerIdByDogId() { var GetDogOwnerContract = new BeContract() { Id = "GetOwnerIdByDogId", Description = "This contract is used to get the dog owner id", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "DogID", Description = "The ID of the Dog", Required = true, Type = typeof(string).Name } }, Queries = new List <Query>() }; GetDogOwnerContract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "OwnerIDOfTheDog", Description = "The ID of the owner of the dog", Type = typeof(string).Name } }; return(GetDogOwnerContract); }
public async Task <BeContract> FindBeContractByIdAsync(string id) { BeContract contract = null; await Task.Run(() => contract = BeContractsMock.GetContracts().FirstOrDefault(c => c.Id.Equals(id))); return(contract); }
//TODO: Call the Authorisation server here ! public async Task <bool> CanInformationSystemUseContract(string isName, BeContract contract) { bool canUse = true; await Task.Run(() => { switch (contract.Id) { case "GetServiceInfo": if (isName.Equals("Insomnia Client") || isName.Equals("Public Service")) { canUse = false; } break; case "GetPopulationContract": if (isName.Equals("Postman")) { canUse = false; } break; case "GetDivContract": if (isName.Equals("Insomnia Client")) { canUse = false; } break; case "GetBankContract": if (isName.Equals("MIC")) { canUse = false; } break; } }); return(canUse); }
public void AreEquals(BeContract expected, BeContract actual) { Assert.AreEqual(expected.Description, actual.Description); Assert.AreEqual(expected.Id, actual.Id); Assert.AreEqual(expected.Version, actual.Version); for (int i = 0; i < expected.Inputs?.Count; i++) { Assert.AreEqual(expected.Inputs[i].Description, actual.Inputs[i].Description); Assert.AreEqual(expected.Inputs[i].Key, actual.Inputs[i].Key); Assert.AreEqual(expected.Inputs[i].Required, actual.Inputs[i].Required); Assert.AreEqual(expected.Inputs[i].Type, actual.Inputs[i].Type); } for (int i = 0; i < expected.Queries?.Count; i++) { Assert.AreEqual(expected.Queries[i].Contract.Id, actual.Queries[i].Contract.Id); for (int j = 0; j < expected.Queries[i].Mappings.Count; j++) { Assert.AreEqual(expected.Queries[i].Mappings[j].LookupInputKey, actual.Queries[i].Mappings[j].LookupInputKey); Assert.AreEqual(expected.Queries[i].Mappings[j].LookupInputId, actual.Queries[i].Mappings[j].LookupInputId); Assert.AreEqual(expected.Queries[i].Mappings[j].InputKey, actual.Queries[i].Mappings[j].InputKey); } } for (int i = 0; i < expected.Outputs?.Count; i++) { Assert.AreEqual(expected.Outputs[i].Description, actual.Outputs[i].Description); Assert.AreEqual(expected.Outputs[i].Key, actual.Outputs[i].Key); Assert.AreEqual(expected.Outputs[i].LookupInputId, actual.Outputs[i].LookupInputId); Assert.AreEqual(expected.Outputs[i].Type, actual.Outputs[i].Type); } }
public async Task <bool> CanInformationSystemUseContract(string isName, BeContract contract) { bool canUse = true; await Task.Run(() => { canUse = true; }); return(canUse); }
public async Task <Boolean> ValidateBeContract(BeContract contract) { var duplicated = new List <string>(); contract.Inputs?.ForEach(input1 => { var occur = contract.Inputs.Count(input2 => input1.Key == input2.Key); if (occur > 1) { duplicated.Add(input1.Key); } } ); if (duplicated.Count > 0) { throw new BeContractException("Duplicated key in " + contract.Id + " contract for Inputs " + string.Join(", ", duplicated)); } try { if (Schema == null) { Schema = await Generators.GenerateSchema(); } var json = Generators.SerializeBeContract(contract); var errors = Schema.Validate(json); var str = ""; foreach (var e in errors.ToList()) { str += e.ToString(); } if (errors.Count > 0) { throw new BeContractException(errors.ToString()) { BeContract = contract }; } else { return(true); } } catch (JsonSerializationException ex) { throw new BeContractException(ex.Message) { BeContract = contract }; } }
public void TestAddAndRead(BeContract contract) { if (Db.Contracts.Any(c => c.Id.Equals(contract.Id))) { Db.Contracts.Remove(Db.Contracts.FirstOrDefault(c => c.Id.Equals(contract.Id))); } Db.Contracts.Add(contract); Db.SaveChanges(); var owner = Db.Contracts.FirstOrDefault(c => c.Id.Equals(contract.Id)); BeContractEquals.AreEquals(contract, owner); }
public async Task <BeContract> FindBeContractByIdAsync(string id) { BeContract ret = await FindSingleBeContractByIdAsync(id); //Because we only get the contracts id's from the queries //We need to get the queries contracts objects from here if (ret?.Queries != null) { foreach (var q in ret?.Queries) { q.Contract = await FindBeContractByIdAsync(q.Contract.Id); } } return(ret); }
public static BeContract GetFunnyContract() { var contract = new BeContract() { Id = "GetFunnyContract", Description = "This contract is used to get some funny information about a citizen", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "NRID", Description = "The ID of the citizen", Required = true, Type = typeof(string).Name }, new Input() { Key = "Justification", Description = "Why are you reading this information", Required = true, Type = typeof(string).Name } }, Queries = new List <Query>() }; contract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "ExtraInfo", Description = "The info that a citizen share's", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Email", Description = "Email of a citizen share's", Type = typeof(string).Name } }; return(contract); }
public static BeContract GetDIVContract() { var contract = new BeContract() { Id = "GetDivContract", Description = "This contract is used to get information about a citizen car's", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "NRID", Description = "The ID of the citizen", Required = true, Type = typeof(string).Name }, new Input() { Key = "Justification", Description = "Why are you reading this information", Required = true, Type = typeof(string).Name } }, Queries = new List <Query>() }; contract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "NumberPlate", Description = "Number plate of the citizen", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Brand", Description = "Brand of the car", Type = typeof(string).Name } }; return(contract); }
public static BeContract GetAddressByOwnerId() { var GetAddressByOwnerContract = new BeContract() { Id = "GetAddressByOwnerId", Description = "This contract is used to get the address by owner id", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "OwnerID", Description = "The ID of the Owner", Required = true, Type = typeof(string).Name } }, Queries = new List <Query>() }; GetAddressByOwnerContract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "Street", Description = "Street name", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "StreetNumber", Description = "Street number", Type = typeof(int).Name }, new Output() { LookupInputId = 0, Key = "Country", Description = "Country of the address", Type = typeof(string).Name } }; return(GetAddressByOwnerContract); }
public static BeContract GetMathemathicFunction() { var GetMathemathicContract = new BeContract() { Id = "GetMathemathicFunction", Description = "This contract is used to do a + b", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "A", Description = "First number", Required = true, Type = typeof(int).Name }, new Input() { Key = "B", Description = "First number", Required = true, Type = typeof(int).Name } }, }; GetMathemathicContract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "Total", Description = "This is the sum of a + b", Type = typeof(int).Name }, new Output() { LookupInputId = 0, Key = "Formula", Description = "This is the sum formula", Type = typeof(string).Name } }; return(GetMathemathicContract); }
public static BeContract GetDoubleInputContract() { var DoubleInputContract = new BeContract() { Id = "DoubleInputContract", Description = "This contract is an examples with 2 inputs", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "First", Description = "First string", Required = false, Type = typeof(int).Name }, new Input() { Key = "Second", Description = "First number", Required = false, Type = typeof(int).Name } }, }; DoubleInputContract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "IsFirstNull", Description = "Return true if the first input is empty", Type = typeof(bool).Name }, new Output() { LookupInputId = 0, Key = "IsSecondNull", Description = "Return true if the first input is empty", Type = typeof(bool).Name } }; return(DoubleInputContract); }
public void ValidateBeContractReturn(BeContract contract, BeContractReturn ret) { if (!contract.Id.Equals(ret.Id)) { throw new BeContractException("Contract's do not have the same ID") { BeContract = contract, BeContractReturn = ret }; } //Test outputs contract.Outputs?.ForEach(output => { if (ret.Outputs.TryGetValue(output.Key, out dynamic value)) { //Check if the dynamic is a int64 if (value.GetType() == typeof(Int64)) { value = (int)value; ret.Outputs[output.Key] = value; } //Check the types if (value.GetType().Name != output.Type) { throw new BeContractException($"The contract expects {output.Type} but {value.GetType().Name} was found") { BeContract = contract, BeContractReturn = ret }; } } else { //Key isn't find throw new BeContractException($"No key was found for {output.Key}") { BeContract = contract, BeContractReturn = ret }; } }); }
public static BeContract GetServiceInfo() { var GetServiceInfoContract = new BeContract() { Id = "GetServiceInfo", Description = "This contract is used to get the information of the service", Version = "V001", Inputs = new List <Input>(), Queries = new List <Query>() }; GetServiceInfoContract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "Name", Description = "The name of the service", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Purpose", Description = "The purpose of the service", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "CreationDate", Description = "The creation date of the service", Type = typeof(string).Name } }; return(GetServiceInfoContract); }
/// <summary> /// Call the contract with the specified inputs to a specified service /// </summary> /// <param name="contract">Contract that will be called</param> /// <param name="call">The inputs for the contract</param> /// <returns>Returns the outputs for that specific contract</returns> private async Task <BeContractReturn> CallServiceAsync(BeContract contract, BeContractCall call) { //Get the ads for this call var ads = await AsService.FindASAsync(call.Id); if (ads == null) { throw new BeContractException($"No service found for {call.Id}") { BeContractCall = call } } ; //Check if the caller has the permission if (!(await AuthService.CanInformationSystemUseContract(call.ISName, contract))) { throw new BeContractException($"The caller {call.ISName} does not have the permission to use {call.Id}") { BeContractCall = call, BeContract = contract } } ; //If everything is fine, call the service BeContractReturn returns = await AsService.CallAsync(ads, call); //TODO: // -Send error to the service if (returns != null) { validators.ValidateBeContractReturn(contract, returns); } return(returns); }
public string SerializeBeContract(BeContract contract) { return(JsonConvert.SerializeObject(contract, Formatting.Indented)); }
public static BeContract GetPopulationContract() { var contract = new BeContract() { Id = "GetPopulationContract", Description = "This contract is used to get the basics information about a citizen", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "NRID", Description = "The ID of the citizen", Required = true, Type = typeof(string).Name }, new Input() { Key = "Justification", Description = "Why are you reading this information", Required = true, Type = typeof(string).Name } }, Queries = new List <Query>() }; contract.Outputs = new List <Output>() { new Output() { LookupInputId = 0, Key = "FirstName", Description = "First name of the citizen", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "LastName", Description = "Last name of the citizen", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Birthday", Description = "Birthday of the citizen", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Locality", Description = "Locality of the citizen", Type = typeof(string).Name }, new Output() { LookupInputId = 0, Key = "Nationality", Description = "Nationality of the citizen", Type = typeof(string).Name }, }; return(contract); }
/// <summary> /// Find the contract with the call id /// Loop over every query /// </summary> /// <param name="call">The inputs for the contract call</param> /// <returns>Return a list of contract id with his return types</returns> private async Task <List <BeContractReturn> > CallAndLoopQueriesAsync(BeContractCall call, BeContract contract = null) { if (contract == null) { contract = await BcService.FindBeContractByIdAsync(call.Id); } if (contract == null) { throw new BeContractException($"No contract was found with id {call.Id}"); } validators.ValidateBeContractCall(contract, call); var returns = new List <BeContractReturn>(); //If it's a nested contract, loop through it if (contract?.Queries?.Count > 0) { foreach (var q in contract.Queries) { //Create the BeContractCall var callInQuery = new BeContractCall() { Id = q.Contract.Id, ISName = call.ISName, Inputs = new Dictionary <string, dynamic>() }; //Fill in the inputs of the BeContractCall q.Contract.Inputs?.ForEach(input => { var mapping = q.Mappings.FirstOrDefault(m => m.InputKey.Equals(input.Key)); if (mapping == null) { throw new BeContractException($"No mapping exists for inputKey {input.Key}"); } //We need to check our contract input if (mapping.LookupInputId == 0) { if (!call.Inputs.TryGetValue(mapping.LookupInputKey, out dynamic value)) { throw new BeContractException($"No value was found for the key {mapping.LookupInputKey} in the lookupinputid n°{mapping.LookupInputId}") { BeContractCall = call } } ; callInQuery.Inputs.Add(input.Key, value); } //Find the contract where we need to search the value else { if ((mapping.LookupInputId - 1) >= returns.Count) { throw new BeContractException($"Mapping LookupInputId n°{mapping.LookupInputId} is bigger then the returns size {returns.Count}"); } var returnToBeUsed = returns[mapping.LookupInputId - 1]; if (returnToBeUsed == null) { throw new BeContractException($"No output was found for query {q.Contract.Id} with mapping lookupinputid n°{mapping.LookupInputId}") { BeContractCall = call } } ; //We need to check our contract outputs if (!returnToBeUsed.Outputs.TryGetValue(mapping.LookupInputKey, out dynamic value)) { throw new BeContractException($"No value was found for the key {mapping.LookupInputKey} in the contract outputs of n°{mapping.LookupInputId}") { BeContractCall = call } } ; callInQuery.Inputs.Add(input.Key, value); } }); //Call the contract var loopdQueries = await CallAndLoopQueriesAsync(callInQuery); if (loopdQueries.Contains(null)) { throw new BeContractException($"Got an null in loopqueries with id {callInQuery.Id}"); } returns.AddRange(loopdQueries); } ; } else { var serviceReturns = await CallServiceAsync(contract, call); if (serviceReturns == null) { throw new BeContractException($"Got an null in serviceCall with id {call.Id}"); } returns.Add(serviceReturns); } return(returns); }
public async System.Threading.Tasks.Task <ActionResult> CreateAsync(BeContractViewModel contractVM) { var contract = new BeContract() { Id = contractVM.Id, Description = contractVM.Description, Version = contractVM.Version, Inputs = contractVM.Inputs, Queries = new List <Query>(), Outputs = contractVM.Outputs }; if (contractVM.Queries != null) { contractVM.Queries.ForEach(q => { contract.Queries.Add(new Query() { Contract = ctx.Contracts.FirstOrDefault(c => c.Id.Equals(q.Contract)), Mappings = q.Mappings }); }); } try { await validators.ValidateBeContract(contract); } catch (BeContractException ex) { var validError = new { Status = "2", Error = ex.Message }; return(Json(validError)); } try { ctx.Contracts.Add(contract); ctx.SaveChanges(); await CallToMLAsync(new { ContractId = contract.Id, UserName = "******" }, "api/Contract/Add"); } catch (Exception ex) { var dbError = new { Status = "3", Error = ex.Message }; return(Json(dbError)); } var ret = new { Status = "1", Error = "None" }; return(Json(ret)); }
public static BeContract GetAddressByDogId() { var GetAddressByOwnerContract = GetAddressByOwnerId(); var GetOwnerIdByDogContract = GetOwnerIdByDogId(); var GetAddressByDogContract = new BeContract() { Id = "GetAddressByDogId", Description = "This contract is used to get the address by dog id", Version = "V001", Inputs = new List <Input>() { new Input() { Key = "MyDogID", Description = "The ID of the Dog", Required = true, Type = typeof(string).Name } }, }; GetAddressByDogContract.Queries = new List <Query>() { new Query() { Contract = GetOwnerIdByDogContract, Mappings = new List <Mapping>() { new Mapping() { InputKey = "DogID", LookupInputId = 0, LookupInputKey = "MyDogID" } } }, new Query() { Contract = GetAddressByOwnerContract, Mappings = new List <Mapping>() { new Mapping() { InputKey = "OwnerID", LookupInputId = 1, LookupInputKey = "OwnerIDOfTheDog" } } } }; GetAddressByDogContract.Outputs = new List <Output>() { new Output() { LookupInputId = 1, Key = "Street", Description = "Street name", Type = typeof(string).Name }, new Output() { LookupInputId = 1, Key = "StreetNumber", Description = "Street number", Type = typeof(int).Name }, new Output() { LookupInputId = 1, Key = "Country", Description = "Country of the address", Type = typeof(string).Name } }; return(GetAddressByDogContract); }