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

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.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.utils
local WFTableMerge = require 'WFTableMerge'

local KSTableUtils = require "KSTableUtils"

	-- Lrmobile_models
local LriOzRenditionServerUtils = require 'LriOzRenditionServerUtils'	

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

local LriOzRenditionServerUtilsTest = {}

local shouldDownload = LriOzRenditionServerUtils.shouldDownloadOzRendition

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

local assetExample = {
	_changeCounter = 30,
	_id = "f47741bf0c494afc84b8146479427952",
	_docType = "asset",
	_localId = 11,
	_localOnly = {
		lrm_customData = {
			importSettings = {
				currentRevision = "abe63a9d597748c3b158b9e59edad5a0",
				data = "eJxjYBgFo2AUjFQAAAQAAAE=",
				isValidForCurrentRevision = false,
			},
		},
	},
	_revId = "c1b2764625282c145ba27bce13202755",
	_serverProvidedMetadata = {
		base = "https://photos.adobe.io/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/",
		created = "2017-11-23T16:22:14.425212Z",
		links = {
			[ "/rels/master" ] = {
				content_type = "image/jpeg",
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/master",
			},
			[ "/rels/master_create" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/{revision_id}/master",
				templated = true,
			},
			[ "/rels/proxy_create" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/{revision_id}/proxies/{proxy_id}",
				templated = true,
			},
			[ "/rels/proxy_type/2560" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/proxies/4dae07db087a82c45d4b88f07cec70c6",
			},
			[ "/rels/rendition_create" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/{revision_id}/renditions/{rendition_id}",
				templated = true,
			},
			[ "/rels/rendition_type/1280" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/renditions/7075d542aeb4b72e82696446e0210ff7",
			},
			[ "/rels/rendition_type/2048" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/renditions/7c615e30b485d91dd382797e1c009fe6",
			},
			[ "/rels/rendition_type/640" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/renditions/a87e0aafa89613cd661a06671e381b8b",
			},
			[ "/rels/rendition_generate/fullsize" ] =  {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/renditions/{rendition_id}?rendition_type=fullsize",
				templated = true
			},
			[ "/rels/rendition_type/thumbnail2x" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/renditions/7ecc9c3e707a2d09ea157d9f7c4670dd",
			},
			[ "/rels/xmp" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/abe63a9d597748c3b158b9e59edad5a0/xmp",
			},
			[ "/rels/xmp_develop_create" ] = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952/revisions/{revision_id}/xmp/develop",
				templated = true,
			},
			self = {
				href = "/v2/catalogs/ee47bf5197544783b9e2f7948c1e6aee/assets/f47741bf0c494afc84b8146479427952",
			},
		},
		revision_ids = {
			"abe63a9d597748c3b158b9e59edad5a0",
			"0b09a0598b7ea86980bd39b82e2f532e",
			"73644f28187ef1d445084e0170c3d4f5",
			"ac41994f42da323deb36209b7c24e8ce",
			"c1b2764625282c145ba27bce13202755",
		},
		updated = "2017-11-23T16:22:29.908045Z",
	},
	aesthetics = {
		application = "ias",
		balancing = 6,
		content = 20,
		created = "2017-11-23T16:22:29Z",
		dof = -1,
		emphasis = -12,
		harmony = 24,
		lighting = -2,
		repetition = 8,
		rot = 2,
		score = 62,
		symmetry = 4,
		version = 1,
		vivid = -22,
	},
	autoTags = {
		application = "haystack2",
		created = "2017-11-23T16:22:29Z",
		tags = {
			architecture = 54,
			asia = 56,
			blue = 69,
			bridge = 59,
			building = 59,
			city = 60,
			cloud = 54,
			clouds = 59,
			highway = 55,
			industrial = 53,
			landscape = 58,
			old = 56,
			rail = 54,
			river = 58,
			road = 63,
			sea = 53,
			sky = 68,
			stadium = 58,
			station = 56,
			traffic = 54,
			train = 56,
			travel = 61,
			urban = 54,
			view = 56,
			water = 58,
		},
		version = 1,
	},
	captureDate = "2017-10-13T18:23:05.852+01:00",
	develop = {
		croppedHeight = 3024,
		croppedWidth = 4032,
		processingModel = "lightroom",
		userOrientation = 1,
	},
	importSource = {
		contentType = "image/jpeg",
		fileName = "IMG_0014.JPG",
		fileSize = 2.6992e+06,
		importTimestamp = "2017-11-23T17:22:08.372+01:00",
		importedBy = "f9e358e68b7137a5d48d66b6e20f2d7a",
		importedOnDevice = "immisch-mp {3d9fa297a94157383b546ff302deb02085271e6f6ccc28ec3b7f630d0d5f3847}",
		localAssetId = "61BDB91E-A15A-4070-9E41-0903803BF196/L0/001",
		originalHeight = 3024,
		originalWidth = 4032,
		sha256 = "a97e791724857a969cbe0416b10f8e6828694d51060269e0db5d2279d3978693",
		uniqueDeviceId = "3d9fa297a94157383b546ff302deb02085271e6f6ccc28ec3b7f630d0d5f3847",
	},
	subtype = "image",
	type = "asset",
	xmp = {
		aux = {
			Lens = "iPhone 7 Plus back dual camera 3.99mm f/1.8",
		},
		exif = {
			ApertureValue = {
				1.69599e+06,
				1e+06,
			},
			BrightnessValue = {
				3504,
				979,
			},
			DateTimeOriginal = "2017-10-13T18:23:05.852+02:00",
			ExposureBiasValue = {
				0,
				1,
			},
			ExposureProgram = "normal program",
			ExposureTime = {
				1,
				35,
			},
			FNumber = {
				18,
				10,
			},
			FlashFired = false,
			FocalLength = {
				399,
				100,
			},
			FocalLengthIn35mmFilm = 28,
			ISOSpeedRatings = 40,
			MeteringMode = "pattern",
			ShutterSpeedValue = {
				5.12928e+06,
				1e+06,
			},
		},
		photoshop = {
			DateCreated = "2017-10-13T18:23:05.000+02:00",
		},
		tiff = {
			Make = "Apple",
			Model = "iPhone 7 Plus",
			Orientation = "top left",
		},
		xmp = {
			CreateDate = "2017-10-13T18:23:05.000+02:00",
			CreatorTool = "Photos 3.0",
			ModifyDate = "2017-10-13T18:23:05.000+02:00",
		},
	},
}

