Skip to content
Permalink
Browse files

feat: Create mocks in API imported from swagger/OAI

  • Loading branch information...
aelamrani committed Feb 14, 2019
1 parent 8cec222 commit 5d809ac1f2a6c79e1a6ac8daa4e624bae859aefb
@@ -41,6 +41,7 @@ class NewApiController {
private importCreateDocumentation: boolean;
private importCreatePolicyPaths: boolean;
private importCreatePathMapping: boolean;
private importCreateMocks: boolean;

constructor(
private $scope: INewApiScope,
@@ -105,91 +106,28 @@ class NewApiController {
}

importSwagger() {
let _this = this;
let swagger;
let swagger = {
with_documentation: this.importCreateDocumentation,
with_path_mapping: this.importCreatePathMapping,
with_policy_paths: this.importCreatePolicyPaths,
with_policy_mocks: this.importCreateMocks
};


if (this.importFileMode) {
swagger = {
type: 'INLINE',
payload: this.$scope.importAPIFile.content,
}
swagger.type = 'INLINE';
swagger.payload = this.$scope.importAPIFile.content;
} else {
swagger = {
type: 'URL',
payload: this.apiDescriptorURL,
}
swagger.type = 'URL';
swagger.payload = this.apiDescriptorURL;
}

this.ApiService.importSwagger(swagger).then(function (api) {
var importedAPI = api.data;
importedAPI.contextPath = importedAPI.name.replace(/\s+/g, '').toLowerCase();
importedAPI.description = (importedAPI.description) ? importedAPI.description : "Default API description";

_this.ApiService.import(null, _this.convertApiEntityToApiDefinition(importedAPI, swagger)).then(function(api) {
_this.NotificationService.show('API created');
_this.$state.go('management.apis.detail.portal.general', {apiId: api.data.id});
});
this.ApiService.importSwagger(swagger).then((api) => {
this.NotificationService.show('API successfully imported');
this.$state.go('management.apis.detail.portal.general', {apiId: api.data.id});
});
}


convertApiEntityToApiDefinition(apiEntity: ApiEntity, swagger: any) {
let apiDefinition = {
name: apiEntity.name,
version: apiEntity.version,
description: apiEntity.description,
proxy: {
context_path: apiEntity.contextPath,
}
};

if (apiEntity.endpoint) {
apiDefinition.proxy["groups"] = [{
name: "default-group",
endpoints: [{
name: "default",
target: apiEntity.endpoint
}]
}];
}

if (this.importCreateDocumentation) {
if ("INLINE" === swagger.type) {
apiDefinition["pages"] = [{
name: "swagger",
type: "SWAGGER",
content: swagger.payload,
order: 1
}]
} else {
apiDefinition["pages"] = [{
name: "swagger",
type: "SWAGGER",
source: {
type: "http-fetcher",
configuration: {
url: swagger.payload
}
},
order: 1
}]
}
}

if (this.importCreatePolicyPaths && apiEntity.paths) {
apiDefinition["paths"] = {};
for (let path of apiEntity.paths) {
apiDefinition["paths"][path] = [];
}
}

if (this.importCreatePathMapping && apiEntity.paths) {
apiDefinition["path_mappings"] = apiEntity.paths;
}

return JSON.stringify(apiDefinition);
}

enableImport() {
if (this.importFileMode) {
return this.enableFileImport;
@@ -52,25 +52,24 @@ <h4>... or import API definition or your Swagger descriptor file</h4>

<div style="margin-top:24px;" layout="column" ng-if="newApiCtrl.isSwaggerImport()">
<h4>Swagger import configurations:</h4>
<md-checkbox
ng-model="newApiCtrl.importCreateDocumentation"
aria-label="Create documentation pages" flex>
<md-checkbox ng-model="newApiCtrl.importCreateDocumentation" aria-label="Create documentation pages" flex>
Create documentation
<div class="ipsum" style="color: grey;">Add a documentation page with the content of the swagger descriptor.</div>
</md-checkbox>
<md-checkbox
ng-model="newApiCtrl.importCreatePathMapping"
aria-label="Create the path-mapping for analytics" flex>
<md-checkbox ng-model="newApiCtrl.importCreatePathMapping" aria-label="Create the path-mapping for analytics" flex>
Create the path-mapping for analytics
<div class="ipsum" style="color: grey;">Create each paths from the swagger descriptor.</div>
</md-checkbox>
<md-checkbox
ng-model="newApiCtrl.importCreatePolicyPaths"
aria-label="Create paths for policies" flex>
Create policies
<md-checkbox ng-model="newApiCtrl.importCreatePolicyPaths" aria-label="Create paths for policies"
ng-click="newApiCtrl.importCreateMocks = false" flex>
Create policies on paths
<div class="ipsum" style="color: grey;">Create each paths from the swagger descriptor.</div>
</md-checkbox>

<md-checkbox ng-model="newApiCtrl.importCreateMocks" aria-label="Create mocks on paths of policies" flex
ng-disabled="!newApiCtrl.importCreatePolicyPaths" style="margin-left: 20px;">
With mocks
<div class="ipsum" style="color: grey;">Add mocks on each paths from the swagger descriptor.</div>
</md-checkbox>
</div>

<div layout="row">
@@ -160,7 +160,7 @@ <h5>{{policy.name}}</h5>
sf-model="apiPoliciesCtrl.selectedApiPolicy[apiPoliciesCtrl.selectedApiPolicy.policyId]"
sf-options="{ validateOnRender: true, formDefaults: { startEmpty: true } }" novalidate></form>

<div ng-if="apiPoliciesCtrl.selectedApiPolicy.schema">
<div ng-if="apiPoliciesCtrl.schemaByPolicyId[apiPoliciesCtrl.selectedApiPolicy.policyId]">
<md-divider></md-divider>
<md-button permission permission-only="'api-definition-u'" class="md-raised"
ng-disabled="policyConfigurationForm.$invalid || policyConfigurationForm.$pristine"
@@ -172,7 +172,6 @@ <h5>{{policy.name}}</h5>
</div>
</div>


<md-button permission permission-only="'api-definition-d'" class="md-fab md-fab-bottom-right" aria-label="Add" ng-click="apiPoliciesCtrl.showAddPathModal($event)">
<ng-md-icon icon="add"></ng-md-icon>
</md-button>
@@ -26,6 +26,7 @@ class ApiPoliciesController {
private pathsToCompare: any;
private dndEnabled: boolean;
private pathsInitialized: any;
private schemaByPolicyId: any;

constructor (
private ApiService,
@@ -49,6 +50,7 @@ class ApiPoliciesController {
this.selectedApiPolicy = {};
this.httpMethods = ['GET','POST','PUT','DELETE','HEAD','PATCH','OPTIONS','TRACE','CONNECT'];
this.httpMethodsFilter = _.clone(this.httpMethods);
this.schemaByPolicyId = {};

this.listAllPolicies().then((policies) => {
_.forEach(policies, ({policy}) => {
@@ -178,16 +180,16 @@ class ApiPoliciesController {
editPolicy(index, path) {
this.$scope.policyJsonSchemaForm = ["*"];
this.selectedApiPolicy = this.apiPoliciesByPath[path][index];
if (this.selectedApiPolicy.schema === undefined) {
if (this.schemaByPolicyId[this.selectedApiPolicy.policyId] === undefined) {
return this.PolicyService.getSchema(this.selectedApiPolicy.policyId).then((response) => {
this.$scope.policyJsonSchema = this.selectedApiPolicy.schema = response.data;
this.$scope.policyJsonSchema = this.schemaByPolicyId[this.selectedApiPolicy.policyId] = response.data;
this.selectedApiPolicy[this.selectedApiPolicy.policyId] = this.selectedApiPolicy[this.selectedApiPolicy.policyId] || {};
this.checkEmptySchema();
return this.$scope.policyJsonSchema;
});
} else {
return this.$q(() => {
this.$scope.policyJsonSchema = this.selectedApiPolicy.schema;
this.$scope.policyJsonSchema = this.schemaByPolicyId[this.selectedApiPolicy.policyId];
this.checkEmptySchema();
});
}

0 comments on commit 5d809ac

Please sign in to comment.
You can’t perform that action at this time.