public void TestPreCommitVersionGreatThanCompare_FacebookCampaign()
        {
            ProxySync.ObjectState         facebookCampaignObjectState;
            ProxySync.PreCommitResponse[] responses = SyncDataServiceHelper.TestPreCommitVersionCompare_FacebookCampaign(-1, out facebookCampaignObjectState).ToArray();

            Assert.AreEqual(0, responses.Length, "The length of PreCommitResponse object array should be 0!!");
        }
        public void TestUpdateOneObjectStateWithClientVersionGreatThanServerVersion_FacebookCampaign()
        {
            //Initialize by precommit
            ProxySync.ObjectState       facebookCampaignObjectState;
            ProxySync.PreCommitResponse precommitResponse = SyncDataServiceHelper.DoPreCommitInitialize_FacebookCampaign(out facebookCampaignObjectState);
            Assert.IsNotNull(precommitResponse, "The PreCommitResponse object should be NOT NULL!!");

            //Insert Facebook Account entity
            TblFacebookAccount facebookAccountEntity = SyncDataServiceHelper.NextFacebookAccountEntity();

            facebookAccountEntity.Id = facebookCampaignObjectState.ParentId;
            SyncDataServiceHelper.RegisterCreatedFacebookAccountEntityForCleanup(facebookAccountEntity);
            SyncDataServiceHelper.InsertFacebookAccountIntoDB(facebookAccountEntity);

            TblFacebookCampaign facebookCampaignEntity;

            SyncDataServiceHelper.DoesExistFacebookCampaignEntityInDB(facebookCampaignObjectState.ObjectId, out facebookCampaignEntity);

            var inputDictionary = new Dictionary <long, ProxySync.ObjectState[]>();

            facebookCampaignObjectState.LocalVersion++;
            inputDictionary.Add(facebookCampaignEntity.LocalParentId, new ProxySync.ObjectState[] { facebookCampaignObjectState });

            ProxyDTO.SEMBaseDTO[] response = null;
            WCFHelper.Using <SyncDataServiceClient>(new SyncDataServiceClient(), client =>
            {
                response = client.Update(inputDictionary);
            });
            Assert.AreEqual(1, response.Length, "The length of response should be 1!!");
            Assert.AreEqual(facebookCampaignObjectState.LocalVersion - 1, response[0].Version, "The response version should be equal to localversion-1!!");
        }
        public void TestPreCommitVersionEqualCompare_FacebookCampaign()
        {
            ProxySync.ObjectState         facebookCampaignObjectState;
            ProxySync.PreCommitResponse[] responses = SyncDataServiceHelper.TestPreCommitVersionCompare_FacebookCampaign(0, out facebookCampaignObjectState).ToArray();

            Assert.IsNotNull(responses, "The PreCommitResponse object should be NOT NULL!!");
            Assert.AreEqual(1, responses.Length, "The length of PreCommitResponse array should be equal 1!!");
            Assert.AreEqual(facebookCampaignObjectState.LocalId, responses[0].GrainId, "The inputed objectstate object's LocalId should be equal to PreCommitResponse's GrainId returned from Server!!");
        }
 public override void OnTestInitialize()
 {
     base.OnTestInitialize();
     SyncDataServiceHelper = Get <SyncDataServiceHelper>();
 }
        public void TestMultipleClientsDoPreCommit_FacebookCampaign()
        {
            ProxySync.ObjectState facebookCampaignObjectState = SyncDataServiceHelper.NextObjectState();
            facebookCampaignObjectState.HostingId        = -1;
            facebookCampaignObjectState.ObjectDetailType = (int)SEMObjectDetailType.FacebookCampaign;
            facebookCampaignObjectState.IsVersionCompare = true;
            facebookCampaignObjectState.EngineType       = (int)SearchEngineType.Facebook;
            facebookCampaignObjectState.LocalId          = SyncDataServiceHelper.GenerateLocalId(facebookCampaignObjectState);

            TblFacebookCampaign facebookCampaignEntity = SyncDataServiceHelper.NextFacebookCampaignEntity();

            facebookCampaignEntity.Id       = facebookCampaignObjectState.ObjectId;
            facebookCampaignEntity.ParentId = facebookCampaignObjectState.ParentId;

            facebookCampaignEntity.Version = facebookCampaignObjectState.LocalVersion;

            SyncDataServiceHelper.RegisterCreatedFacebookCampaignEntityForCleanup(facebookCampaignEntity);
            SyncDataServiceHelper.InsertFacebookCampaignIntoDB(facebookCampaignEntity);

            var taskA = Task.Factory.StartNew <ProxySync.PreCommitResponse[]>(() =>
            {
                ProxySync.PreCommitResponse[] responsesA = null;
                ProxySync.ObjectState facebookCampaignObejctStateForLambdaExpressionA = facebookCampaignObjectState;
                WCFHelper.Using <SyncDataServiceClient>(new SyncDataServiceClient(), client =>
                {
                    responsesA = client.PreCommit(new ProxySync.ObjectState[] { facebookCampaignObejctStateForLambdaExpressionA });
                });
                return(responsesA);
            });

            var taskB = Task.Factory.StartNew <ProxySync.PreCommitResponse[]>(() =>
            {
                ProxySync.PreCommitResponse[] responsesB = null;
                ProxySync.ObjectState facebookCampaignObejctStateForLambdaExpressionB = facebookCampaignObjectState;
                WCFHelper.Using <SyncDataServiceClient>(new SyncDataServiceClient(), client =>
                {
                    responsesB = client.PreCommit(new ProxySync.ObjectState[] { facebookCampaignObejctStateForLambdaExpressionB });
                });
                return(responsesB);
            });

            Task.WaitAll(taskA, taskB);

            //Verify two task results
            if (taskA.Result == null && taskB.Result == null)
            {
                Assert.Fail("The two clients all return NULL!!");
            }
            if (taskA.Result != null && taskB.Result != null)
            {
                Assert.Fail("The two clients all don't return NULL!!");
            }
            if (taskA.Result != null && taskB.Result == null)
            {
                Assert.AreEqual(1, taskA.Result.Length, "The length of PreCommitResponse array should be equal 1!!");
                Assert.AreEqual(facebookCampaignObjectState.LocalId, taskA.Result[0].GrainId, "The inputed objectstate object's LocalId should be equal to PreCommitResponse's GrainId returned from Server!!");
            }
            if (taskA.Result == null && taskB.Result != null)
            {
                Assert.AreEqual(1, taskB.Result.Length, "The length of PreCommitResponse array should be equal 1!!");
                Assert.AreEqual(facebookCampaignObjectState.LocalId, taskB.Result[0].GrainId, "The inputed objectstate object's LocalId should be equal to PreCommitResponse's GrainId returned from Server!!");
            }
        }
        public void TestNewDataCommitOneSyncDataObject_FacebookCampaign()
        {
            //Initialize by precommit
            ProxySync.ObjectState       facebookCampaignObjectState;
            ProxySync.PreCommitResponse precommitResponse = SyncDataServiceHelper.DoPreCommitInitialize_FacebookCampaign(out facebookCampaignObjectState);
            Assert.IsNotNull(precommitResponse, "The PreCommitResponse object should be NOT NULL!!");

            //Insert Facebook Account entity
            TblFacebookAccount facebookAccountEntity = SyncDataServiceHelper.NextFacebookAccountEntity();

            facebookAccountEntity.Id = facebookCampaignObjectState.ParentId;
            SyncDataServiceHelper.RegisterCreatedFacebookAccountEntityForCleanup(facebookAccountEntity);
            SyncDataServiceHelper.InsertFacebookAccountIntoDB(facebookAccountEntity);

            //Generate randomized syncdata object
            ProxyDTO.Facebook.FacebookCampaignDTO facebookCampaignDTO = SyncDataServiceHelper.NextFacebookCampaignDTO();
            facebookCampaignDTO.ParentId = facebookAccountEntity.Id;

            //string JsonSerializedFacebookCampaignDTO = ServiceStack.Text.JsonSerializer.SerializeToString(facebookCampaignDTO, typeof(ProxyDTO.Facebook.FacebookCampaignDTO));
            ProxySync.SyncData facebookCampaignSyncData = SyncDataServiceHelper.NextSyncData();

            //Setting property of syncdata object
            facebookCampaignSyncData.ObjectDTO         = facebookCampaignDTO;
            facebookCampaignSyncData.ObjectId          = facebookCampaignObjectState.ObjectId;
            facebookCampaignSyncData.ParentId          = facebookCampaignObjectState.ParentId;
            facebookCampaignSyncData.LocalId           = precommitResponse.GrainId;
            facebookCampaignSyncData.EngineType        = (int)SearchEngineType.Facebook;
            facebookCampaignSyncData.ObjectDetailType  = (int)SEMObjectDetailType.FacebookCampaign;
            facebookCampaignSyncData.Operation         = 0;
            facebookCampaignSyncData.TimeStamp         = precommitResponse.PreCommitToken;
            facebookCampaignSyncData.GrainId           = facebookCampaignSyncData.LocalId;
            facebookCampaignSyncData.HostingCampaignId = -1;
            facebookCampaignSyncData.HostingId         = -1;
            facebookCampaignSyncData.HostingParentId   = -1;

            //Insert sequenceNumber into db
            TblSequenceNumber sequenceNumberEntity = SyncDataServiceHelper.NextSequenceNumberEntity();

            sequenceNumberEntity.EngineType = (int)facebookCampaignSyncData.EngineType;
            SyncDataServiceHelper.RegisterCreatedSequenceNumberEntityForCleanup(sequenceNumberEntity);
            SyncDataServiceHelper.InsertSequenceNumberIntoDB(sequenceNumberEntity);

            ProxySync.CommitResponse[] response = null;
            WCFHelper.Using <SyncDataServiceClient>(new SyncDataServiceClient(), client =>
            {
                response = client.NewDataCommit(new ProxySync.SyncData[] { facebookCampaignSyncData });
            });
            Assert.IsNotNull(response, "The response object should not be NULL!!");
            Assert.AreEqual(sequenceNumberEntity.Id + 1, response[0].HostingId, "The response object's hosting id should be equal to [tblSequenceNumber].[Id]+1");
            Assert.IsTrue(SyncDataServiceHelper.DoesExistSequenceNumberEntityInDB(
                              new TblSequenceNumber
            {
                Id         = sequenceNumberEntity.Id + 1,
                EngineType = sequenceNumberEntity.EngineType
            }));

            TblFacebookCampaign actualFacebookCampaignEntity;

            Assert.IsTrue(SyncDataServiceHelper.DoesExistFacebookCampaignEntityInDB(facebookCampaignDTO.Id, out actualFacebookCampaignEntity), "There should be one facebook campaign enty in db!!");
            SyncDataServiceHelper.CompareFacebookCampaignDTOAndEntityObject(facebookCampaignDTO, actualFacebookCampaignEntity, response[0].HostingId, response[0].HostingParentId, 1);
        }