--[[----------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

	-- WF.core.rxlua
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.oz_client.sync_agent
local OzSyncMetadataMapper = require 'OzSyncMetadataMapper'
local WFOzSyncConstants = require 'WFOzSyncConstants'

--==============================================================================

local OzSyncMetadataMapperTest = {}

--------------------------------------------------------------------------------

function OzSyncMetadataMapperTest.rxTestMapping( rxTest )

	local globalId = WFOzSyncConstants.kGlobalDocumentId
	local userId = '5901c4d4707e48d6948ed5d1a1a0b9c6'
	
	local remap = OzSyncMetadataMapper.remapGlobalDocumentId
	
	local docValue = {
		name = 'foo',
	}
	
	remap( userId, docValue )
	rxTest:expectEqual( docValue, { name = 'foo' } )
	
	docValue = {
		name = 'foo',
		reviews = {
			[ globalId ] = {
				flag = 'pick',
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, { 
		name = 'foo',
		reviews = {
			[ userId ] = {
				flag = 'pick',
			}
		}
	} )
	
	--------------------------------------------------------------------------------

	docValue = {
		name = 'foo',
		ratings = {
			[ userId ] = {
				rating = 5,
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		name = 'foo',
		ratings = {
			[ userId ] = {
				rating = 5,
			}
		}
	} )
	
	docValue = {
		name = 'foo',
		ratings = {
			[ globalId ] = {
				rating = 5,
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		name = 'foo',
		ratings = {
			[ userId ] = {
				rating = 5,
			}
		}
	} )
	
	--------------------------------------------------------------------------------

	docValue = {
		name = 'foo',
		ratings = {
			[ globalId ] = {
				rating = 5,
			}
		},
		reviews = {
			[ globalId ] = {
				flag = 'pick',
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		name = 'foo',
		ratings = {
			[ userId ] = {
				rating = 5,
			}
		},
		reviews = {
			[ userId ] = {
				flag = 'pick',
			}
		}
	} )
	
	--------------------------------------------------------------------------------

	docValue = {
		importSource = {
			importedBy = globalId
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		importSource = {
			importedBy = userId,
		},
	} )
	
	--------------------------------------------------------------------------------

	docValue = {
		importSource = {
			importedBy = globalId,
			importedOnDevice = 'iPhone',
		},
		name = 'foo',
		ratings = {
			[ globalId ] = {
				rating = 5,
			}
		},
		reviews = {
			[ globalId ] = {
				flag = 'pick',
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		importSource = {
			importedBy = userId,
			importedOnDevice = 'iPhone',
		},
		name = 'foo',
		ratings = {
			[ userId ] = {
				rating = 5,
			}
		},
		reviews = {
			[ userId ] = {
				flag = 'pick',
			}
		}
	} )
	
	--------------------------------------------------------------------------------

	docValue = {
		importSource = {
			importedBy = globalId,
			importedOnDevice = 'iPhone',
		},
		name = 'foo',
		reviews = {
			[ globalId ] = {
				flag = 'pick',
			},
			[ userId ] = {
				flag = 'rejected',
			}
		}
	}
	
	remap( userId, docValue )
	
	rxTest:expectEqual( docValue, {
		importSource = {
			importedBy = userId,
			importedOnDevice = 'iPhone',
		},
		name = 'foo',
		reviews = {
			[ userId ] = {
				flag = 'rejected',
			}
		}
	} )
	
end

--------------------------------------------------------------------------------

function OzSyncMetadataMapperTest.rxTestRemoveEmptyTables( rxTest )

	local remove = OzSyncMetadataMapper.removeEmptyTables
	
	local function assert_value( docValue, expected )
	
		remove( docValue )
		rxTest:expectEqual( docValue, expected )
	
	end
	
	local docValue = {
		name = 'foo',
	}
	
	assert_value( docValue, { name = 'foo' } )
	
	-- reviews 
	
	docValue = {
		reviews = {
			e55f1a82cb0d499c9c175fdda126e7e6 = {
				flag = 'pick',
			}
		}
	}
	
	assert_value( docValue, { 
		reviews = { 
			e55f1a82cb0d499c9c175fdda126e7e6 = {
				flag = 'pick'
			} 
		}
	} )
	
	docValue = {
		reviews = {}
	}
	
	assert_value( docValue, {} )
	
	-- ratings
	
	docValue = {
		ratings = {
			e55f1a82cb0d499c9c175fdda126e7e6 = {
				rating = 5,
			}
		}
	}
	
	assert_value( docValue, { 
		ratings = { 
			e55f1a82cb0d499c9c175fdda126e7e6 = {
				rating = 5
			} 
		}
	} )
	
	docValue = {
		ratings = {}
	}
	
	assert_value( docValue, {} )
	
end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( 'OzSyncMetadataMapperTest', OzSyncMetadataMapperTest )
