@@ -197,7 +197,16 @@ describe('SavedObjectsRepository', () => {
197197 { type, id, references, namespace : objectNamespace , originId } ,
198198 namespace
199199 ) => {
200- const namespaceId = objectNamespace === 'default' ? undefined : objectNamespace ?? namespace ;
200+ let namespaces ;
201+ if ( objectNamespace ) {
202+ namespaces = [ objectNamespace ] ;
203+ } else if ( namespace ) {
204+ namespaces = Array . isArray ( namespace ) ? namespace : [ namespace ] ;
205+ } else {
206+ namespaces = [ 'default' ] ;
207+ }
208+ const namespaceId = namespaces [ 0 ] === 'default' ? undefined : namespaces [ 0 ] ;
209+
201210 return {
202211 // NOTE: Elasticsearch returns more fields (_index, _type) but the SavedObjectsRepository method ignores these
203212 found : true ,
@@ -207,7 +216,7 @@ describe('SavedObjectsRepository', () => {
207216 ...mockVersionProps ,
208217 _source : {
209218 ...( registry . isSingleNamespace ( type ) && { namespace : namespaceId } ) ,
210- ...( registry . isMultiNamespace ( type ) && { namespaces : [ namespaceId ?? 'default' ] } ) ,
219+ ...( registry . isMultiNamespace ( type ) && { namespaces } ) ,
211220 ...( originId && { originId } ) ,
212221 type,
213222 [ type ] : { title : 'Testing' } ,
@@ -219,7 +228,9 @@ describe('SavedObjectsRepository', () => {
219228 } ;
220229
221230 const getMockMgetResponse = ( objects , namespace ) => ( {
222- docs : objects . map ( ( obj ) => ( obj . found === false ? obj : getMockGetResponse ( obj , namespace ) ) ) ,
231+ docs : objects . map ( ( obj ) =>
232+ obj . found === false ? obj : getMockGetResponse ( obj , obj . initialNamespaces ?? namespace )
233+ ) ,
223234 } ) ;
224235
225236 expect . extend ( {
@@ -797,6 +808,54 @@ describe('SavedObjectsRepository', () => {
797808 } ) ;
798809 } ) ;
799810
811+ it ( `returns error when there is an unresolvable conflict with an existing multi-namespace saved object when using initialNamespaces (get)` , async ( ) => {
812+ const obj = {
813+ ...obj3 ,
814+ type : MULTI_NAMESPACE_TYPE ,
815+ initialNamespaces : [ 'foo-namespace' , 'default' ] ,
816+ } ;
817+ const response1 = {
818+ status : 200 ,
819+ docs : [
820+ {
821+ found : true ,
822+ _source : {
823+ type : obj . type ,
824+ namespaces : [ 'bar-namespace' ] ,
825+ } ,
826+ } ,
827+ ] ,
828+ } ;
829+ client . mget . mockResolvedValueOnce (
830+ elasticsearchClientMock . createSuccessTransportRequestPromise ( response1 )
831+ ) ;
832+ const response2 = getMockBulkCreateResponse ( [ obj1 , obj2 ] ) ;
833+ client . bulk . mockResolvedValueOnce (
834+ elasticsearchClientMock . createSuccessTransportRequestPromise ( response2 )
835+ ) ;
836+
837+ const options = { overwrite : true } ;
838+ const result = await savedObjectsRepository . bulkCreate ( [ obj1 , obj , obj2 ] , options ) ;
839+
840+ expect ( client . bulk ) . toHaveBeenCalled ( ) ;
841+ expect ( client . mget ) . toHaveBeenCalled ( ) ;
842+
843+ const body1 = { docs : [ expect . objectContaining ( { _id : `${ obj . type } :${ obj . id } ` } ) ] } ;
844+ expect ( client . mget ) . toHaveBeenCalledWith (
845+ expect . objectContaining ( { body : body1 } ) ,
846+ expect . anything ( )
847+ ) ;
848+ const body2 = [ ...expectObjArgs ( obj1 ) , ...expectObjArgs ( obj2 ) ] ;
849+ expect ( client . bulk ) . toHaveBeenCalledWith (
850+ expect . objectContaining ( { body : body2 } ) ,
851+ expect . anything ( )
852+ ) ;
853+ const expectedError = expectErrorConflict ( obj , { metadata : { isNotOverwritable : true } } ) ;
854+ expect ( result ) . toEqual ( {
855+ saved_objects : [ expectSuccess ( obj1 ) , expectedError , expectSuccess ( obj2 ) ] ,
856+ } ) ;
857+ } ) ;
858+
800859 it ( `returns bulk error` , async ( ) => {
801860 const expectedErrorResult = { type : obj3 . type , id : obj3 . id , error : 'Oh no, a bulk error!' } ;
802861 await bulkCreateError ( obj3 , true , expectedErrorResult ) ;
@@ -2197,6 +2256,22 @@ describe('SavedObjectsRepository', () => {
21972256 expect ( client . get ) . toHaveBeenCalled ( ) ;
21982257 } ) ;
21992258
2259+ it ( `throws when there is an unresolvable conflict with an existing multi-namespace saved object when using initialNamespaces (get)` , async ( ) => {
2260+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_ISOLATED_TYPE , id } , namespace ) ;
2261+ client . get . mockResolvedValueOnce (
2262+ elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
2263+ ) ;
2264+ await expect (
2265+ savedObjectsRepository . create ( MULTI_NAMESPACE_TYPE , attributes , {
2266+ id,
2267+ overwrite : true ,
2268+ initialNamespaces : [ 'bar-ns' , 'dolly-ns' ] ,
2269+ namespace,
2270+ } )
2271+ ) . rejects . toThrowError ( createConflictError ( MULTI_NAMESPACE_TYPE , id ) ) ;
2272+ expect ( client . get ) . toHaveBeenCalled ( ) ;
2273+ } ) ;
2274+
22002275 it . todo ( `throws when automatic index creation fails` ) ;
22012276
22022277 it . todo ( `throws when an unexpected failure occurs` ) ;
0 commit comments