-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8777a3
commit a310408
Showing
9 changed files
with
207 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/policy-engine/src/app/core/factory/data-store-repository.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { FileSystemDataStoreRepository } from '../../persistence/repository/file-system-data-store.repository' | ||
import { HttpDataStoreRepository } from '../../persistence/repository/http-data-store.repository' | ||
import { DataStoreException } from '../exception/data-store.exception' | ||
import { DataStoreRepository } from '../repository/data-store.repository' | ||
|
||
@Injectable() | ||
export class DataStoreRepositoryFactory { | ||
constructor( | ||
private fileSystemRepository: FileSystemDataStoreRepository, | ||
private httpRepository: HttpDataStoreRepository | ||
) {} | ||
|
||
getRepository(url: string): DataStoreRepository { | ||
switch (this.getProtocol(url)) { | ||
case 'file': | ||
return this.fileSystemRepository | ||
case 'http': | ||
case 'https': | ||
return this.httpRepository | ||
default: | ||
throw new DataStoreException({ | ||
message: 'Data store URL protocol not supported', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { url } | ||
}) | ||
} | ||
} | ||
|
||
private getProtocol(url: string): string { | ||
return url.split(':')[0] | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
apps/policy-engine/src/app/core/service/__test__/integration/data-store.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { DataStoreConfiguration, EntityData, EntitySignature, FIXTURE } from '@narval/policy-engine-shared' | ||
import { HttpModule } from '@nestjs/axios' | ||
import { HttpStatus } from '@nestjs/common' | ||
import { Test } from '@nestjs/testing' | ||
import nock from 'nock' | ||
import { FileSystemDataStoreRepository } from '../../../../../app/persistence/repository/file-system-data-store.repository' | ||
import { HttpDataStoreRepository } from '../../../../../app/persistence/repository/http-data-store.repository' | ||
import { withTempJsonFile } from '../../../../../shared/testing/with-temp-json-file.testing' | ||
import { DataStoreRepositoryFactory } from '../../../factory/data-store-repository.factory' | ||
import { DataStoreService } from '../../data-store.service' | ||
|
||
describe(DataStoreService.name, () => { | ||
let service: DataStoreService | ||
|
||
const remoteDataStoreUrl = 'http://9.9.9.9:9000' | ||
|
||
const entityDataStore: EntityData = { | ||
entity: { | ||
data: FIXTURE.ENTITIES | ||
} | ||
} | ||
|
||
const entitySignatureStore: EntitySignature = { | ||
entity: { | ||
signature: 'test-signature' | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [HttpModule], | ||
providers: [DataStoreService, DataStoreRepositoryFactory, HttpDataStoreRepository, FileSystemDataStoreRepository] | ||
}).compile() | ||
|
||
service = module.get<DataStoreService>(DataStoreService) | ||
}) | ||
|
||
describe('fetch', () => { | ||
it('fetches data and signature from distinct stores', async () => { | ||
nock(remoteDataStoreUrl).get('/').reply(HttpStatus.OK, entityDataStore) | ||
|
||
await withTempJsonFile(JSON.stringify(entitySignatureStore), async (path) => { | ||
const url = `file://${path}` | ||
const config: DataStoreConfiguration = { | ||
dataUrl: remoteDataStoreUrl, | ||
signatureUrl: url, | ||
keys: [] | ||
} | ||
|
||
const { entity } = await service.fetch(config) | ||
|
||
expect(entity.data).toEqual(entityDataStore.entity.data) | ||
expect(entity.signature).toEqual(entitySignatureStore.entity.signature) | ||
}) | ||
}) | ||
}) | ||
}) |
51 changes: 51 additions & 0 deletions
51
apps/policy-engine/src/app/core/service/data-store.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { DataStoreConfiguration, entityDataSchema, entitySignatureSchema } from '@narval/policy-engine-shared' | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { ZodObject, z } from 'zod' | ||
import { DataStoreException } from '../exception/data-store.exception' | ||
import { DataStoreRepositoryFactory } from '../factory/data-store-repository.factory' | ||
|
||
@Injectable() | ||
export class DataStoreService { | ||
constructor(private dataStoreRepositoryFactory: DataStoreRepositoryFactory) {} | ||
|
||
async fetch(config: DataStoreConfiguration) { | ||
const [entityData, entitySignature] = await Promise.all([ | ||
this.fetchByUrl(config.dataUrl, entityDataSchema), | ||
this.fetchByUrl(config.signatureUrl, entitySignatureSchema) | ||
]) | ||
|
||
return { | ||
entity: { | ||
data: entityData.entity.data, | ||
signature: entitySignature.entity.signature | ||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private async fetchByUrl<DataSchema extends ZodObject<any>>( | ||
url: string, | ||
schema: DataSchema | ||
): Promise<z.infer<typeof schema>> { | ||
const data = await this.dataStoreRepositoryFactory.getRepository(url).fetch(url) | ||
const result = schema.safeParse(data) | ||
|
||
if (result.success) { | ||
return result.data | ||
} | ||
|
||
throw new DataStoreException({ | ||
message: 'Invalid store schema', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { | ||
...(schema.description ? { schema: schema.description } : {}), | ||
url, | ||
errors: result.error.errors.map(({ path, message, code }) => ({ | ||
path, | ||
code, | ||
message | ||
})) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
apps/policy-engine/src/shared/testing/with-temp-json-file.testing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { unlink, writeFile } from 'fs/promises' | ||
import { v4 as uuid } from 'uuid' | ||
|
||
/** | ||
* Executes a callback function with a temporary JSON file. | ||
* | ||
* The file is created with the provided data and deleted after the callback is | ||
* executed. | ||
* | ||
* @param data - The data to be written to the temporary JSON file. | ||
* @param thunk - The callback function to be executed with the path of the | ||
* temporary JSON file. | ||
*/ | ||
export const withTempJsonFile = async (data: string, thunk: (path: string) => void | Promise<void>) => { | ||
const path = `./test-temp-data-store-${uuid()}.json` | ||
|
||
await writeFile(path, data, 'utf-8') | ||
|
||
try { | ||
await thunk(path) | ||
} finally { | ||
await unlink(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters