@@ -6,90 +6,165 @@ import {
6
6
} from 'vitest'
7
7
import createConcept from '../handler'
8
8
import conceptIdExists from '../../utils/conceptIdExists'
9
+ import getConceptId from '../../utils/getConceptId'
9
10
import { getApplicationConfig } from '../../utils/getConfig'
10
11
import { sparqlRequest } from '../../utils/sparqlRequest'
11
12
12
13
// Mock the dependencies
13
14
vi . mock ( '../../utils/conceptIdExists' )
15
+ vi . mock ( '../../utils/getConceptId' )
14
16
vi . mock ( '../../utils/getConfig' )
15
17
vi . mock ( '../../utils/sparqlRequest' )
16
18
17
19
describe ( 'createConcept' , ( ) => {
18
- const mockEvent = {
19
- body : '<rdf:RDF>...</rdf:RDF>' ,
20
- pathParameters : { conceptId : '123' }
21
- }
22
-
20
+ const mockRdfXml = '<rdf:RDF>...</rdf:RDF>'
21
+ const mockEvent = { body : mockRdfXml }
23
22
const mockDefaultHeaders = { 'Content-Type' : 'application/json' }
24
- beforeAll ( ( ) => {
25
- vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
26
- vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
27
- } )
23
+ const mockConceptId = '123'
24
+ const mockConceptIRI = `https://gcmd.earthdata.nasa.gov/kms/concept/${ mockConceptId } `
28
25
29
26
beforeEach ( ( ) => {
30
27
vi . resetAllMocks ( )
31
28
getApplicationConfig . mockReturnValue ( { defaultResponseHeaders : mockDefaultHeaders } )
29
+ getConceptId . mockReturnValue ( mockConceptId )
30
+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
31
+ vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
32
32
} )
33
33
34
- test ( 'should return 404 if concept already exists ' , async ( ) => {
35
- conceptIdExists . mockResolvedValue ( true )
34
+ test ( 'should handle missing body in event ' , async ( ) => {
35
+ const eventWithoutBody = { }
36
36
37
- const result = await createConcept ( mockEvent )
37
+ const result = await createConcept ( eventWithoutBody )
38
38
39
39
expect ( result ) . toEqual ( {
40
- statusCode : 404 ,
41
- body : JSON . stringify ( { message : 'Concept https://gcmd.earthdata.nasa.gov/kms/concept/123 already exists.' } ) ,
40
+ statusCode : 400 ,
41
+ body : JSON . stringify ( {
42
+ message : 'Error creating concept' ,
43
+ error : 'Missing RDF/XML data in request body'
44
+ } ) ,
42
45
headers : mockDefaultHeaders
43
46
} )
44
47
} )
45
48
46
- test ( 'should create concept and return 200 if concept does not exist ' , async ( ) => {
49
+ test ( 'should successfully create a concept' , async ( ) => {
47
50
conceptIdExists . mockResolvedValue ( false )
48
51
sparqlRequest . mockResolvedValue ( { ok : true } )
49
52
50
53
const result = await createConcept ( mockEvent )
51
54
55
+ expect ( getConceptId ) . toHaveBeenCalledWith ( mockRdfXml )
56
+ expect ( conceptIdExists ) . toHaveBeenCalledWith ( mockConceptIRI )
52
57
expect ( sparqlRequest ) . toHaveBeenCalledWith ( {
53
58
contentType : 'application/rdf+xml' ,
54
59
accept : 'application/rdf+xml' ,
55
60
path : '/statements' ,
56
61
method : 'POST' ,
57
- body : mockEvent . body
62
+ body : mockRdfXml
63
+ } )
64
+
65
+ expect ( result ) . toEqual ( {
66
+ statusCode : 201 ,
67
+ body : JSON . stringify ( {
68
+ message : 'Successfully created concept' ,
69
+ conceptId : mockConceptId
70
+ } ) ,
71
+ headers : mockDefaultHeaders
58
72
} )
73
+ } )
74
+
75
+ test ( 'should return 409 if concept already exists' , async ( ) => {
76
+ conceptIdExists . mockResolvedValue ( true )
77
+
78
+ const result = await createConcept ( mockEvent )
59
79
60
80
expect ( result ) . toEqual ( {
61
- statusCode : 200 ,
62
- body : 'Successfully loaded RDF XML into RDF4J' ,
81
+ statusCode : 409 ,
82
+ body : JSON . stringify ( { message : `Concept ${ mockConceptIRI } already exists.` } ) ,
63
83
headers : mockDefaultHeaders
64
84
} )
65
85
} )
66
86
67
- test ( 'should return 500 if sparqlRequest fails' , async ( ) => {
87
+ test ( 'should handle getConceptId throwing an error' , async ( ) => {
88
+ getConceptId . mockImplementation ( ( ) => {
89
+ throw new Error ( 'Invalid XML' )
90
+ } )
91
+
92
+ const result = await createConcept ( mockEvent )
93
+
94
+ expect ( result ) . toEqual ( {
95
+ statusCode : 400 ,
96
+ body : JSON . stringify ( {
97
+ message : 'Error creating concept' ,
98
+ error : 'Invalid XML'
99
+ } ) ,
100
+ headers : mockDefaultHeaders
101
+ } )
102
+ } )
103
+
104
+ test ( 'should handle missing concept ID' , async ( ) => {
105
+ getConceptId . mockReturnValue ( null )
106
+
107
+ const result = await createConcept ( mockEvent )
108
+
109
+ expect ( result ) . toEqual ( {
110
+ statusCode : 400 ,
111
+ body : JSON . stringify ( {
112
+ message : 'Error creating concept' ,
113
+ error : 'Invalid or missing concept ID'
114
+ } ) ,
115
+ headers : mockDefaultHeaders
116
+ } )
117
+ } )
118
+
119
+ test ( 'should handle sparqlRequest failure' , async ( ) => {
68
120
conceptIdExists . mockResolvedValue ( false )
69
121
sparqlRequest . mockResolvedValue ( {
70
122
ok : false ,
71
123
status : 500 ,
72
- text : ( ) => Promise . resolve ( 'Server error' )
124
+ text : async ( ) => 'Internal Server Error'
125
+ } )
126
+
127
+ const result = await createConcept ( mockEvent )
128
+
129
+ expect ( result ) . toEqual ( {
130
+ statusCode : 400 ,
131
+ body : JSON . stringify ( {
132
+ message : 'Error creating concept' ,
133
+ error : 'HTTP error! status: 500'
134
+ } ) ,
135
+ headers : mockDefaultHeaders
73
136
} )
74
137
138
+ expect ( console . log ) . toHaveBeenCalledWith ( 'Response text:' , 'Internal Server Error' )
139
+ } )
140
+
141
+ test ( 'should handle conceptIdExists throwing an error' , async ( ) => {
142
+ conceptIdExists . mockRejectedValue ( new Error ( 'Database error' ) )
143
+
75
144
const result = await createConcept ( mockEvent )
76
145
77
146
expect ( result ) . toEqual ( {
78
- statusCode : 500 ,
79
- body : 'Error loading RDF XML into RDF4J' ,
147
+ statusCode : 400 ,
148
+ body : JSON . stringify ( {
149
+ message : 'Error creating concept' ,
150
+ error : 'Database error'
151
+ } ) ,
80
152
headers : mockDefaultHeaders
81
153
} )
82
154
} )
83
155
84
- test ( 'should return 500 if an error is thrown ' , async ( ) => {
156
+ test ( 'should handle sparqlRequest throwing an error' , async ( ) => {
85
157
conceptIdExists . mockResolvedValue ( false )
86
158
sparqlRequest . mockRejectedValue ( new Error ( 'Network error' ) )
87
159
88
160
const result = await createConcept ( mockEvent )
89
161
90
162
expect ( result ) . toEqual ( {
91
- statusCode : 500 ,
92
- body : 'Error loading RDF XML into RDF4J' ,
163
+ statusCode : 400 ,
164
+ body : JSON . stringify ( {
165
+ message : 'Error creating concept' ,
166
+ error : 'Network error'
167
+ } ) ,
93
168
headers : mockDefaultHeaders
94
169
} )
95
170
} )
0 commit comments