|
| 1 | +/** |
| 2 | + * Integration test for find monitored entity by name functionality |
| 3 | + * |
| 4 | + * This test verifies the entity finding functionality by making actual API calls |
| 5 | + * to the Dynatrace environment. These tests require valid authentication credentials. |
| 6 | + */ |
| 7 | + |
| 8 | +import { config } from 'dotenv'; |
| 9 | +import { createDtHttpClient } from '../src/authentication/dynatrace-clients'; |
| 10 | +import { findMonitoredEntityByName } from '../src/capabilities/find-monitored-entity-by-name'; |
| 11 | +import { getDynatraceEnv, DynatraceEnv } from '../src/getDynatraceEnv'; |
| 12 | + |
| 13 | +// Load environment variables |
| 14 | +config(); |
| 15 | + |
| 16 | +const API_RATE_LIMIT_DELAY = 100; // Delay in milliseconds to avoid hitting API rate limits |
| 17 | + |
| 18 | +const scopesBase = [ |
| 19 | + 'app-engine:apps:run', // needed for environmentInformationClient |
| 20 | + 'app-engine:functions:run', // needed for environmentInformationClient |
| 21 | +]; |
| 22 | + |
| 23 | +const scopesEntitySearch = [ |
| 24 | + 'storage:entities:read', // Read entities from Grail |
| 25 | +]; |
| 26 | + |
| 27 | +describe('Find Monitored Entity by Name Integration Tests', () => { |
| 28 | + let dynatraceEnv: DynatraceEnv; |
| 29 | + |
| 30 | + // Setup that runs once before all tests |
| 31 | + beforeAll(async () => { |
| 32 | + try { |
| 33 | + dynatraceEnv = getDynatraceEnv(); |
| 34 | + console.log(`Testing against environment: ${dynatraceEnv.dtEnvironment}`); |
| 35 | + } catch (err) { |
| 36 | + throw new Error(`Environment configuration error: ${(err as Error).message}`); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + // sleep after every call to avoid hitting API Rate limits |
| 42 | + await new Promise((resolve) => setTimeout(resolve, API_RATE_LIMIT_DELAY)); // Delay to avoid hitting API rate limits |
| 43 | + }); |
| 44 | + |
| 45 | + // Helper function to create HTTP client for entity search |
| 46 | + const createHttpClient = async () => { |
| 47 | + const { oauthClientId, oauthClientSecret, dtEnvironment, dtPlatformToken } = dynatraceEnv; |
| 48 | + |
| 49 | + return await createDtHttpClient( |
| 50 | + dtEnvironment, |
| 51 | + scopesBase.concat(scopesEntitySearch), |
| 52 | + oauthClientId, |
| 53 | + oauthClientSecret, |
| 54 | + dtPlatformToken, |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + test('should handle search for non-existent entity gracefully', async () => { |
| 59 | + const dtClient = await createHttpClient(); |
| 60 | + |
| 61 | + // Search for an entity name that is very unlikely to exist |
| 62 | + const searchTerm = 'this-entity-definitely-does-not-exist-12345'; |
| 63 | + |
| 64 | + const response = await findMonitoredEntityByName(dtClient, searchTerm); |
| 65 | + |
| 66 | + expect(response).toBeDefined(); |
| 67 | + expect(typeof response).toBe('string'); |
| 68 | + expect(response).toBe('No monitored entity found with the specified name.'); |
| 69 | + }, 30_000); // Increased timeout for API calls |
| 70 | + |
| 71 | + test('should handle search with empty string', async () => { |
| 72 | + const dtClient = await createHttpClient(); |
| 73 | + |
| 74 | + // Test with empty string |
| 75 | + const searchTerm = ''; |
| 76 | + |
| 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'); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should return properly formatted response when entities are found', async () => { |
| 87 | + const dtClient = await createHttpClient(); |
| 88 | + |
| 89 | + // Search for a pattern that is likely to find at least one entity |
| 90 | + // "host" is common in most Dynatrace environments |
| 91 | + const searchTerm = 'host'; |
| 92 | + |
| 93 | + const response = await findMonitoredEntityByName(dtClient, searchTerm); |
| 94 | + |
| 95 | + 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 '.*'$/); |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments