Skip to content

Commit

Permalink
Merge pull request #74 from DynamicsValue/feature/25x-improvements
Browse files Browse the repository at this point in the history
Extract and Refactor fake crud methods into reusable extensions Dynam…
  • Loading branch information
jordimontana82 authored Jun 7, 2024
2 parents 619c0e7 + a64bc5e commit 399eb72
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using FakeItEasy;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace FakeXrmEasy.Middleware.Crud
{
internal static class IOrganizationServiceMiddlewareExtensions
{
public static IOrganizationService AddFakeCreate(this IOrganizationService service)
{
A.CallTo(() => service.Create(A<Entity>._))
.ReturnsLazily((Entity e) =>
{
var request = new CreateRequest();
request.Target = e;
var response = service.Execute(request) as CreateResponse;
return response.id;
});

return service;
}

public static IOrganizationService AddFakeUpdate(this IOrganizationService service)
{
A.CallTo(() => service.Update(A<Entity>._))
.Invokes((Entity e) =>
{
var request = new UpdateRequest();
request.Target = e;
service.Execute(request);
});

return service;
}

public static IOrganizationService AddFakeRetrieveMultiple(this IOrganizationService service)
{
//refactored from RetrieveMultipleExecutor
A.CallTo(() => service.RetrieveMultiple(A<QueryBase>._))
.ReturnsLazily((QueryBase req) => {
var request = new RetrieveMultipleRequest { Query = req };
var response = service.Execute(request) as RetrieveMultipleResponse;
return response.EntityCollection;
});

return service;
}

public static IOrganizationService AddFakeRetrieve(this IOrganizationService service)
{
A.CallTo(() => service.Retrieve(A<string>._, A<Guid>._, A<ColumnSet>._))
.ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) =>
{
var request = new RetrieveRequest()
{
Target = new EntityReference() { LogicalName = entityName, Id = id },
ColumnSet = columnSet
};
var response = service.Execute(request) as RetrieveResponse;
return response.Entity;
});

return service;
}

public static IOrganizationService AddFakeDelete(this IOrganizationService service)
{
A.CallTo(() => service.Delete(A<string>._, A<Guid>._))
.Invokes((string entityName, Guid id) =>
{
if (string.IsNullOrWhiteSpace(entityName))
{
throw new InvalidOperationException("The entity logical name must not be null or empty.");
}
if (id == Guid.Empty)
{
throw new InvalidOperationException("The id must not be empty.");
}
var entityReference = new EntityReference(entityName, id);
var request = new DeleteRequest() { Target = entityReference };
service.Execute(request);
});

return service;
}

public static IOrganizationService AddFakeAssociate(this IOrganizationService service)
{
A.CallTo(() => service.Associate(A<string>._, A<Guid>._, A<Relationship>._, A<EntityReferenceCollection>._))
.Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) =>
{
var request = new AssociateRequest()
{
Target = new EntityReference() { Id = entityId, LogicalName = entityName },
Relationship = relationship,
RelatedEntities = entityCollection
};
service.Execute(request);
});

return service;
}

public static IOrganizationService AddFakeDisassociate(this IOrganizationService service)
{
A.CallTo(() => service.Disassociate(A<string>._, A<Guid>._, A<Relationship>._, A<EntityReferenceCollection>._))
.Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) =>
{
var request = new DisassociateRequest()
{
Target = new EntityReference() { Id = entityId, LogicalName = entityName },
Relationship = relationship,
RelatedEntities = entityCollection
};
service.Execute(request);
});

return service;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public static IMiddlewareBuilder AddCrud(this IMiddlewareBuilder builder)
#endif
context.SetProperty(crudMessageExecutors);
AddFakeCreate(context, service);
AddFakeRetrieve(context, service);
AddFakeRetrieveMultiple(context, service);
AddFakeUpdate(context,service);
AddFakeDelete(context,service);
AddFakeAssociate(service);
AddFakeDisassociate(service);
service.AddFakeCreate()
.AddFakeRetrieve()
.AddFakeRetrieveMultiple()
.AddFakeUpdate()
.AddFakeDelete()
.AddFakeAssociate()
.AddFakeDisassociate();
});

return builder;
Expand Down Expand Up @@ -94,7 +94,6 @@ public static IMiddlewareBuilder AddCrud(this IMiddlewareBuilder builder, IInteg
/// <returns></returns>
public static IMiddlewareBuilder UseCrud(this IMiddlewareBuilder builder)
{

Func<OrganizationRequestDelegate, OrganizationRequestDelegate> middleware = next => {
return (IXrmFakedContext context, OrganizationRequest request) => {
Expand Down Expand Up @@ -133,108 +132,5 @@ private static OrganizationResponse ProcessRequest(IXrmFakedContext context, Org
return fakeMessageExecutor.Execute(request, context);
}

private static void AddFakeCreate(IXrmFakedContext context, IOrganizationService service)
{
A.CallTo(() => service.Create(A<Entity>._))
.ReturnsLazily((Entity e) =>
{
var request = new CreateRequest();
request.Target = e;
var response = service.Execute(request) as CreateResponse;
return response.id;
});
}

private static void AddFakeUpdate(IXrmFakedContext context, IOrganizationService service)
{
A.CallTo(() => service.Update(A<Entity>._))
.Invokes((Entity e) =>
{
var request = new UpdateRequest();
request.Target = e;
service.Execute(request);
});
}

private static void AddFakeRetrieveMultiple(IXrmFakedContext context, IOrganizationService service)
{
//refactored from RetrieveMultipleExecutor
A.CallTo(() => service.RetrieveMultiple(A<QueryBase>._))
.ReturnsLazily((QueryBase req) => {
var request = new RetrieveMultipleRequest { Query = req };
var response = service.Execute(request) as RetrieveMultipleResponse;
return response.EntityCollection;
});
}

private static void AddFakeRetrieve(IXrmFakedContext context, IOrganizationService service)
{
A.CallTo(() => service.Retrieve(A<string>._, A<Guid>._, A<ColumnSet>._))
.ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) =>
{
var request = new RetrieveRequest()
{
Target = new EntityReference() { LogicalName = entityName, Id = id },
ColumnSet = columnSet
};
var response = service.Execute(request) as RetrieveResponse;
return response.Entity;
});
}

private static void AddFakeDelete(IXrmFakedContext context, IOrganizationService service)
{
A.CallTo(() => service.Delete(A<string>._, A<Guid>._))
.Invokes((string entityName, Guid id) =>
{
if (string.IsNullOrWhiteSpace(entityName))
{
throw new InvalidOperationException("The entity logical name must not be null or empty.");
}
if (id == Guid.Empty)
{
throw new InvalidOperationException("The id must not be empty.");
}
var entityReference = new EntityReference(entityName, id);
var request = new DeleteRequest() { Target = entityReference };
service.Execute(request);
});
}

private static void AddFakeAssociate(IOrganizationService service)
{
A.CallTo(() => service.Associate(A<string>._, A<Guid>._, A<Relationship>._, A<EntityReferenceCollection>._))
.Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) =>
{
var request = new AssociateRequest()
{
Target = new EntityReference() { Id = entityId, LogicalName = entityName },
Relationship = relationship,
RelatedEntities = entityCollection
};
service.Execute(request);
});
}

private static void AddFakeDisassociate(IOrganizationService service)
{
A.CallTo(() => service.Disassociate(A<string>._, A<Guid>._, A<Relationship>._, A<EntityReferenceCollection>._))
.Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) =>
{
var request = new DisassociateRequest()
{
Target = new EntityReference() { Id = entityId, LogicalName = entityName },
Relationship = relationship,
RelatedEntities = entityCollection
};
service.Execute(request);
});
}

}
}

0 comments on commit 399eb72

Please sign in to comment.