@@ -26,7 +26,6 @@ import {
2626 githubNumberProvider ,
2727 jiraBooleanMetricMetadata ,
2828 jiraBooleanProvider ,
29- MockNumberProvider ,
3029} from '../../__fixtures__/mockProviders' ;
3130import { mockEntity } from '../../__fixtures__/mockEntities' ;
3231import {
@@ -36,23 +35,31 @@ import {
3635} from '@backstage/plugin-permission-common' ;
3736import { mockMetricValuesStore } from '../../__fixtures__/mockDatabase' ;
3837
39- class AnnotationProvider extends MockNumberProvider {
40- constructor ( ) {
41- super (
42- 'annotation.metric' ,
43- 'annotation' ,
44- 'Annotation Metric' ,
45- "A metric that only works with entities having a 'custom.annotation' annotation" ,
46- 10 ,
47- ) ;
48- }
49- }
38+ const timestamp = '2024-01-15T10:30:00.000Z' ;
39+ const timestampDate = new Date ( timestamp ) ;
40+ const storedMetricValues = [
41+ {
42+ id : 1 ,
43+ catalog_entity_ref : 'component:default/test-component' ,
44+ metric_id : 'github.number_metric' ,
45+ value : 42 ,
46+ timestamp : timestampDate ,
47+ error_message : undefined ,
48+ } ,
49+ {
50+ id : 2 ,
51+ catalog_entity_ref : 'component:default/test-component' ,
52+ metric_id : 'jira.boolean_metric' ,
53+ value : false ,
54+ timestamp : timestampDate ,
55+ error_message : undefined ,
56+ } ,
57+ ] ;
5058
5159describe ( 'CatalogMetricService' , ( ) => {
5260 let catalogMetricService : CatalogMetricService ;
5361 let registry : MetricProvidersRegistry ;
5462 const mockCatalogService = catalogServiceMock . mock ( ) ;
55- const timestamp = '2024-01-15T10:30:00.000Z' ;
5663
5764 const githubMetricResult = {
5865 id : 'github.number_metric' ,
@@ -107,6 +114,10 @@ describe('CatalogMetricService', () => {
107114 registry . register ( githubNumberProvider ) ;
108115 registry . register ( jiraBooleanProvider ) ;
109116
117+ mockMetricValuesStore . readLatestEntityMetricValues . mockResolvedValue (
118+ storedMetricValues ,
119+ ) ;
120+
110121 catalogMetricService = new CatalogMetricService ( {
111122 catalog : mockCatalogService ,
112123 auth : mockServices . auth ( ) ,
@@ -116,19 +127,32 @@ describe('CatalogMetricService', () => {
116127 } ) ;
117128 } ) ;
118129
119- describe ( 'calculateEntityMetrics ' , ( ) => {
120- it ( 'should calculate metrics successfully with default thresholds' , async ( ) => {
130+ describe ( 'getLatestEntityMetrics ' , ( ) => {
131+ it ( 'should get metrics with default thresholds' , async ( ) => {
121132 mockCatalogService . getEntityByRef . mockResolvedValue ( mockEntity ) ;
122133
123134 const result = await catalogMetricService . getLatestEntityMetrics (
124135 'component:default/test-component' ,
125136 ) ;
126137
138+ expect ( mockCatalogService . getEntityByRef ) . toHaveBeenCalledWith (
139+ 'component:default/test-component' ,
140+ expect . objectContaining ( {
141+ credentials : expect . any ( Object ) ,
142+ } ) ,
143+ ) ;
144+ expect (
145+ mockMetricValuesStore . readLatestEntityMetricValues ,
146+ ) . toHaveBeenCalledWith ( 'component:default/test-component' , [
147+ 'github.number_metric' ,
148+ 'jira.boolean_metric' ,
149+ ] ) ;
150+
127151 expect ( result ) . toHaveLength ( 2 ) ;
128152 expect ( result ) . toEqual ( [ githubMetricResult , jiraMetricResult ] ) ;
129153 } ) ;
130154
131- it ( 'should calculate metrics successfully with entity thresholds' , async ( ) => {
155+ it ( 'should get metrics with entity thresholds' , async ( ) => {
132156 const annotatedEntity = {
133157 ...mockEntity ,
134158 metadata : {
@@ -168,22 +192,40 @@ describe('CatalogMetricService', () => {
168192 expect ( result ) . toEqual ( [ expectedGithubResult , jiraMetricResult ] ) ;
169193 } ) ;
170194
171- it ( 'should calculate metrics for specific provider IDs' , async ( ) => {
195+ it ( 'should retrieve metrics for specific provider IDs' , async ( ) => {
172196 mockCatalogService . getEntityByRef . mockResolvedValue ( mockEntity ) ;
197+ mockMetricValuesStore . readLatestEntityMetricValues . mockResolvedValue ( [
198+ storedMetricValues [ 1 ] ,
199+ ] ) ;
173200
174201 const result = await catalogMetricService . getLatestEntityMetrics (
175202 'component:default/test-component' ,
176203 [ 'jira.boolean_metric' ] ,
177204 ) ;
178205
206+ expect (
207+ mockMetricValuesStore . readLatestEntityMetricValues ,
208+ ) . toHaveBeenCalledWith ( 'component:default/test-component' , [
209+ 'jira.boolean_metric' ,
210+ ] ) ;
179211 expect ( result ) . toHaveLength ( 1 ) ;
180212 expect ( result ) . toEqual ( [ jiraMetricResult ] ) ;
181213 } ) ;
182214
183- it ( 'should handle metric calculation error' , async ( ) => {
184- jest
185- . spyOn ( jiraBooleanProvider , 'calculateMetric' )
186- . mockRejectedValue ( new Error ( 'Jira API failure' ) ) ;
215+ it ( "should return 'error' status when metric calculation error" , async ( ) => {
216+ mockCatalogService . getEntityByRef . mockResolvedValue ( mockEntity ) ;
217+ const metricsWithError = [
218+ storedMetricValues [ 0 ] , // github metric - success
219+ {
220+ ...storedMetricValues [ 1 ] , // jira metric - error
221+ value : undefined ,
222+ error_message : 'Error: Jira API failure' ,
223+ } ,
224+ ] ;
225+ mockMetricValuesStore . readLatestEntityMetricValues . mockResolvedValue (
226+ metricsWithError ,
227+ ) ;
228+
187229 const jiraMetricErrorResult = {
188230 ...jiraMetricResult ,
189231 status : 'error' as const ,
@@ -236,6 +278,10 @@ describe('CatalogMetricService', () => {
236278 entityWithInvalidThresholds ,
237279 ) ;
238280
281+ mockMetricValuesStore . readLatestEntityMetricValues . mockResolvedValue ( [
282+ storedMetricValues [ 0 ] ,
283+ ] ) ;
284+
239285 const result = await catalogMetricService . getLatestEntityMetrics (
240286 'component:default/test-component' ,
241287 [ 'github.number_metric' ] ,
@@ -283,56 +329,24 @@ describe('CatalogMetricService', () => {
283329 ] ,
284330 } ;
285331
332+ mockMetricValuesStore . readLatestEntityMetricValues . mockResolvedValue ( [
333+ storedMetricValues [ 0 ] ,
334+ ] ) ;
335+
286336 const result = await catalogMetricService . getLatestEntityMetrics (
287337 'component:default/test-component' ,
288338 undefined ,
289339 filter ,
290340 ) ;
291341
342+ expect (
343+ mockMetricValuesStore . readLatestEntityMetricValues ,
344+ ) . toHaveBeenCalledWith ( 'component:default/test-component' , [
345+ 'github.number_metric' ,
346+ ] ) ;
292347 expect ( result ) . toHaveLength ( 1 ) ;
293348 expect ( result [ 0 ] . id ) . toBe ( 'github.number_metric' ) ;
294349 expect ( result ) . toEqual ( [ githubMetricResult ] ) ;
295350 } ) ;
296-
297- it ( 'should filter out metrics that provider does not support for certain entity' , async ( ) => {
298- registry . register ( new AnnotationProvider ( ) ) ;
299- mockCatalogService . getEntityByRef . mockResolvedValue ( mockEntity ) ;
300-
301- const result = await catalogMetricService . getLatestEntityMetrics (
302- 'component:default/test-component' ,
303- ) ;
304-
305- expect ( result ) . toHaveLength ( 2 ) ;
306- expect ( result . map ( r => r . id ) ) . toEqual ( [
307- 'github.number_metric' ,
308- 'jira.boolean_metric' ,
309- ] ) ;
310- } ) ;
311-
312- it ( 'should include metrics that provider supports for certain entity' , async ( ) => {
313- registry . register ( new AnnotationProvider ( ) ) ;
314- const entityWithAnnotation : Entity = {
315- ...mockEntity ,
316- metadata : {
317- ...mockEntity . metadata ,
318- annotations : {
319- 'custom.annotation' : 'A123' ,
320- } ,
321- } ,
322- } ;
323-
324- mockCatalogService . getEntityByRef . mockResolvedValue ( entityWithAnnotation ) ;
325-
326- const result = await catalogMetricService . getLatestEntityMetrics (
327- 'component:default/test-component' ,
328- ) ;
329-
330- expect ( result ) . toHaveLength ( 3 ) ;
331- expect ( result . map ( r => r . id ) ) . toEqual ( [
332- 'github.number_metric' ,
333- 'jira.boolean_metric' ,
334- 'annotation.metric' ,
335- ] ) ;
336- } ) ;
337351 } ) ;
338352} ) ;
0 commit comments