Skip to content

Commit

Permalink
feat(plugin-registry): get keychain by keychainId
Browse files Browse the repository at this point in the history
Adds a utility method where I can just pass in the
keychain ID and get back a plugin instance or an
exception thrown if it was not found.

Fixes hyperledger-cacti#381

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Dec 15, 2020
1 parent cbbcbb2 commit 4d93c72
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Optional } from "typescript-optional";
import { ICactusPlugin, isICactusPlugin } from "../plugin/i-cactus-plugin";
import { PluginAspect } from "../plugin/plugin-aspect";
import { IPluginKeychain } from "./keychain/i-plugin-keychain";

/**
* This interface describes the constructor options object that can be used to provide configuration parameters to
Expand Down Expand Up @@ -80,6 +81,21 @@ export class PluginRegistry {
return Optional.ofNullable(plugin as T);
}

public findOneByKeychainId<T extends IPluginKeychain>(keychainId: string): T {
const fnTag = "PluginRegistry#findOneByKeychainId()";
if (typeof keychainId !== "string" || keychainId.trim().length < 1) {
throw new Error(`${fnTag} need keychainId arg as non-blank string.`);
}

const plugin = this.findManyByAspect<IPluginKeychain>(
PluginAspect.KEYCHAIN
).find((keychainPlugin) => keychainPlugin.getKeychainId() === keychainId);

return Optional.ofNullable(plugin as T).orElseThrow(
() => new Error(`${fnTag} No keychain found for ID ${keychainId}`)
);
}

public findManyByAspect<T extends ICactusPlugin>(aspect: PluginAspect): T[] {
return this.getPlugins().filter((p) => p.getAspect() === aspect) as T[];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test, { Test } from "tape";
import { v4 as uuidv4 } from "uuid";

import {
ICactusPlugin,
IPluginKeychain,
PluginAspect,
PluginRegistry,
} from "../../../main/typescript/public-api";

test("PluginRegistry", (tMain: Test) => {
test("findOneByKeychainId() finds plugin by keychain ID", (t: Test) => {
const keychainId = uuidv4();

const mockKeychainPlugin = {
getKeychainId: () => keychainId,
getAspect: () => PluginAspect.KEYCHAIN,
} as IPluginKeychain;

const pluginRegistry = new PluginRegistry({
plugins: [
mockKeychainPlugin,
{
getAspect: () => PluginAspect.CONSORTIUM,
} as ICactusPlugin,
{
getAspect: () => PluginAspect.KV_STORAGE,
} as ICactusPlugin,
{
getAspect: () => PluginAspect.LEDGER_CONNECTOR,
} as ICactusPlugin,
],
});

t.doesNotThrow(() => pluginRegistry.findOneByKeychainId(keychainId));
const keychainPlugin = pluginRegistry.findOneByKeychainId(keychainId);
t.equal(keychainPlugin, mockKeychainPlugin, "Finds same object OK");

t.throws(
() => pluginRegistry.findOneByKeychainId(""),
/need keychainId arg as non-blank string/,
"Check for keychain ID blankness OK"
);
t.throws(
() => pluginRegistry.findOneByKeychainId("x"),
/No keychain found for ID/,
"Throws for keychain not found OK"
);

t.end();
});

tMain.end();
});

0 comments on commit 4d93c72

Please sign in to comment.