Skip to content

Commit 8cd02c8

Browse files
aledsagechristian-kreuzberger-dtx
authored andcommitted
Fix integration tests for find-monitored-entity
- Use findMonitoredEntitiesByName (the old findMonitoredEntityByName no longer exists) - Result is no longer a string, but a DqlExecutionResult - Fix when no entities provided (throw exception) - Removed test for empty string (instead have test for empty list of entities)
1 parent af6ecdc commit 8cd02c8

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

integration-tests/find-monitored-entity-by-name.integration.test.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import { config } from 'dotenv';
99
import { createDtHttpClient } from '../src/authentication/dynatrace-clients';
10-
import { findMonitoredEntityByName } from '../src/capabilities/find-monitored-entity-by-name';
10+
import { findMonitoredEntitiesByName } from '../src/capabilities/find-monitored-entity-by-name';
1111
import { getDynatraceEnv, DynatraceEnv } from '../src/getDynatraceEnv';
12+
import { getEntityTypeFromId } from '../src/utils/dynatrace-entity-types';
1213

1314
// Load environment variables
1415
config();
@@ -60,27 +61,25 @@ describe('Find Monitored Entity by Name Integration Tests', () => {
6061

6162
// Search for an entity name that is very unlikely to exist
6263
const searchTerm = 'this-entity-definitely-does-not-exist-12345';
64+
const extendedSearch = false;
6365

64-
const response = await findMonitoredEntityByName(dtClient, searchTerm);
66+
const response = await findMonitoredEntitiesByName(dtClient, [searchTerm], extendedSearch);
6567

6668
expect(response).toBeDefined();
67-
expect(typeof response).toBe('string');
68-
expect(response).toBe('No monitored entity found with the specified name.');
69+
expect(response?.records).toBeDefined();
70+
expect(response?.records?.length).toEqual(0);
6971
}, 30_000); // Increased timeout for API calls
7072

71-
test('should handle search with empty string', async () => {
73+
test('should handle search with empty list', async () => {
7274
const dtClient = await createHttpClient();
7375

7476
// Test with empty string
75-
const searchTerm = '';
77+
const searchTerms = [] as string[];
78+
const extendedSearch = false;
7679

77-
const response = await findMonitoredEntityByName(dtClient, searchTerm);
78-
79-
expect(response).toBeDefined();
80-
expect(typeof response).toBe('string');
81-
82-
// Should handle gracefully - likely will return many results or handle empty search
83-
expect(response).toContain('You need to provide an entity name to search for');
80+
await expect(findMonitoredEntitiesByName(dtClient, searchTerms, extendedSearch)).rejects.toThrow(
81+
/No entity names supplied to search for/,
82+
);
8483
});
8584

8685
test('should return properly formatted response when entities are found', async () => {
@@ -89,20 +88,19 @@ describe('Find Monitored Entity by Name Integration Tests', () => {
8988
// Search for a pattern that is likely to find at least one entity
9089
// "host" is common in most Dynatrace environments
9190
const searchTerm = 'host';
91+
const extendedSearch = false;
9292

93-
const response = await findMonitoredEntityByName(dtClient, searchTerm);
93+
const response = await findMonitoredEntitiesByName(dtClient, [searchTerm], extendedSearch);
9494

95+
// Assert, based on the DqlExecutionResult
9596
expect(response).toBeDefined();
96-
expect(typeof response).toBe('string');
97-
98-
// If entities are found, check the format
99-
if (response.includes('The following monitored entities were found:')) {
100-
// Each line should follow the expected format
101-
const lines = response.split('\n').filter((line) => line.startsWith('- Entity'));
102-
103-
lines.forEach((line) => {
104-
expect(line).toMatch(/^- Entity '.*' of type '.* has entity id '.*'$/);
97+
if (response?.records && response.records.length > 0) {
98+
response.records.forEach((entity) => {
99+
expect(entity?.id).toBeDefined();
100+
expect(getEntityTypeFromId(String(entity?.id))).toBeDefined();
105101
});
102+
} else {
103+
// Nothing to assert; environment for testing has no entities found.
106104
}
107105
});
108106
});

src/capabilities/find-monitored-entity-by-name.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
* @returns DQL Statement for searching all entity types
1414
*/
1515
export const generateDqlSearchEntityCommand = (entityNames: string[], extendedSearch: boolean): string => {
16+
if (entityNames == undefined || entityNames.length == 0) {
17+
throw new Error(`No entity names supplied to search for`);
18+
}
19+
1620
// If extendedSearch is true, use all entity types, otherwise use only basic ones
1721
const fetchDqlCommands = (extendedSearch ? DYNATRACE_ENTITY_TYPES_ALL : DYNATRACE_ENTITY_TYPES_BASICS).map(
1822
(entityType, index) => {

0 commit comments

Comments
 (0)