Commit 86118dee authored by mo khan's avatar mo khan

Introduce ObjectMother for report objects

* https://martinfowler.com/bliki/ObjectMother.html
parent 712f0771
......@@ -32,23 +32,19 @@ export default class V2Report {
if (matchingLicense) matchingLicense.count += 1;
}
mapFromDependency(dependency) {
const combinedLicense = this.combine(dependency.licenses, license => {
mapFromDependency({ name, description, url, licenses }) {
const combinedLicense = this.combine(licenses, license => {
this.incrementCountFor(license.name);
});
return {
license: combinedLicense,
dependency: {
name: dependency.name,
url: dependency.url,
description: dependency.description,
},
dependency: { name, url, description },
};
}
static mapFromLicense(license) {
return { name: license.name, count: 0 };
static mapFromLicense({ name, url = '', count = 0 }) {
return { name, url, count };
}
static createLicenseMap(licenses) {
......
import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants';
const urlFor = ({ scheme = 'https', host = 'www.example.org', path = '/' }) =>
`${scheme}://${host}${path}`;
const licenseUrlFor = name =>
urlFor({ host: 'opensource.org', path: `/licenses/${name.split(' ')[0]}` });
const dependencyUrlFor = name => urlFor({ path: `/${name}` });
const normalizeV1License = ({ name, url = licenseUrlFor(name) }) => ({ name, url });
const V1 = {
normalizeLicenseSummary: ({ name, url = licenseUrlFor(name), count = 0 }) => ({
name,
url,
count,
}),
normalizeDependency: ({
name,
url = dependencyUrlFor(name),
description = name.toUpperCase(),
license = {},
}) => ({ dependency: { name, url, description }, license: normalizeV1License(license) }),
};
const V2 = {
normalizeLicenseSummary: ({ id, name, url = licenseUrlFor(id) }) => ({ id, name, url }),
normalizeDependency: ({
name,
url = dependencyUrlFor(name),
description = name.toUpperCase(),
licenses = [],
}) => ({ name, url, licenses, description }),
};
export class Builder {
static for(version, template = { licenses: [], dependencies: [] }) {
return new Builder(version, template);
}
static forV1() {
return this.for(V1);
}
static forV2() {
return this.for(V2, { version: '2.0', licenses: [], dependencies: [] });
}
constructor(version, template = { licenses: [], dependencies: [] }) {
this.report = template;
this.version = version;
}
addLicense(license) {
this.report.licenses.push(this.version.normalizeLicenseSummary(license));
return this;
}
addDependency(dependency) {
this.report.dependencies.push(this.version.normalizeDependency(dependency));
return this;
}
build(override = {}) {
return Object.assign(this.report, override);
}
}
export const approvedLicense = {
id: 5,
name: 'MIT',
......@@ -12,94 +74,46 @@ export const blacklistedLicense = {
approvalStatus: LICENSE_APPROVAL_STATUS.BLACKLISTED,
};
export const licenseBaseIssues = {
licenses: [
{
count: 1,
name: 'MIT',
},
],
dependencies: [
{
license: {
name: 'MIT',
url: 'http://opensource.org/licenses/mit-license',
},
dependency: {
export const licenseBaseIssues = Builder.forV1()
.addLicense({ name: 'MIT', count: 1 })
.addDependency({
name: 'bundler',
url: 'http://bundler.io',
description: "The best way to manage your application's dependencies",
paths: ['.'],
},
},
],
};
license: { name: 'MIT', url: 'http://opensource.org/licenses/mit-license' },
})
.build();
export const licenseHeadIssues = {
licenses: [
{
count: 3,
name: 'New BSD',
},
{
count: 1,
name: 'MIT',
},
],
dependencies: [
{
license: {
name: 'New BSD',
url: 'http://opensource.org/licenses/BSD-3-Clause',
},
dependency: {
export const licenseHeadIssues = Builder.forV1()
.addLicense({ name: 'New BSD', count: 3 })
.addLicense({ name: 'MIT', count: 1 })
.addDependency({
name: 'pg',
url: 'https://bitbucket.org/ged/ruby-pg',
description:
'Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/]',
paths: ['.'],
},
},
{
license: {
name: 'New BSD',
url: 'http://opensource.org/licenses/BSD-3-Clause',
},
dependency: {
description: 'Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/]',
license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
})
.addDependency({
name: 'puma',
url: 'http://puma.io',
description:
'Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
paths: ['.'],
},
},
{
license: {
name: 'New BSD',
url: 'http://opensource.org/licenses/BSD-3-Clause',
},
dependency: {
license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
})
.addDependency({
name: 'foo',
url: 'http://foo.io',
description:
'Foo is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
paths: ['.'],
},
},
{
license: {
name: 'MIT',
url: 'http://opensource.org/licenses/mit-license',
},
dependency: {
license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
})
.addDependency({
name: 'execjs',
url: 'https://github.com/rails/execjs',
description: 'Run JavaScript code from Ruby',
paths: ['.'],
},
},
],
};
license: { name: 'MIT', url: 'http://opensource.org/licenses/mit-license' },
})
.build();
export const licenseReport = [
{
......
import { Builder } from '../../license_management/mock_data';
describe('build', () => {
it('creates a v1 report', () => {
const result = Builder.forV1()
.addLicense({ name: 'MIT License' })
.addDependency({ name: 'rails', license: { name: 'MIT License' } })
.build();
expect(result).toMatchObject({
licenses: [{ name: 'MIT License', url: 'https://opensource.org/licenses/MIT', count: 0 }],
dependencies: [
{
license: { name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
dependency: { name: 'rails', description: 'RAILS', url: 'https://www.example.org/rails' },
},
],
});
});
it('creates a v2 report', () => {
const result = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addDependency({ name: 'rails', licenses: ['MIT'] })
.build();
expect(result).toMatchObject({
version: '2.0',
licenses: [{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }],
dependencies: [{ name: 'rails', description: 'RAILS', licenses: ['MIT'] }],
});
});
});
import ReportMapper from 'ee/vue_shared/license_management/report_mapper';
import { Builder } from '../../license_management/mock_data';
describe('mapFrom', () => {
let subject = null;
......@@ -8,80 +9,34 @@ describe('mapFrom', () => {
});
it('converts a v2 schema report to v1', () => {
const report = {
version: '2.0',
licenses: [
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' },
],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const result = subject.mapFrom(report);
const report = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addLicense({ id: 'BSD', name: 'BSD License' })
.addDependency({ name: 'x', licenses: ['MIT'] })
.addDependency({ name: 'y', licenses: ['BSD'] })
.addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
.build();
expect(result).toMatchObject({
licenses: [{ name: 'BSD License', count: 2 }, { name: 'MIT License', count: 2 }],
dependencies: [
{
license: {
name: 'MIT License',
url: 'https://opensource.org/licenses/MIT',
},
dependency: {
name: 'x',
url: 'https://www.example.com/x',
description: 'X',
},
},
{
license: {
name: 'BSD License',
url: 'https://opensource.org/licenses/BSD',
},
dependency: {
name: 'y',
url: 'https://www.example.com/y',
description: 'Y',
},
},
{
license: {
name: 'BSD License, MIT License',
url: '',
},
dependency: {
name: 'z',
url: 'https://www.example.com/z',
description: 'Z',
},
},
],
});
const result = subject.mapFrom(report);
expect(result).toMatchObject(
Builder.forV1()
.addLicense({ name: 'BSD License', count: 2 })
.addLicense({ name: 'MIT License', count: 2 })
.addDependency({ name: 'x', license: { name: 'MIT License' } })
.addDependency({ name: 'y', license: { name: 'BSD License' } })
.addDependency({ name: 'z', license: { name: 'BSD License, MIT License', url: '' } })
.build(),
);
});
it('returns a v1 schema report', () => {
const report = {
licenses: [],
dependencies: [],
};
const report = Builder.forV1().build();
expect(subject.mapFrom(report)).toBe(report);
});
it('returns a v1.1 schema report', () => {
const report = {
version: '1.1',
licenses: [],
dependencies: [],
};
const report = Builder.forV1().build({ version: '1.1' });
expect(subject.mapFrom(report)).toBe(report);
});
......
......@@ -9,6 +9,7 @@ import {
import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants';
import { STATUS_FAILED, STATUS_NEUTRAL, STATUS_SUCCESS } from '~/reports/constants';
import {
Builder,
approvedLicense,
blacklistedLicense,
licenseHeadIssues,
......@@ -65,31 +66,18 @@ describe('utils', () => {
it('compares a v2 report with a v2 report', () => {
const policies = [{ id: 100, name: 'BSD License', approvalStatus: 'blacklisted' }];
const baseReport = {
version: '2.0',
licenses: [{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
],
};
const headReport = {
version: '2.0',
licenses: [
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' },
],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const baseReport = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addDependency({ name: 'x', licenses: ['MIT'] })
.build();
const headReport = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addLicense({ id: 'BSD', name: 'BSD License' })
.addDependency({ name: 'x', licenses: ['MIT'] })
.addDependency({ name: 'y', licenses: ['BSD'] })
.addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
.build();
const result = parseLicenseReportMetrics(headReport, baseReport, policies);
......@@ -101,43 +89,29 @@ describe('utils', () => {
count: 2,
status: 'failed',
name: 'BSD License',
packages: [{ name: 'y', url: 'https://www.example.com/y', description: 'Y' }],
url: 'https://opensource.org/licenses/BSD',
packages: [{ name: 'y', url: 'https://www.example.org/y', description: 'Y' }],
}),
);
});
it('compares a v1 report with a v2 report', () => {
const policies = [{ id: 101, name: 'BSD License', approvalStatus: 'blacklisted' }];
const baseReport = {
licenses: [{ name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }],
dependencies: [
{
license: {
name: 'MIT License',
url: 'https://opensource.org/licenses/MIT',
},
dependency: { name: 'x', url: 'https://www.example.com/x', description: 'X' },
},
],
};
const headReport = {
version: '2.0',
licenses: [
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' },
],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const baseReport = Builder.forV1()
.addLicense({ name: 'MIT License' })
.addDependency({
name: 'x',
license: { name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
})
.build();
const headReport = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addLicense({ id: 'BSD', name: 'BSD License' })
.addDependency({ name: 'x', licenses: ['MIT'] })
.addDependency({ name: 'y', licenses: ['BSD'] })
.addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
.build();
const result = parseLicenseReportMetrics(headReport, baseReport, policies);
......@@ -149,7 +123,8 @@ describe('utils', () => {
count: 2,
status: 'failed',
name: 'BSD License',
packages: [{ name: 'y', url: 'https://www.example.com/y', description: 'Y' }],
url: 'https://opensource.org/licenses/BSD',
packages: [{ name: 'y', url: 'https://www.example.org/y', description: 'Y' }],
}),
);
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment