Skip to content

Commit

Permalink
Fixed issue where an unknown exchange type would cause an exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlivingrooms committed Apr 18, 2018
1 parent ff01983 commit 1d119f5
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
Binary file added ExampleDG.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ $ npm install --save rabbit-mermaid
## Usage

```js
const rabbitmermaid = require("rabbit-mermaid");
const { MermaidGenerator } = require("rabbit-mermaid");
const topology = require("./topology.json");

rabbitmermaid(topology);
const mermaidGenerator = new MermaidGenerator(topology);
mermaidGenerator.generate();
```

## License
Expand Down
15 changes: 15 additions & 0 deletions lib/__tests__/mermaid-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fanout = require("./fanout.json");
const topic = require("./topic.json");
const multiple = require("./multiple.json");
const exchangeToExhange = require("./exchangeToExchange.json");
const unknownExchange = require("./unknownExchange.json");

describe("MermaidGenerator", () => {
it("generates a string", () => {
Expand All @@ -26,6 +27,20 @@ end`;
assert.deepEqual(mermaidGenerator.generate(), expectedString);
});

it("ignores exchange types it does not recognize", () => {
const mermaidGenerator = new MermaidGenerator(unknownExchange, {});
const expectedString = `graph LR
subgraph Images
Images(("Images<br/>(direct)"))
Images --routing_key=images.archive--> archiver1("archiver1")
Images --routing_key=images.archive--> archiver2("archiver2")
Images --routing_key=images.crop--> cropper("cropper")
Images --routing_key=images.resize--> resizer("resizer")
end`;

assert.deepEqual(mermaidGenerator.generate(), expectedString);
});

it("generates a fanout exchange block with queue bindings", () => {
const mermaidGenerator = new MermaidGenerator(fanout, {});
const expectedString = `graph LR
Expand Down
94 changes: 94 additions & 0 deletions lib/__tests__/unknownExchange.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"exchanges": [
{
"name": "images",
"vhost": "/",
"type": "direct",
"durable": true,
"auto_delete": false,
"internal": false,
"arguments": {}
},
{
"name": "breaker",
"vhost": "/",
"type": "header",
"durable": true,
"auto_delete": false,
"internal": false,
"arguments": {}
}
],
"queues": [
{
"name": "archiver1",
"vhost": "/",
"durable": true,
"auto_delete": false,
"arguments": {}
},
{
"name": "archiver2",
"vhost": "/",
"durable": true,
"auto_delete": false,
"arguments": {}
},
{
"name": "cropper",
"vhost": "/",
"durable": true,
"auto_delete": false,
"arguments": {}
},
{
"name": "resizer",
"vhost": "/",
"durable": true,
"auto_delete": false,
"arguments": {}
}
],
"bindings": [
{
"source": "images",
"vhost": "/",
"destination": "archiver1",
"destination_type": "queue",
"routing_key": "images.archive",
"arguments": {}
},
{
"source": "breaker",
"vhost": "/",
"destination": "archiver1",
"destination_type": "queue",
"routing_key": "images.archive",
"arguments": {}
},
{
"source": "images",
"vhost": "/",
"destination": "archiver2",
"destination_type": "queue",
"routing_key": "images.archive",
"arguments": {}
},
{
"source": "images",
"vhost": "/",
"destination": "cropper",
"destination_type": "queue",
"routing_key": "images.crop",
"arguments": {}
},
{
"source": "images",
"vhost": "/",
"destination": "resizer",
"destination_type": "queue",
"routing_key": "images.resize",
"arguments": {}
}
]
}
9 changes: 8 additions & 1 deletion lib/mermaid-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ class MermaidGenerator {
return format;
}

isSupportedExchange(ex) {
return ex.type === "direct" || ex.type === "fanout" || ex.type === "topic";
}

processExchangeBlocks(json) {
const exchanges = json.exchanges;
const exBuffer = [];
exchanges.forEach(ex => {
const supportedExchanges = exchanges.filter(ex =>
this.isSupportedExchange(ex)
);
supportedExchanges.forEach(ex => {
exBuffer.push(this.genExchangeBlock(ex));
});

Expand Down

0 comments on commit 1d119f5

Please sign in to comment.