forked from CUBRID/cubrid-webmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker.service.spec.ts
More file actions
136 lines (116 loc) · 3.95 KB
/
broker.service.spec.ts
File metadata and controls
136 lines (116 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Test, TestingModule } from '@nestjs/testing';
import { BrokerService } from './broker.service';
import { HostService } from '@host';
import { CmsHttpsClientService } from '@cms-https-client/cms-https-client.service';
import { BrokerError } from '@error/broker/broker-error';
import * as common from '@common';
jest.mock('@common', () => ({
...jest.requireActual('@common'),
checkCmsTokenError: jest.fn(),
checkCmsStatusError: jest.fn(),
}));
describe('BrokerService', () => {
let service: BrokerService;
let hostService: jest.Mocked<HostService>;
let cmsClient: jest.Mocked<CmsHttpsClientService>;
const mockHost = {
uid: 'host-uid-1',
id: 'host-1',
address: 'localhost',
port: 8001,
password: 'host-password',
token: 'test-token',
dbProfiles: {},
};
const mockUserId = 'user-123';
const mockHostUid = 'host-uid-1';
beforeEach(async () => {
const mockHostService = { findHostInternal: jest.fn() };
const mockCmsClient = { postAuthenticated: jest.fn() };
const module: TestingModule = await Test.createTestingModule({
providers: [
BrokerService,
{ provide: HostService, useValue: mockHostService },
{ provide: CmsHttpsClientService, useValue: mockCmsClient },
],
}).compile();
service = module.get(BrokerService);
hostService = module.get(HostService);
cmsClient = module.get(CmsHttpsClientService);
hostService.findHostInternal.mockResolvedValue(mockHost);
(common.checkCmsTokenError as jest.Mock).mockImplementation(() => {});
(common.checkCmsStatusError as jest.Mock).mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('startAllBrokers', () => {
it('should send startbroker task and return { success: true }', async () => {
const mockResponse = {
__EXEC_TIME: '72 ms',
note: 'none',
status: 'success',
task: 'startbroker',
};
cmsClient.postAuthenticated.mockResolvedValue(mockResponse);
const result = await service.startAllBrokers(mockUserId, mockHostUid);
expect(hostService.findHostInternal).toHaveBeenCalledWith(
mockUserId,
mockHostUid
);
expect(cmsClient.postAuthenticated).toHaveBeenCalledWith(
`https://${mockHost.address}:${mockHost.port}/cm_api`,
expect.objectContaining({
task: 'startbroker',
token: mockHost.token,
})
);
expect(result).toEqual({ success: true });
});
it('should throw BrokerError when CMS status is not success', async () => {
cmsClient.postAuthenticated.mockResolvedValue({
__EXEC_TIME: '0 ms',
note: 'failed',
status: 'fail',
task: 'startbroker',
});
await expect(
service.startAllBrokers(mockUserId, mockHostUid)
).rejects.toThrow(BrokerError);
});
});
describe('stopAllBrokers', () => {
it('should send stopbroker task and return { success: true }', async () => {
const mockResponse = {
__EXEC_TIME: '72 ms',
note: 'none',
status: 'success',
task: 'stopbroker',
};
cmsClient.postAuthenticated.mockResolvedValue(mockResponse);
const result = await service.stopAllBrokers(mockUserId, mockHostUid);
expect(cmsClient.postAuthenticated).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
task: 'stopbroker',
token: mockHost.token,
})
);
expect(result).toEqual({ success: true });
});
it('should throw BrokerError when CMS status is not success', async () => {
cmsClient.postAuthenticated.mockResolvedValue({
__EXEC_TIME: '0 ms',
note: 'failed',
status: 'fail',
task: 'stopbroker',
});
await expect(
service.stopAllBrokers(mockUserId, mockHostUid)
).rejects.toThrow(BrokerError);
});
});
});