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

WFXmpTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

local KSData = require 'KSData'

local WFPlatformInfo = require "WFPlatformInfo"

	-- WF.oz_client.managed_catalog.metadata
local WFXMLNS = require 'WFXMLNS'

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

local function assertNil( message, ... )
	assert( select( '#', ... ) >= 1, message )
	assert( (...) == nil, message .. " (was " .. type( ( ... ) ) .. ")" )
end

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

local WFXmpTest = {}

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

function WFXmpTest.setUp()
	
	local WFXmp = assert( require 'WFXmp', 'namespace exists' )
	WFXmp.registerNamespace( WFXMLNS.mwgRegions, "mwg-rs" )

end

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

if not WFPlatformInfo.hasACR then

	return {}

end

function WFXmpTest.testParse()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = WFXmp.WFXmp()

	assert( xmp, 'can create xmp' )
	assert( xmp.parse, 'parse() is fn' )

	local result, errCode, msg = xmp:parse()
	assert( result == false, 'invalid xmp has error result' )
	assert( errCode == 'BadParam', 'invalid xmp has error code' )
	assert( type( msg ) == 'string', 'invalid xmp has error message is a string' )

	local xmpStr = [[<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.3-c007 1.136881, 2010/06/10-18:11:35        ">
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about=""
    xmlns:xmp="http://ns.adobe.com/xap/1.0/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmp:Rating="5">
   <dc:creator>
    <rdf:Seq>
     <rdf:li>Rahn</rdf:li>
    </rdf:Seq>
   </dc:creator>
  </rdf:Description>
 </rdf:RDF>
</x:xmpmeta>
]]

	assert( xmp:parse( xmpStr ), 'Can parse' )
	assert( xmp:get( WFXMLNS.xap, 'Rating' ) == '5', 'parse got xmp:Rating of 5' )

	local parse = xmp.parse
	xmp:close()

	result, errCode, msg = xmp:parse( xmpStr )

	assert( result == false, 'Trying to call method on invalidated XMP throws' )
	assert( errCode == 'Closed', 'invalid xmp has error code' )
	assert( type( msg ), 'error message is a string' )

	result, msg = parse( xmp, xmpStr )

	assert( result == false, 'Trying to call method on invalidated XMP throws' )
	assert( type( msg ), 'error message is a string' )

end

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