local assetWithoutRenditions = {
	_changeCounter = 30,
	_id = "915f7236b26b462f8e5be20a3f78c05e",
	_docType = "asset",
	_localId = 11,
	_revId = "c1b2764625282c145ba27bce13202755",
	_spaceId = "s3p2764625282c145ba27bce13202755",
	captureDate = "2017-10-13T18:23:05.852+01:00",
	develop = {
		croppedHeight = 3024,
		croppedWidth = 4032,
		processingModel = "lightroom",
		userOrientation = 1,
	},
	importSource = {
		contentType = "image/jpeg",
		fileName = "IMG_0014.JPG",
		fileSize = 2.6992e+06,
		importTimestamp = "2017-11-23T17:22:08.372+01:00",
		importedBy = "f9e358e68b7137a5d48d66b6e20f2d7a",
		importedOnDevice = "immisch-mp {3d9fa297a94157383b546ff302deb02085271e6f6ccc28ec3b7f630d0d5f3847}",
		localAssetId = "61BDB91E-A15A-4070-9E41-0903803BF196/L0/001",
		originalHeight = 3024,
		originalWidth = 4032,
		sha256 = "a97e791724857a969cbe0416b10f8e6828694d51060269e0db5d2279d3978693",
		uniqueDeviceId = "3d9fa297a94157383b546ff302deb02085271e6f6ccc28ec3b7f630d0d5f3847",
	},
	subtype = "image",
	type = "asset",
	xmp = {
		aux = {
			Lens = "iPhone 7 Plus back dual camera 3.99mm f/1.8",
		},
		exif = {
			ApertureValue = {
				1.69599e+06,
				1e+06,
			},
			BrightnessValue = {
				3504,
				979,
			},
			DateTimeOriginal = "2017-10-13T18:23:05.852+02:00",
			ExposureBiasValue = {
				0,
				1,
			},
			ExposureProgram = "normal program",
			ExposureTime = {
				1,
				35,
			},
			FNumber = {
				18,
				10,
			},
			FlashFired = false,
			FocalLength = {
				399,
				100,
			},
			FocalLengthIn35mmFilm = 28,
			ISOSpeedRatings = 40,
			MeteringMode = "pattern",
			ShutterSpeedValue = {
				5.12928e+06,
				1e+06,
			},
		},
		photoshop = {
			DateCreated = "2017-10-13T18:23:05.000+02:00",
		},
		tiff = {
			Make = "Apple",
			Model = "iPhone 7 Plus",
			Orientation = "top left",
		},
		xmp = {
			CreateDate = "2017-10-13T18:23:05.000+02:00",
			CreatorTool = "Photos 3.0",
			ModifyDate = "2017-10-13T18:23:05.000+02:00",
		},
	},
}

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

