forked from asyncapi/parser-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add models for server object (asyncapi#497)
- Loading branch information
1 parent
d7e6bb0
commit d749906
Showing
15 changed files
with
256 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Disable specific duplicate code since it would introduce more complexity to reduce it. | ||
sonar.cpd.exclusions=src/models/**/*.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
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,10 @@ | ||
import { BaseModel } from "./base"; | ||
import { BindingsMixinInterface, DescriptionMixinInterface } from './mixins'; | ||
|
||
export interface ServerInterface extends BaseModel, DescriptionMixinInterface, BindingsMixinInterface { | ||
id(): string | ||
protocol(): string | undefined; | ||
protocolVersion(): string; | ||
hasProtocolVersion(): boolean; | ||
url(): string; | ||
} |
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,4 @@ | ||
import { Collection } from "./collection"; | ||
import {ServerInterface} from "./server"; | ||
|
||
export interface ServersInterface extends Collection<ServerInterface> {} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Mixin } from '../utils'; | ||
import { BaseModel } from '../base'; | ||
import { ServerInterface } from '../server'; | ||
import { DescriptionMixin } from './mixins/description'; | ||
import { BindingsMixin } from './mixins/bindings'; | ||
|
||
export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { | ||
constructor( | ||
private readonly _id: string, | ||
_json: Record<string, any> | ||
){ | ||
super(_json); | ||
} | ||
|
||
id(): string { | ||
return this._id; | ||
} | ||
|
||
protocol(): string | undefined { | ||
return this.json('protocol'); | ||
} | ||
|
||
hasProtocolVersion(): boolean { | ||
return !!this.json('protocolVersion'); | ||
} | ||
|
||
protocolVersion(): string { | ||
return this.json('protocolVersion'); | ||
} | ||
|
||
url(): string { | ||
return this.json('url'); | ||
} | ||
} |
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,13 @@ | ||
import { Collection } from '../collection'; | ||
import { ServerInterface } from '../server'; | ||
import { ServersInterface } from '../servers'; | ||
|
||
export class Servers extends Collection<ServerInterface> implements ServersInterface { | ||
override get(id: string): ServerInterface | undefined { | ||
return this.collections.find(server => server.id() === id); | ||
} | ||
|
||
override has(id: string): boolean { | ||
return this.collections.some(server => server.id() === id); | ||
} | ||
} |
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
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,34 @@ | ||
import { Mixin } from '../utils'; | ||
import { BaseModel } from '../base'; | ||
import { ServerInterface } from '../server'; | ||
import { DescriptionMixin } from './mixins/description'; | ||
import { BindingsMixin } from './mixins/bindings'; | ||
|
||
export class Server extends Mixin(BaseModel, DescriptionMixin, BindingsMixin) implements ServerInterface { | ||
constructor( | ||
private readonly _id: string, | ||
_json: Record<string, any> | ||
){ | ||
super(_json); | ||
} | ||
|
||
id(): string { | ||
return this._id; | ||
} | ||
|
||
protocol(): string | undefined { | ||
return this.json('protocol'); | ||
} | ||
|
||
hasProtocolVersion(): boolean { | ||
return !!this.json('protocolVersion'); | ||
} | ||
|
||
protocolVersion(): string { | ||
return this.json('protocolVersion'); | ||
} | ||
|
||
url(): string { | ||
return this.json('url'); | ||
} | ||
} |
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,13 @@ | ||
import { Collection } from '../collection'; | ||
import { ServerInterface } from '../server'; | ||
import { ServersInterface } from '../servers'; | ||
|
||
export class Servers extends Collection<ServerInterface> implements ServersInterface { | ||
override get(id: string): ServerInterface | undefined { | ||
return this.collections.find(server => server.id() === id); | ||
} | ||
|
||
override has(id: string): boolean { | ||
return this.collections.some(server => server.id() === id); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Server } from '../../../src/models/v2/server'; | ||
import { Servers } from '../../../src/models/v2/servers'; | ||
|
||
const doc = { | ||
'development': { | ||
protocol: 'mqtt', | ||
protocolVersion: '1.0.0', | ||
url: 'development.gigantic-server.com' | ||
} | ||
}; | ||
const docItem = new Server('development', doc.development); | ||
const emptyItem = new Server('',{}); | ||
|
||
describe('Servers model', function () { | ||
describe('.isEmpty()', function () { | ||
it('should return true if collection is empty', function () { | ||
const servers = new Servers([]); | ||
expect(servers.isEmpty()).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if collection is not empty', function () { | ||
const servers = new Servers([docItem]); | ||
expect(servers.isEmpty()).toBeFalsy(); | ||
}); | ||
}) | ||
|
||
describe('.get(id)', function () { | ||
it('should return a specific server Object if it is present', function () { | ||
const servers = new Servers([docItem]); | ||
expect(servers.get('development')).toBeTruthy(); | ||
}); | ||
|
||
it('should return undefined if a server is said Id is missing ', function () { | ||
const servers = new Servers([]); | ||
expect(servers.get('development')).toBeUndefined(); | ||
}); | ||
}) | ||
|
||
describe('.has(id)', function () { | ||
|
||
const servers = new Servers([docItem]); | ||
|
||
it('should return true if the said name is available', function () { | ||
expect(servers.has('development')).toBeTruthy(); | ||
}) | ||
|
||
it('should return false if the server name is missing', function () { | ||
expect(servers.has('production')).toBeFalsy(); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Server Model', function () { | ||
|
||
describe('.id()', function () { | ||
it('should return name if present', function () { | ||
expect(docItem.id()).toMatch('development'); | ||
}); | ||
}); | ||
|
||
describe('protocol()', function () { | ||
it('should return protocol ', function () { | ||
expect(docItem.protocol()).toMatch(doc.development.protocol); | ||
}); | ||
}); | ||
|
||
describe('.hasProtocolVersion()', function () { | ||
it('should return true if protocolVersion is not missing', function () { | ||
expect(docItem.hasProtocolVersion()).toBeTruthy(); | ||
}); | ||
|
||
it('should be false when protocolVersion is missing', function () { | ||
expect(emptyItem.hasProtocolVersion()).toBeFalsy(); | ||
}); | ||
}) | ||
|
||
describe('.protocolVersion()', function () { | ||
it('should return protocolVersion', function () { | ||
expect(docItem.protocolVersion()).toMatch(doc.development.protocolVersion); | ||
}); | ||
|
||
it('should return undefined protocolVersion when protocolVersion is missing', function () { | ||
expect(emptyItem.protocolVersion()).toBeUndefined(); | ||
}) | ||
}) | ||
|
||
describe('.url()', function () { | ||
it('should return url', function () { | ||
expect(docItem.url()).toMatch(doc.development.url); | ||
}); | ||
}); | ||
}) |