diff --git a/spec/AllocationManagerImpl.spec.ts b/spec/AllocationManagerImpl.spec.ts new file mode 100644 index 0000000..cad4d81 --- /dev/null +++ b/spec/AllocationManagerImpl.spec.ts @@ -0,0 +1,41 @@ +import Allocation from '../src/Allocation'; +import AllocationManagerImpl from '../src/AllocationManagerImpl'; +import ArrayModelStore from '../src/ArrayModelStore'; +import Developer from '../src/Developer'; +import Project from '../src/Project'; +import Technology from '../src/Technology'; + +describe('AllocationManagerImpl', function () { + describe('getFreeDevelopersForTechnology', function () { + it('should give empty array when no developer exists for technology', function () { + const developerStore = new ArrayModelStore(); + const projectStore = new ArrayModelStore(); + const allocationStore = new ArrayModelStore(); + const alloc_manager = new AllocationManagerImpl(projectStore, developerStore, allocationStore); + + const freeDevelopersForJava = alloc_manager.getFreeDevelopersForTechnology(Technology.JAVA); + + expect(freeDevelopersForJava).toEqual([]); + }); + }); + + describe('getFreeDevelopersForTechnology', function () { + it('should give free developers for technology', function () { + const developerStore = new ArrayModelStore(); + developerStore.add(new Developer({ id: 1, name: 'Kick', knownTechnologies: [Technology.JAVA] })); + developerStore.add(new Developer({ id: 2, name: 'Nick', knownTechnologies: [Technology.JAVA] })); + + const projectStore = new ArrayModelStore(); + const allocationStore = new ArrayModelStore(); + const alloc_manager = new AllocationManagerImpl(projectStore, developerStore, allocationStore); + + const freeDevelopersForJava = alloc_manager.getFreeDevelopersForTechnology(Technology.JAVA); + + expect(freeDevelopersForJava).toEqual([ + new Developer({ id: 1, name: 'Kick', knownTechnologies: [Technology.JAVA] }), + new Developer({ id: 2, name: 'Nick', knownTechnologies: [Technology.JAVA] }), + ]); + }); + }); + +}); diff --git a/src/AllocationManager.ts b/src/AllocationManager.ts index 0070cdd..e653997 100644 --- a/src/AllocationManager.ts +++ b/src/AllocationManager.ts @@ -8,6 +8,3 @@ export default interface AllocationManager { getFreeDevelopersForTechnology(technology: Technology): Developer[]; allocateDeveloperToProjectForTechnology(developer: Developer, project: Project, technology: Technology): void; } - - - diff --git a/src/AllocationManagerImpl.ts b/src/AllocationManagerImpl.ts index 999137f..4b41b88 100644 --- a/src/AllocationManagerImpl.ts +++ b/src/AllocationManagerImpl.ts @@ -1,42 +1,45 @@ import Allocation from './Allocation'; import AllocationManager from './AllocationManager'; import Developer from './Developer'; +import ModelStore from './ModelStore'; import Project from './Project'; -import { allocationStore, developerStore, projectStore } from './stores'; import Technology from './Technology'; export default class AllocationManagerImpl implements AllocationManager { - constructor() { } + constructor( + private projectStore: ModelStore, + private developerStore: ModelStore, + private allocationStore: ModelStore) { } getUnfulfilledRequirementsForProject(projectId: number): Technology[] { - const requiredTechnologies = projectStore.getById(projectId).requiredTechnologies; + const requiredTechnologies = this.projectStore.getById(projectId).requiredTechnologies; const fulfilledTechnologies = new Set(this.getFulfilledRequirementsForProject(projectId)); return requiredTechnologies.filter(technology => !fulfilledTechnologies.has(technology)); } getAllocatedDeveloperTechnologiesForProject(projectId: number): { developerId: number, technology: Technology }[] { - return allocationStore + return this.allocationStore .filter(alloc => alloc.projectId === projectId) .map(alloc => ({ developerId: alloc.developerId, technology: alloc.technology })); } getFreeDevelopersForTechnology(technology: Technology): Developer[] { - return developerStore + return this.developerStore .filter(developer => developer.knownTechnologies.some(t => t === technology)) .filter(developer => !this.getAllocatedDeveloperIds().some(id => id === developer.id)); } allocateDeveloperToProjectForTechnology(developer: Developer, project: Project, technology: Technology): void { - allocationStore.add(new Allocation(developer.id, project.id, technology)); + this.allocationStore.add(new Allocation(developer.id, project.id, technology)); } private getAllocatedDeveloperIds(): number[] { - return allocationStore.all().map(developer => developer.id); + return this.allocationStore.all().map(developer => developer.id); } private getFulfilledRequirementsForProject(projectId: number): Technology[] { - return allocationStore + return this.allocationStore .filter(alloc => alloc.projectId === projectId) .map(alloc => alloc.technology); } diff --git a/src/Developer.ts b/src/Developer.ts index 361fb56..97aa026 100644 --- a/src/Developer.ts +++ b/src/Developer.ts @@ -4,4 +4,9 @@ import Technology from './Technology'; export default class Developer extends Model { name: string; knownTechnologies: Technology[]; + + constructor(obj?: Partial) { + super(); + Object.assign(this, obj); + } } diff --git a/src/Technology.ts b/src/Technology.ts index 5fd849f..11cf75d 100644 --- a/src/Technology.ts +++ b/src/Technology.ts @@ -1,3 +1,7 @@ -type Technology = string; +enum Technology { + JAVA, + PYTHON, + JAVASCRIPT, +} export default Technology; diff --git a/src/index.ts b/src/index.ts index 4700047..ac3f702 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,11 @@ import Developer from "./Developer"; import { developerStore } from "./stores"; +import Technology from "./Technology"; const d = new Developer(); d.id = 1; -d.knownTechnologies = ['java', 'python']; +d.knownTechnologies = [Technology.JAVA, Technology.PYTHON]; developerStore.add(d); diff --git a/src/mockData.ts b/src/mockData.ts index ee697b6..07eabfb 100644 --- a/src/mockData.ts +++ b/src/mockData.ts @@ -1,24 +1,26 @@ +import Technology from './Technology'; + const MOCK_DATA = { DEVELOPERS: [ { id: 1, name: 'Kick Buttowski', - knownTechnologies: ['java', 'python', 'javascript'], + knownTechnologies: [Technology.JAVA, Technology.PYTHON, Technology.JAVASCRIPT], }, { id: 2, name: 'Courage C.D', - knownTechnologies: ['python'], + knownTechnologies: [Technology.PYTHON], }, ], PROJECTS: [ { id: 1, - requiredTechnologies: ['java'] + requiredTechnologies: [Technology.JAVA] }, { id: 2, - requiredTechnologies: ['python', 'javascript'] + requiredTechnologies: [Technology.PYTHON, Technology.JAVASCRIPT] } ] };