-- These three tests need to be kept in sync with the ComputeContentId tests in TIBlobProviderTests.mm

function LriOzRenditionServerUtilsTest.rxTestComputeContentId( rxTest )

	local contentId = LriOzRenditionServerUtils.renditionContentId( assetExample )
	
	rxTest:expectEqual( 'fb4fe699845e71c7544d649024d8f949', contentId )

end

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

function LriOzRenditionServerUtilsTest.rxTestComputeContentIdContributed( rxTest )

	local contentId = LriOzRenditionServerUtils.renditionContentId( assetWithoutRenditions )
	
	rxTest:expectEqual( '3ebfb2cb0d1e213654f2f9ff823134e2', contentId )

	local newRev = KSTableUtils.deepCopy( assetWithoutRenditions )
	
	newRev._revId = '14d4737738d44891829a2d5bbf904303'

	contentId = LriOzRenditionServerUtils.renditionContentId( newRev )

	rxTest:expectEqual( '1615e8ecce49d6868c3efa12c6d8b626', contentId )

end

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

function LriOzRenditionServerUtilsTest.rxTestComputeContentIdSha256Missing( rxTest )

	-- simulate asset with missing importSource.sha256

	local assetWithMissingSha256 = WFTableMerge.applyOverlay( assetExample, {
			importSource = {
				sha256 = {
					_delete = true
				}
			},
		} )

	local contentId = LriOzRenditionServerUtils.renditionContentId( assetWithMissingSha256 )

	rxTest:expectEqual( 'c41195fbdb8801a1af708008f3b768ca', contentId )

end

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

function LriOzRenditionServerUtilsTest.rxTestShouldDownload( rxTest )

	local revId = string.rep( 'a', 32 )
	
	local docValue = {
		_revId = revId,
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
			revision_ids = {
				revId,
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( nil, 'thumbnail2x' ), false )
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), true )
	
	local contentId = LriOzRenditionServerUtils.renditionContentId( docValue )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_localOnly = {
				files = {
					thumbnail2x = {
						contentId = contentId,
						path = 'blobs/...',
					}
				}
			},
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'ffff/renditions/aaaa',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/dddd',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/aaaa',
						invalid = false
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/2048' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'preview' ), true )
	
	docValue = {
		_localOnly = {
				files = {
					thumbnail2x = {
						contentId = contentId,
						path = 'blobs/...'
					}
				}
			},
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaab',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_localOnly = {
			files = {
				thumbnail2x = {
					contentId = contentId,
					path = 'blobs/...',
				}
			}
		},
		_serverProvidedMetadata = {
			links = {
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
end

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

return WFRxTestCase.wrapSuite(  LriOzRenditionServerUtilsTest )

