jec-sandcat
Version:
JEC Sandcat - The default RESTful web services framework for GlassCat applications.
161 lines (147 loc) • 6.02 kB
text/typescript
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
//
// Copyright 2016-2018 Pascal ECHEMANN.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { TestSuite, Test, BeforeAll, AfterAll, Before, After, TestSorters } from "jec-juta";
import { expect } from "chai";
import * as sinon from "sinon";
import { ResourceDescriptorUtil } from "../../../../../src/com/onsoft/sandcat/utils/ResourceDescriptorUtil";
import { DefaultSandcatContainer } from "../../../../../src/com/onsoft/sandcat/core/DefaultSandcatContainer";
import { ResourceDescriptor } from "../../../../../src/com/onsoft/sandcat/reflect/ResourceDescriptor";
import { MethodDescriptor } from "../../../../../src/com/onsoft/sandcat/reflect/MethodDescriptor";
import { Sandcat } from "../../../../../src/com/onsoft/sandcat/Sandcat";
import { ParametersMapUtil } from "../../../../../src/com/onsoft/sandcat//utils/ParametersMapUtil";
import { HttpMethod } from "jec-commons";
import { ResourceDescriptorRegistry } from "../../../../../src/com/onsoft/sandcat/metadata/ResourceDescriptorRegistry";
export class ResourceDescriptorUtilTest {
public descriptorUtil:ResourceDescriptorUtil = null;
public descriptor:ResourceDescriptor = null;
public methodDescriptor:MethodDescriptor = null;
public sandcatContainer:Sandcat = null;
public resource:any = null;
public initTest():void {
this.sandcatContainer = new DefaultSandcatContainer();
this.descriptor = new ResourceDescriptor();
this.descriptor.contextRoot = "foo";
this.descriptor.resourcePath = "/resourcePath";
ResourceDescriptorRegistry.registerDescriptor(this.descriptor);
this.methodDescriptor = new MethodDescriptor();
this.methodDescriptor.name = "methodDescriptorName";
this.methodDescriptor.httpMethod = HttpMethod.GET;
this.methodDescriptor.route = "/bar";
}
public initResourceTest():void {
this.resource = {};
}
public resetTest():void {
ResourceDescriptorRegistry.registerDescriptor(null);
this.sandcatContainer = null;
this.descriptor = null;
}
public resetResource():void {
this.resource = null;
}
public decoratePropertyTest():void {
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
this.descriptorUtil.decorate();
expect(this.resource).to.have.property("__sandcatResourceDescriptor");
}
public decorateImmutablePropertyTest():void {
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
this.descriptorUtil.decorate();
const doOverride:Function = function():void {
this.resource.__sandcatResourceDescriptor = {};
};
expect(doOverride.bind(this)).to.throw(TypeError);
}
public decorateMethodTest():void {
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
this.descriptorUtil.decorate();
expect(this.resource.getResourceDescriptor).to.not.be.undefined;
}
public decorateImmutableMethodTest():void {
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
this.descriptorUtil.decorate();
const doOverride:Function = function():void {
this.resource.getResourceDescriptor = function():void {};
};
expect(doOverride.bind(this)).to.throw(TypeError);
}
public fixCompositeValuesTest():void {
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
expect(this.descriptorUtil.fixCompositeValues()).to.be.undefined;
}
public parametersMapUtilTest():void {
const spy:any = sinon.spy(ParametersMapUtil, "getParameterCollection");
this.descriptor.addMethod(this.methodDescriptor);
this.descriptorUtil = new ResourceDescriptorUtil(
this.resource, this.descriptor, this.sandcatContainer
);
this.descriptorUtil.fixCompositeValues();
sinon.assert.calledWith(spy, this.methodDescriptor.name);
sinon.restore();
}
public setMethodUrlPatternsTest():void {
const pattern:string = this.methodDescriptor.urlPatterns[0];
expect(pattern).to.include(this.descriptor.contextRoot);
expect(pattern).to.include(this.descriptor.resourcePath);
this.descriptor.methodsMap.clear();
}
}