function WFXmpTest.testSetAndGet()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )

	assertNil( 'no set then read got nil xmp:Rating', xmp:get( WFXMLNS.xap, 'Rating' ) )
	xmp:set( WFXMLNS.xap, 'Rating', '4' )
	assert( xmp:get( WFXMLNS.xap, 'Rating' ) == '4', 'set then read got xmp:Rating of 4' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'integer' ) == 4, 'set then read got xmp:Rating of 4 as int' )
	local result, errCode, msg = xmp:get( WFXMLNS.xap, 'Rating', 'boolean' )
	assert( result == false, 'set then read got xmp:Rating of 0 as boolean is no good' )
	assert( errCode == 'BadParam', 'reading the string 4 as a boolean is not handled by XMPCore' )
	assert( type( msg ) == 'string', 'XMP failures have msg that is a string' )
	result, msg, msg = xmp:get( WFXMLNS.xap, 'Rating', 'boolean' )
	assert( result == false, 'set then read got xmp:Rating of 0 as boolean is no good' )
	assert( errCode == 'BadParam', 'reading the string 4 as a rational is not handled by WFXmp' )
	assert( type( msg ) == 'string', 'XMP failures have msg that is a string' )

	xmp:set( WFXMLNS.xap, 'Rating', 0 )
	assert( xmp:get( WFXMLNS.xap, 'Rating' ) == '0', 'set as integer then read got xmp:Rating of 0' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'integer' ) == 0, 'set then read got xmp:Rating of 0 as int' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'boolean' ) == false, 'set then read got xmp:Rating of 0 as bool' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'rational' ) == false, 'set then read got xmp:Rating of 0 as rational' )

	xmp:set( WFXMLNS.xap, 'Rating', 1 )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'boolean' ) == true, 'set then read got xmp:Rating of 1 as bool' )

	assert( xmp:set( WFXMLNS.xap, 'Rating', { 25, 15 } ), "Can set to rational value" )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'boolean' ) == false, 'rationals cannot be read as booleans' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'string' ) == '25/15', 'rationals can be read as strings' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'rational' )[ 1 ] == 25, 'rationals can be read as rationals' )
	assert( xmp:get( WFXMLNS.xap, 'Rating', 'rational' )[ 2 ] == 15, 'rationals can be read as rationals' )

	xmp:set( WFXMLNS.xapRights, 'Marked', true )
	assert( xmp:get( WFXMLNS.xapRights, 'Marked' ) == 'True', 'set then read as string xmpRights:Marked of true' )
	assert( xmp:get( WFXMLNS.xapRights, 'Marked', 'boolean' ) == true, 'set then read as boolean xmpRights:Marked of true' )

	result, errCode, msg = xmp:get( WFXMLNS.xapRights, 'Marked', 'integer' )

	assert( result == false, 'set then read as integer xmpRights:Marked is no good' )
	assert( errCode == 'BadParam', 'set then read as integer xmpRights:Marked errCode' )
	assert( type( msg ) == 'string', 'set then read as integer xmpRights:Marked details' )

	result, errCode, msg = xmp:set( WFXMLNS.xap, 'Rating', {} )

	assert( result == false, 'Setting XMP to invalid type (table) is an error' )
	assert( type( msg ), 'error message is a string' )

	xmp:set( WFXMLNS.xap, 'Rating' )
	assertNil( 'set then set to none then read got nil xmp:Rating', xmp:get( WFXMLNS.xap, 'Rating' ) )

	result, errCode, msg = xmp:set( WFXMLNS.xap, 'Rating', coroutine.create( function() end ) )

	assert( result == false, 'invalid struct value is an error' )
	assert( errCode == 'BadValue', 'invalid struct value has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:set( {}, 'Rating', 5 )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:set( WFXMLNS.xap, false, true )

	assert( result == false, 'non string fieldName is an error' )
	assert( errCode == 'BadParam', 'non string fieldName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:get( {}, 'Rating' )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:get( WFXMLNS.xap, false )

	assert( result == false, 'non string fieldName is an error' )
	assert( errCode == 'BadParam', 'non string fieldName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:get( WFXMLNS.xap, 'Rating', 'unsupported' )

	assert( result == false, 'unsupported XMP field type is an error' )
	assert( errCode == 'BadParam', 'unsupported XMP field type has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:get( WFXMLNS.xap, 'unsetfield', 'rational' )

	assert( result == false, 'integer cannot be read as rational XMP field type is an error' )
	assert( errCode == 'BadValue', 'integer cannot be read as rational XMP field type has error code' )
	assert( type( msg ), 'error message is a string' )

end

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

function WFXmpTest.testStruct()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )

	assertNil( 'no set then read got no results for Flash.Fired', xmp:getStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired' ) )
	xmp:setStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired', 'True' )
	assert( xmp:getStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired' ) == 'True', 'exif:Flash.Fired set/read error' )

	local result, errCode, msg = xmp:setStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired', { invalid = true } )

	assert( result == false, 'invalid struct value is an error' )
	assert( errCode == 'BadValue', 'invalid struct value has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setStruct( {}, 'Flash', WFXMLNS.exif, 'Fired', false )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setStruct( WFXMLNS.exif, false, WFXMLNS.exif, 'Flash', true )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, false, true )

	assert( result == false, 'invalid struct fieldName is an error' )
	assert( errCode == 'BadParam', 'invalid struct fieldName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired', { invalid = true } )

	assert( result == false, 'invalid XmpType is an error' )
	assert( errCode == 'BadParam', 'invalid XmpType has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStruct( {}, 'Flash', 'Fired' )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStruct( WFXMLNS.xap, false, 'Flash' )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStruct( WFXMLNS.xap, 'Flash', false )

	assert( result == false, 'invalid struct fieldName is an error' )
	assert( errCode == 'BadParam', 'invalid struct fieldName has error code' )
	assert( type( msg ), 'error message is a string' )

	xmp:setStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired', nil )
	assertNil( 'set then set to nil then read got nil Flash.Fired', xmp:getStruct( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired' ) )


	-- Test fields with a different namespace
	assertNil( 'value found for xmpMM:History.stEvt:action', xmp:getStruct( WFXMLNS.xmpMM, 'History', WFXMLNS.stEvt, 'action' ) )
	
	xmp:setStruct( WFXMLNS.xmpMM, "History", WFXMLNS.stEvt, "action", "edited" )
	assert( xmp:getStruct( WFXMLNS.xmpMM, 'History', WFXMLNS.stEvt, 'action' ) == 'edited', 'xmpMM:History.stEvt:action set/read error' )
	
	
	-- Test fields which are subattributes
	assert( 'value found for Regions.RegionList', xmp:hasProperty( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' ) == false )
	
	local regionListPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' )
	local pathToAddedRegion = xmp:appendStructItemToArray( WFXMLNS.mwgRegions, regionListPath, true )
	
	xmp:setStruct( WFXMLNS.mwgRegions, pathToAddedRegion, WFXMLNS.mwgRegions, "Name", "John" )
	assert( xmp:getStruct( WFXMLNS.mwgRegions, pathToAddedRegion, WFXMLNS.mwgRegions, "Name" ) == 'John', 'mwgRegions:Regions.mwgRegions:RegionList[1].mwgRegions:Name set/read error' )
end

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

function WFXmpTest.testAltLangDefault()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )

	xmp:parse [[<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.3-c007 1.136881, 2010/06/10-18:11:35        ">
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about=""
    xmlns:xmp="http://ns.adobe.com/xap/1.0/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
  <dc:rights>
    <rdf:Alt>
     <rdf:li xml:lang="x-default">Copyright Person Of Interest</rdf:li>
    </rdf:Alt>
   </dc:rights>
  </rdf:Description>
 </rdf:RDF>
</x:xmpmeta>
]]

	local copyright, language = xmp:getLocalizedText( WFXMLNS.dc, 'rights' )

	assert( copyright == 'Copyright Person Of Interest', 'read default dc:rights' )
	assert( language == 'x-default', 'default language is x-default' )

	xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, nil, 'Copyright Changed' )

	copyright, language = xmp:getLocalizedText( WFXMLNS.dc, 'rights' )

	assert( copyright == 'Copyright Changed', 'read modified dc:rights for default lang' )
	assert( language == 'x-default', 'default language is x-default' )

	xmp:setLocalizedText( WFXMLNS.dc, 'rights' )

	assertNil( 'read deleted localized text is nil', xmp:getLocalizedText( WFXMLNS.dc, 'rights' ) )

	local result, errCode, msg = xmp:getLocalizedText( false, 'rights' )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getLocalizedText( WFXMLNS.dc, false )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getLocalizedText( WFXMLNS.dc, 'rights', false )

	assert( result == false, 'non string optGenericLang is an error' )
	assert( errCode == 'BadParam', 'non string optGenericLang has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getLocalizedText( WFXMLNS.dc, 'rights', nil, false )

	assert( result == false, 'non string optSpecificLang is an error' )
	assert( errCode == 'BadParam', 'non string optSpecificLang has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setLocalizedText( false, 'rights' )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setLocalizedText( WFXMLNS.dc, false )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setLocalizedText( WFXMLNS.dc, 'rights', false )

	assert( result == false, 'non string optGenericLang is an error' )
	assert( errCode == 'BadParam', 'non string optGenericLang has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, false )

	assert( result == false, 'non string optSpecificLang is an error' )
	assert( errCode == 'BadParam', 'non string optSpecificLang has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, nil, { invalid = true } )

	assert( result == false, 'invalid value is an error' )
	assert( errCode == 'BadValue', 'non string value has error code' )
	assert( type( msg ), 'error message is a string' )

end

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

function WFXmpTest.testChangeTypes()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	xmp:set( WFXMLNS.dc, 'rights', 'Copyright put in wrong' )
	assert( xmp:get( WFXMLNS.dc, 'rights' ) == 'Copyright put in wrong', 'can read wrong dc:rights with wrong type' )

	local success, msg = xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, nil, 'Copyright Fixed' )

	assert( success == true, 'Can set localized when already has literal: ' .. ( msg or "" ) )

	local copyright, language = xmp:getLocalizedText( WFXMLNS.dc, 'rights' )

	assert( copyright == 'Copyright Fixed', 'read modified dc:rights for default lang' )
	assert( language == 'x-default', 'default language is x-default' )

	success, msg = xmp:setStruct( WFXMLNS.dc, 'rights', WFXMLNS.dc, 'badfield', 'Copyright put in bad struct' )

	assert( success == true, 'Can set to struct when already an array: ' .. ( msg or "" ) )
	assert( xmp:getStruct( WFXMLNS.dc, 'rights', WFXMLNS.dc, 'badfield' ) == 'Copyright put in bad struct', 'can read wrong dc:rights with wrong type of struct' )

	success, msg = xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, nil, 'Copyright Fixed Again' )

	assert( success == true, 'Can set localized when already has literal: ' .. ( msg or "" ) )

end

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

function WFXmpTest.testStringList()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )

	assertNil( 'no string list is empty list', xmp:getStringList( WFXMLNS.dc, 'creator' ) )
	assert( xmp:setStringList( WFXMLNS.dc, 'creator', { "Melissa", "Ellie", "Lauren", "Quynn", "Katy" } ) )
	local list, err, details = xmp:getStringList( WFXMLNS.dc, 'creator' )
	assert( err == nil, 'no err' )
	assert( details == nil, 'no details' )
	assert( type( list ) == 'table', 'string list is a table' )
	assert( list[ 1 ] == 'Melissa', 'first item okay' )
	assert( list[ 5 ] == 'Katy', 'last item okay' )
	assert( #list == 5, 'num list ok' )

	local result, errCode, msg = xmp:setStringList( false, 'rights', { } )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setStringList( WFXMLNS.dc, {}, { } )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:setStringList( WFXMLNS.dc, 'rights', false )

	assert( result == false, 'non table string list is an error' )
	assert( errCode == 'BadValue', 'non table string list has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStringList( false, 'rights' )

	assert( result == false, 'non string namespace is an error' )
	assert( errCode == 'BadParam', 'non string namespace has error code' )
	assert( type( msg ), 'error message is a string' )

	result, errCode, msg = xmp:getStringList( WFXMLNS.dc, {} )

	assert( result == false, 'non string structName is an error' )
	assert( errCode == 'BadParam', 'non string structName has error code' )
	assert( type( msg ), 'error message is a string' )

	xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	assert( xmp:setStringList( WFXMLNS.dc, 'subject', { "red", "orange", "yellow", "green", "blue", "indigo", "violet" }, true ) )

	local serz = xmp:serialize()

	assert( KSData.isData( serz ), 'serialized XMP is KSData' )
	local str = tostring( serz )

	str = string.gsub( str, "xpacket[^?]+?>", " == replaced == ?>" )
	str = string.gsub( str, [[x:xmptk="[^"]+"]], [[x:xmptk=" == replaced == "]] )
		-- " fix Halide syntax hiliting for unbalanced quote
	str = string.gsub( str, "[ \n\t]+", " " )

	assert( str == [[<? == replaced == ?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk=" == replaced == "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> <dc:subject> <rdf:Bag> <rdf:li>red</rdf:li> <rdf:li>orange</rdf:li> <rdf:li>yellow</rdf:li> <rdf:li>green</rdf:li> <rdf:li>blue</rdf:li> <rdf:li>indigo</rdf:li> <rdf:li>violet</rdf:li> </rdf:Bag> </dc:subject> </rdf:Description> </rdf:RDF> </x:xmpmeta> <? == replaced == ?>]], 'got expected data' )

end

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

function WFXmpTest.testSerialize()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	assert( xmp:set( WFXMLNS.xap, 'Rating', '4' ) )
	assert( xmp:setStringList( WFXMLNS.dc, 'creator', { "Andy", "Eric", "Jeff", "Pallavi" } ) )
	assert( xmp:setLocalizedText( WFXMLNS.dc, 'rights', nil, nil, 'Copyright Data Here' ) )

	local serz = xmp:serialize()

	assert( KSData.isData( serz ), 'serialized XMP is KSData' )
	local str = tostring( serz )

	str = string.gsub( str, "xpacket[^?]+?>", " == replaced == ?>" )
	str = string.gsub( str, [[x:xmptk="[^"]+"]], [[x:xmptk=" == replaced == "]] )
		-- " fix Halide syntax hiliting for unbalanced quote
	str = string.gsub( str, "[ \n\t]+", " " )

	assert( str == [[<? == replaced == ?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk=" == replaced == "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmp:Rating="4"> <dc:creator> <rdf:Seq> <rdf:li>Andy</rdf:li> <rdf:li>Eric</rdf:li> <rdf:li>Jeff</rdf:li> <rdf:li>Pallavi</rdf:li> </rdf:Seq> </dc:creator> <dc:rights> <rdf:Alt> <rdf:li xml:lang="x-default">Copyright Data Here</rdf:li> </rdf:Alt> </dc:rights> </rdf:Description> </rdf:RDF> </x:xmpmeta> <? == replaced == ?>]], 'got expected data' )

end

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

function WFXmpTest.testHasProperty()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	
	assert( xmp:hasProperty( WFXMLNS.xap, 'Rating' ) == false, 'value found for xap.Rating' )
	assert( xmp:set( WFXMLNS.xap, 'Rating', '4' ) )
	assert( xmp:hasProperty( WFXMLNS.xap, 'Rating' ) == true, 'value not found for xap.Rating' )
	
	
	-- Test complex structures
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, 'Regions' ) == false, 'value found for Regions.RegionList' )
	
	local regionListPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' )
	local pathToAddedRegion = xmp:appendStructItemToArray( WFXMLNS.mwgRegions, regionListPath, true )
	
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, regionListPath ) == true, 'value not found for Regions.RegionList' )
	
	local regionListItemNamePath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, pathToAddedRegion, WFXMLNS.mwgRegions, 'Name' )
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, regionListItemNamePath ) == false, 'value found for Regions.RegionList[1].Name' )
	
	xmp:setStruct( WFXMLNS.mwgRegions, pathToAddedRegion, WFXMLNS.mwgRegions, "Name", "John" )
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, regionListItemNamePath ) == true, 'value not found for Regions.RegionList[1].Name' )
	
end

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

function WFXmpTest.testComposeStructFieldPath()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	
	local regionListPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' )
	assert( regionListPath == "Regions/mwg-rs:RegionList", 'Incorrect field path' )
	
	local pathToAddedRegion = xmp:appendStructItemToArray( WFXMLNS.mwgRegions, regionListPath, true )
	local itemPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, pathToAddedRegion, WFXMLNS.mwgRegions, 'Name' )
	assert( itemPath == "Regions/mwg-rs:RegionList[1]/mwg-rs:Name", 'Incorrect field path' )
	
	-- Test for attributes with different NS
	local pathToStruct = xmp:appendStructItemToArray( WFXMLNS.xmpMM, "History", false )
	itemPath = xmp:composeStructFieldPath( WFXMLNS.xmpMM, pathToStruct, WFXMLNS.stEvt, "action" )
	assert( itemPath == "History[1]/stEvt:action", 'Incorrect field path' )
	
end

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

function WFXmpTest.testAppendStructItemToArray()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, 'Regions' ) == false, 'value found for Regions.RegionList' )
	
	local regionListPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' )
	local pathToAddedRegion = xmp:appendStructItemToArray( WFXMLNS.mwgRegions, regionListPath, true )
	
	assert( pathToAddedRegion == "Regions/mwg-rs:RegionList[1]", 'value not found for Regions.RegionList' )
	
end

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

function WFXmpTest.testCountAndComposeAndDeleteArrayItem()

	local WFXmp = assert( require 'WFXmp', 'namespace exists' )

	local xmp = assert( WFXmp.WFXmp(), 'can create xmp' )
	
	assert( xmp:hasProperty( WFXMLNS.mwgRegions, 'Regions' ) == false, 'value found for Regions.RegionList' )
	
	local regionListPath = xmp:composeStructFieldPath( WFXMLNS.mwgRegions, 'Regions', WFXMLNS.mwgRegions, 'RegionList' )
	local pathToAddedRegion = xmp:appendStructItemToArray( WFXMLNS.mwgRegions, regionListPath, true )
	
	assert( pathToAddedRegion == "Regions/mwg-rs:RegionList[1]", 'Incorrect region item path' )
	
	local numOfRegions = xmp:countArrayItems( WFXMLNS.mwgRegions, regionListPath )
	assert( numOfRegions == 1, 'Region count incorrect' )
	
	local itemPathAtFirstIndex = xmp:composeArrayItemPath( WFXMLNS.mwgRegions, regionListPath, 1 )
	assert( itemPathAtFirstIndex == pathToAddedRegion, "Incorrect composed array item path" )
	
	xmp:deleteArrayItem( WFXMLNS.mwgRegions, regionListPath, 1 )
	numOfRegions = xmp:countArrayItems( WFXMLNS.mwgRegions, regionListPath )
	
	assert( numOfRegions == 0, 'Region count incorrect' )
	
end

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

return WFXmpTest
