public void ExecutionEnvironmentUtils_GetSwaggerOutputForService_ScalarInputsNoOutputs_ValidSwaggerDefinition() { //------------Setup for test-------------------------- var mockResource = new Mock <IResource>(); mockResource.Setup(resource => resource.ResourceName).Returns("resourceName"); var versionInfo = new VersionInfo { VersionNumber = "1.0" }; mockResource.Setup(resource => resource.VersionInfo).Returns(versionInfo); const string expectedSwaggerVersion = "\"swagger\":2"; const string expectedParameters = "\"parameters\":[" + "{" + "\"name\":\"Name\"," + "\"in\":\"query\"," + "\"required\":true," + "\"type\":\"string\"" + "}]"; const string expectedEmptyResponse = "\"200\":{\"schema\":{\"$ref\":\"#/definition/Output\"}}"; //------------Execute Test--------------------------- var swaggerOutputForService = ExecutionEnvironmentUtils.GetSwaggerOutputForService(mockResource.Object, "<DataList><Name Description=\"\" IsEditable=\"True\" ColumnIODirection=\"Input\" /></DataList>").Replace(Environment.NewLine, "").Replace(" ", ""); //------------Assert Results------------------------- StringAssert.Contains(swaggerOutputForService, expectedSwaggerVersion); StringAssert.Contains(swaggerOutputForService, expectedParameters); StringAssert.Contains(swaggerOutputForService, expectedEmptyResponse); }
static string GetExecutePayload(IDSFDataObject dataObject, IResource resource, WebRequestTO webRequest, ref DataListFormat formatter) { var notDebug = !dataObject.IsDebug || dataObject.RemoteInvoke || dataObject.RemoteNonDebugInvoke; if (notDebug && resource?.DataList != null) { switch (dataObject.ReturnType) { case EmitionTypes.XML: { return(ExecutionEnvironmentUtils.GetXmlOutputFromEnvironment(dataObject, resource.DataList.ToString(), 0)); } case EmitionTypes.SWAGGER: { formatter = DataListFormat.CreateFormat("SWAGGER", EmitionTypes.SWAGGER, "application/json"); return(ExecutionEnvironmentUtils.GetSwaggerOutputForService(resource, resource.DataList.ToString(), webRequest.WebServerUrl)); } default: case EmitionTypes.JSON: { formatter = DataListFormat.CreateFormat("JSON", EmitionTypes.JSON, "application/json"); return(ExecutionEnvironmentUtils.GetJsonOutputFromEnvironment(dataObject, resource.DataList.ToString(), 0)); } } } return(string.Empty); }
public void ExecutionEnvironmentUtils_GetSwaggerOutputForService_ScalarInputsNoOutputs_ValidSwaggerDefinition() { //------------Setup for test-------------------------- var mockResource = new Mock <IWarewolfResource>(); mockResource.Setup(resource => resource.ResourceName).Returns("resourceName"); var versionInfo = new VersionInfo { VersionNumber = "1.0" }; mockResource.Setup(resource => resource.VersionInfo).Returns(versionInfo); const string expectedOpenapi = "\"openapi\":\"3.0.1\""; const string expectedInfo = "\"info\":{\"title\":\"resourceName\",\"description\":\"resourceName\",\"version\":\"1.0\"}"; const string expectedServers = "\"servers\":[{\"url\":\"https://servername\"}]"; const string expectedParameters = "\"parameters\":[{\"name\":\"Name\",\"in\":\"query\",\"required\":true,\"schema\":{\"type\":\"string\"}}]"; const string expectedEmptyResponse = "\"responses\":{\"200\":{\"description\":\"Success\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{}}}}}}}}}}"; //------------Execute Test--------------------------- var swaggerOutputForService = ExecutionEnvironmentUtils.GetSwaggerOutputForService(mockResource.Object, "<DataList><Name Description=\"\" IsEditable=\"True\" ColumnIODirection=\"Input\" /></DataList>", "https://serverName:3142/public/resourceName.api").Replace(Environment.NewLine, "").Replace(" ", ""); //------------Assert Results------------------------- StringAssert.Contains(swaggerOutputForService, expectedOpenapi); StringAssert.Contains(swaggerOutputForService, expectedInfo); StringAssert.Contains(swaggerOutputForService, expectedServers); StringAssert.Contains(swaggerOutputForService, expectedParameters); StringAssert.Contains(swaggerOutputForService, expectedEmptyResponse); }
public void ExecutionEnvironmentUtils_GetSwaggerOutputForService_NullDataList_ExpectedException() { //------------Setup for test-------------------------- //------------Execute Test--------------------------- ExecutionEnvironmentUtils.GetSwaggerOutputForService(new Mock <IResource>().Object, null); //------------Assert Results------------------------- }
public void ExecutionEnvironmentUtils_GetSwaggerOutputForService_NullServiceName_ExpectedException() { //------------Setup for test-------------------------- //------------Execute Test--------------------------- ExecutionEnvironmentUtils.GetSwaggerOutputForService(null, ""); //------------Assert Results------------------------- }
public void ExecutionEnvironmentUtils_GetSwaggerOutputForService_NoInputsNoOutputs_ValidSwaggerDefinition() { //------------Setup for test-------------------------- var mockResource = new Mock <IResource>(); mockResource.Setup(resource => resource.ResourceName).Returns("resourceName"); var versionInfo = new VersionInfo { VersionNumber = "1.0" }; mockResource.Setup(resource => resource.VersionInfo).Returns(versionInfo); const string expectedSwaggerVersion = "\"swagger\":2"; const string expectedEmptyParameters = "\"parameters\":[]"; const string expectedEmptyResponse = "\"200\":{\"schema\":{\"$ref\":\"#/definition/Output\"}}"; //------------Execute Test--------------------------- var swaggerOutputForService = ExecutionEnvironmentUtils.GetSwaggerOutputForService(mockResource.Object, "<DataList></DataList>").Replace(Environment.NewLine, "").Replace(" ", ""); //------------Assert Results------------------------- StringAssert.Contains(swaggerOutputForService, expectedSwaggerVersion); StringAssert.Contains(swaggerOutputForService, expectedEmptyParameters); StringAssert.Contains(swaggerOutputForService, expectedEmptyResponse); }
protected static IResponseWriter CreateForm(WebRequestTO webRequest, string serviceName, string workspaceId, NameValueCollection headers, IPrincipal user = null) { //lock(ExecutionObject) { string executePayload = ""; Guid workspaceGuid; if (workspaceId != null) { if (!Guid.TryParse(workspaceId, out workspaceGuid)) { workspaceGuid = WorkspaceRepository.Instance.ServerWorkspace.ID; } } else { workspaceGuid = WorkspaceRepository.Instance.ServerWorkspace.ID; } var allErrors = new ErrorResultTO(); var dataObject = new DsfDataObject(webRequest.RawRequestPayload, GlobalConstants.NullDataListID, webRequest.RawRequestPayload) { IsFromWebServer = true, ExecutingUser = user, ServiceName = serviceName, WorkspaceID = workspaceGuid }; // now bind any variables that are part of the path arguments ;) BindRequestVariablesToDataObject(webRequest, ref dataObject); // now process headers ;) if (headers != null) { Dev2Logger.Log.Debug("Remote Invoke"); var isRemote = headers.Get(HttpRequestHeader.Cookie.ToString()); var remoteId = headers.Get(HttpRequestHeader.From.ToString()); if (isRemote != null && remoteId != null) { if (isRemote.Equals(GlobalConstants.RemoteServerInvoke)) { // we have a remote invoke ;) dataObject.RemoteInvoke = true; } if (isRemote.Equals(GlobalConstants.RemoteDebugServerInvoke)) { // we have a remote invoke ;) dataObject.RemoteNonDebugInvoke = true; } dataObject.RemoteInvokerID = remoteId; } } // now set the emition type ;) int loc; if (!String.IsNullOrEmpty(serviceName) && (loc = serviceName.LastIndexOf(".", StringComparison.Ordinal)) > 0) { // default it to xml dataObject.ReturnType = EmitionTypes.XML; if (loc > 0) { var typeOf = serviceName.Substring((loc + 1)).ToUpper(); EmitionTypes myType; if (Enum.TryParse(typeOf, out myType)) { dataObject.ReturnType = myType; } // adjust the service name to drop the type ;) // avoid .wiz amendments ;) if (!typeOf.ToLower().Equals(GlobalConstants.WizardExt)) { serviceName = serviceName.Substring(0, loc); dataObject.ServiceName = serviceName; } if (typeOf.Equals("api", StringComparison.OrdinalIgnoreCase)) { dataObject.ReturnType = EmitionTypes.SWAGGER; } } } else { // default it to xml dataObject.ReturnType = EmitionTypes.XML; } // ensure service gets set ;) if (dataObject.ServiceName == null) { dataObject.ServiceName = serviceName; } IResource resource = null; if (!String.IsNullOrEmpty(dataObject.ServiceName)) { resource = ResourceCatalog.Instance.GetResource(dataObject.WorkspaceID, dataObject.ServiceName); if (resource != null) { dataObject.ResourceID = resource.ResourceID; } } var esbEndpoint = new EsbServicesEndpoint(); dataObject.EsbChannel = esbEndpoint; var canExecute = true; if (ServerAuthorizationService.Instance != null) { var authorizationService = ServerAuthorizationService.Instance; var hasView = authorizationService.IsAuthorized(AuthorizationContext.View, dataObject.ResourceID.ToString()); var hasExecute = authorizationService.IsAuthorized(AuthorizationContext.Execute, dataObject.ResourceID.ToString()); canExecute = (hasExecute && hasView) || ((dataObject.RemoteInvoke || dataObject.RemoteNonDebugInvoke) && hasExecute) || (resource != null && resource.ResourceType == ResourceType.ReservedService); } // Build EsbExecutionRequest - Internal Services Require This ;) EsbExecuteRequest esbExecuteRequest = new EsbExecuteRequest { ServiceName = serviceName }; foreach (string key in webRequest.Variables) { esbExecuteRequest.AddArgument(key, new StringBuilder(webRequest.Variables[key])); } Dev2Logger.Log.Debug("About to execute web request [ " + serviceName + " ] DataObject Payload [ " + dataObject.RawPayload + " ]"); var executionDlid = GlobalConstants.NullDataListID; if (canExecute && dataObject.ReturnType != EmitionTypes.SWAGGER) { ErrorResultTO errors; executionDlid = esbEndpoint.ExecuteRequest(dataObject, esbExecuteRequest, workspaceGuid, out errors); allErrors.MergeErrors(errors); } else if (!canExecute) { allErrors.AddError("Executing a service externally requires View and Execute permissions"); } foreach (var error in dataObject.Environment.Errors) { allErrors.AddError(error, true); } // Fetch return type ;) var formatter = DataListFormat.CreateFormat("XML", EmitionTypes.XML, "text/xml"); // force it to XML if need be ;) // Fetch and convert DL ;) if (!dataObject.Environment.HasErrors()) { // a normal service request if (!esbExecuteRequest.WasInternalService) { dataObject.DataListID = executionDlid; dataObject.WorkspaceID = workspaceGuid; dataObject.ServiceName = serviceName; if (!dataObject.IsDebug || dataObject.RemoteInvoke || dataObject.RemoteNonDebugInvoke) { if (dataObject.ReturnType == EmitionTypes.JSON) { formatter = DataListFormat.CreateFormat("JSON", EmitionTypes.JSON, "application/json"); executePayload = ExecutionEnvironmentUtils.GetJsonOutputFromEnvironment(dataObject, resource.DataList.ToString(), 0); } else if (dataObject.ReturnType == EmitionTypes.XML) { executePayload = ExecutionEnvironmentUtils.GetXmlOutputFromEnvironment(dataObject, Guid.Empty, resource.DataList.ToString(), 0); } else if (dataObject.ReturnType == EmitionTypes.SWAGGER) { formatter = DataListFormat.CreateFormat("SWAGGER", EmitionTypes.SWAGGER, "application/json"); executePayload = ExecutionEnvironmentUtils.GetSwaggerOutputForService(resource, resource.DataList.ToString()); } } else { executePayload = string.Empty; } } else { // internal service request we need to return data for it from the request object ;) var serializer = new Dev2JsonSerializer(); executePayload = string.Empty; var msg = serializer.Deserialize <ExecuteMessage>(esbExecuteRequest.ExecuteResult); if (msg != null) { executePayload = msg.Message.ToString(); } // out fail safe to return different types of data from services ;) if (string.IsNullOrEmpty(executePayload)) { executePayload = esbExecuteRequest.ExecuteResult.ToString(); } } } else { if (dataObject.ReturnType == EmitionTypes.XML) { executePayload = "<FatalError> <Message> An internal error occurred while executing the service request </Message>"; executePayload += allErrors.MakeDataListReady(); executePayload += "</FatalError>"; } else { // convert output to JSON ;) executePayload = "{ \"FatalError\": \"An internal error occurred while executing the service request\","; executePayload += allErrors.MakeDataListReady(false); executePayload += "}"; } } Dev2Logger.Log.Debug("Execution Result [ " + executePayload + " ]"); // JSON Data ;) if (executePayload.IndexOf("</JSON>", StringComparison.Ordinal) >= 0) { int start = executePayload.IndexOf(GlobalConstants.OpenJSON, StringComparison.Ordinal); if (start >= 0) { int end = executePayload.IndexOf(GlobalConstants.CloseJSON, StringComparison.Ordinal); start += GlobalConstants.OpenJSON.Length; executePayload = CleanupHtml(executePayload.Substring(start, (end - start))); if (!String.IsNullOrEmpty(executePayload)) { return(new StringResponseWriter(executePayload, ContentTypes.Json)); } } } Dev2DataListDecisionHandler.Instance.RemoveEnvironment(dataObject.DataListID); dataObject.Environment = null; return(new StringResponseWriter(executePayload, formatter.ContentType)); } }
public static IResponseWriter CreateResponseWriter(this ExecutionDto dto) { var dataObject = dto.DataObject; var esbExecuteRequest = dto.Request; var executionDlid = dto.DataListIdGuid; var workspaceGuid = dto.WorkspaceID; var serviceName = dto.ServiceName; var resource = dto.Resource; var formatter = dto.DataListFormat; var executePayload = dto.PayLoad; var webRequest = dto.WebRequestTO; var serializer = dto.Serializer; var allErrors = dto.ErrorResultTO; if (!dataObject.Environment.HasErrors()) { if (!esbExecuteRequest.WasInternalService) { dataObject.DataListID = executionDlid; dataObject.WorkspaceID = workspaceGuid; dataObject.ServiceName = serviceName; if (!dataObject.IsDebug || dataObject.RemoteInvoke || dataObject.RemoteNonDebugInvoke) { if (resource?.DataList != null) { if (dataObject.ReturnType == EmitionTypes.JSON) { formatter = DataListFormat.CreateFormat("JSON", EmitionTypes.JSON, "application/json"); executePayload = ExecutionEnvironmentUtils.GetJsonOutputFromEnvironment(dataObject, resource.DataList.ToString(), 0); } else if (dataObject.ReturnType == EmitionTypes.XML) { executePayload = ExecutionEnvironmentUtils.GetXmlOutputFromEnvironment(dataObject, resource.DataList.ToString(), 0); } else if (dataObject.ReturnType == EmitionTypes.SWAGGER) { formatter = DataListFormat.CreateFormat("SWAGGER", EmitionTypes.SWAGGER, "application/json"); executePayload = ExecutionEnvironmentUtils.GetSwaggerOutputForService(resource, resource.DataList.ToString(), webRequest.WebServerUrl); } } } else { executePayload = string.Empty; } } else { // internal service request we need to return data for it from the request object ;) executePayload = string.Empty; var msg = serializer.Deserialize <ExecuteMessage>(esbExecuteRequest.ExecuteResult); if (msg != null) { executePayload = msg.Message.ToString(); } // out fail safe to return different types of data from services ;) if (string.IsNullOrEmpty(executePayload)) { executePayload = esbExecuteRequest.ExecuteResult.ToString(); } } } else { executePayload = SetupErrors(dataObject, allErrors); } Dev2Logger.Debug("Execution Result [ " + executePayload + " ]"); if (!dataObject.Environment.HasErrors() && esbExecuteRequest.WasInternalService) { if (executePayload.IsJSON()) { formatter = DataListFormat.CreateFormat("JSON", EmitionTypes.JSON, "application/json"); } else if (executePayload.IsXml()) { formatter = DataListFormat.CreateFormat("XML", EmitionTypes.XML, "text/xml"); } } Dev2DataListDecisionHandler.Instance.RemoveEnvironment(dataObject.DataListID); dataObject.Environment = null; return(new StringResponseWriter(executePayload, formatter.ContentType)); }