private Mock <IServiceProvider> GetServiceProvider() { var data = new StorageViewModel2 { Code = "GTM.01", Id = 8, IsCentral = true, Name = "GUDANG TRANSFER STOCK MAJOR MINOR" }; Dictionary <string, object> result = new ResultFormatter("1.0", General.OK_STATUS_CODE, General.OK_MESSAGE) .Ok(data); var exp = new ExpeditionServiceViewModel { code = "Dikirim Sendiri", name = "Dikirim Sendiri", _id = 2 }; Dictionary <string, object> result2 = new ResultFormatter("1.0", General.OK_STATUS_CODE, General.OK_MESSAGE) .Ok(exp); //HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.OK); //message.Content = new StringContent("{\"apiVersion\":\"1.0\",\"statusCode\":200,\"message\":\"Ok\",\"data\":[{\"Code\":MMS.01,\"Name\":\"MAJOR MINOR SHOP/ONLINE\",\"Description\":null,\"_id\":\"1\"}],\"info\":{\"count\":1,\"page\":1,\"size\":1,\"total\":2,\"order\":{\"date\":\"desc\"},\"select\":[\"Id\",\"code\",\"rate\",\"date\"]}}"); var HttpClientService = new Mock <IHttpClientService>(); HttpClientService .Setup(x => x.GetAsync(It.Is <string>(s => s.Contains("storages/code")))) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(result)) }); HttpClientService .Setup(x => x.GetAsync(It.Is <string>(s => s.Contains("expedition-service-routers/all/code")))) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(result2)) }); var serviceProvider = new Mock <IServiceProvider>(); serviceProvider .Setup(x => x.GetService(typeof(IdentityService))) .Returns(new IdentityService() { Token = "Token", Username = "******" }); serviceProvider .Setup(x => x.GetService(typeof(IHttpClientService))) .Returns(HttpClientService.Object); return(serviceProvider); }
private StorageViewModel2 GetStorage(string code) { string itemUri = "master/storages/code"; string queryUri = "?code=" + code; string uri = itemUri + queryUri; IHttpClientService httpClient = (IHttpClientService)serviceProvider.GetService(typeof(IHttpClientService)); var response = httpClient.GetAsync($"{APIEndpoint.Core}{uri}").Result; if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().Result; Dictionary <string, object> result = JsonConvert.DeserializeObject <Dictionary <string, object> >(content); StorageViewModel2 viewModel = JsonConvert.DeserializeObject <StorageViewModel2>(result.GetValueOrDefault("data").ToString()); return(viewModel);//.Where(x => x.dataDestination[0].name == name && x.dataDestination[0].code == code).FirstOrDefault(); //throw new Exception(string.Format("{0}, {1}, {2}", response.StatusCode, response.Content, APIEndpoint.Purchasing)); } else { return(null); } }