FluentValidation: how to simplify this code? - ef-fluent-api

I'm using FluentValidation library to create validators like this:
Please any one help me to simplify this part of code?
private bool UniqueSimNo(string simNo)
{
MyContext _db = new MyContext();
Sim s = _db.Sims.Where(x => x.SimNo.ToLower() == simNo.ToLower()).SingleOrDefault();
var sim = _db.Sims.Where(x => x.SimNo.ToLower() == s.SimNo.ToLower()).Where(x => x.Id != s.Id).FirstOrDefault();
if (sim == null) return true;
return false;
}

Well, your code doesn't make sense if you don't pass an id as parameter to your method.
I guess you wanna use the same validator for a new item (id = 0) and an existing (id !=0).
This line doesn't do what you think it is doing after your SingleOrDefault test, as SingleOrDefault would raise an exception if there was more than one item with same SimNo :
var sim = _db.Sims.Where(x => x.SimNo.ToLower() == s.SimNo.ToLower()).Where(x => x.Id != s.Id).FirstOrDefault();
So I would do
private bool UniqueSimNo(string simNo, int id = 0) {
var _db = new MYContext());
return !_db.Sims.Any(x => x.Id != id && x.simNo.ToLower() == simNo.ToLower());
}

Related

How to use Moq in unit test that calls another method in same EF Repository

In my project I am using Repository.
I'm trying to unit test a SAVE method and return the call value via the Get method.
I'm having a Repository Query mockup issue when calling through the Get method. Can you help me in this case?
I Have a Class:
public class ClientRoleBo
{
private readonly IRepositoryBase<ClientRole> _repository;
public ClientRoleBo(IRepositoryBase<ClientRole> repository)
{
_repository = repository;
}
public Task<ClientRoleResp?> Get(Guid clientGuid, Guid id)
{
return Task.FromResult(
_repository.Query(x => x.Guid == id && x.ClientGuid == clientGuid && !x.IsDeleted)
.Select(x => new ClientRoleResp
{
Code = x.Code,
Guid = x.Guid,
IsActive = x.IsActive,
Name = x.Name
}).FirstOrDefault()
);
}
public async Task<ClientRole> Save(Guid clientGuid, Guid? guid, ClientRoleReq req)
{
ClientRole? data = null;
var existItem = _repository.Query(x => x.Code == req.Code && x.ClientGuid == clientGuid).FirstOrDefault();
if (existItem != null)
throw new HttpResponseException(400, "Exist clientrole");
data = new()
{
Code = req.Code,
Name = req.Name,
IsActive = req.IsActive,
ModifiedDate = DateTime.Now,
CreatedDate = DateTime.Now,
ClientGuid = clientGuid
};
await _repository.AddAsync(data);
return (await Get(clientGuid, data!.Guid))!;
}
}
I have a issue when code call method "Save" return data of method "Get" same Repository
My Mock Repository:
public class TestClientRole{
public static IRepositoryBase<TEntiy> MockRepo<TEntiy>(TEntiy[] data, Expression<Func<TEntiy, bool>> returnExpression = null) where TEntiy : class
{
var mock = new Mock<IRepositoryBase<TEntiy>>();
mock.Setup(x => x.Query(
It.IsAny<Expression<Func<TEntiy, bool>>>()
)).Returns(returnExpression != null ? data.AsQueryable().Where(returnExpression).AsEnumerable() : data.AsEnumerable());
return mock.Object;
}
[Fact]
public void Save()
{
var clientRoles = new ClientRole[]
{
new ClientRole
{
Code = "123",
Name = "Role1",
Guid = Guid.NewGuid(),
},
new ClientRole
{
Code = "1234",
Name = "Role2",
Guid = Guid.NewGuid(),
}
};
var mockRepo = MockRepo<ClientRole>(clientRoles, x => x.Guid == clientRoles[0].Guid);
var svc = new ClientRoleBo(mockRepo);
var res = svc.Save(Guid.NewGuid, null, new ClientRoleReq { Code = "Role", Name = "Role" }).GetAwaiter().GetResult();
Assert.True(res.Guid == clientRoles[0].Guid);
}
}

MapGesture.OnGestureListener to let user select route they want

UPDATE BELOW!!
I'm trying to let the user decide which of the 3 routes provided, they want to take. Here is the screenshot of the routes.
I am using the HERE SDK FOR ANDROID (PREMIUM EDITION) 3.18.5
Here is my code for adding the routes to the map:
coreRouter.calculateRoute(routePlan,
object : Router.Listener<List<RouteResult>, RoutingError> {
override fun onProgress(i: Int) {
}
override fun onCalculateRouteFinished(
routeResults: List<RouteResult>,
routingError: RoutingError
) {
if (routingError === RoutingError.NONE) {
routeResultsList = routeResults
if (routeResults[0].route != null) {
route = routeResults[0].route
mapRoute = MapRoute(routeResults[0].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
mapRoute!!.tag = "0"
map!!.addMapObject(mapRoute!!)
geoBoundingBox = routeResults[0].route.boundingBox
geoBoundingBox!!
map!!.zoomTo(
geoBoundingBox!!, Map.Animation.NONE, 5f
)
timeDistanceForCL()
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this#Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
if (routeResults[1].route != null) {
route = routeResults[1].route
mapRoute = MapRoute(routeResults[1].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.color = ContextCompat.getColor(this#Route, R.color.gray_lines)
mapRoute!!.zIndex = 2
mapRoute!!.tag = "1"
map!!.addMapObject(mapRoute!!)
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this#Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
if (routeResults[2].route != null) {
route = routeResults[2].route
mapRoute = MapRoute(routeResults[2].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.color = ContextCompat.getColor(this#Route, R.color.gray_lines)
mapRoute!!.zIndex = 1
mapRoute!!.tag = "2"
map!!.addMapObject(mapRoute!!)
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this#Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
} else {
Toast.makeText(
this#Route,
"Error: route calculation returned error code: $routingError",
Toast.LENGTH_LONG
).show()
}
}
})
When I add them to the map, I make the first route the normal color, then the other two are in grey and they have a lower zIndex as to not cover up the selected route.
I am using the MapGesture.OnGestureListener to determine when one of the routes is selected. Here is the code.
private val mapG: MapGesture.OnGestureListener = object : MapGesture.OnGestureListener.OnGestureListenerAdapter() {
override fun onMapObjectsSelected(p0: MutableList<ViewObject>): Boolean {
Log.d("onMapObjectsSelected", "onMapObjectsSelected ran")
for (p0 in routeResultsList) {
Log.d("onMapObjectsSelected", "onMapObjectsSelected for loop ran ran")
if (p0.route == routeResultsList[0].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(1) ran ran")
route = routeResultsList[0].route
mapRoute = MapRoute(routeResultsList[0].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
if (p0.route == routeResultsList[1].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(2) ran ran")
route = routeResultsList[1].route
mapRoute = MapRoute(routeResultsList[1].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
if (p0.route == routeResultsList[2].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(3) ran ran")
route = routeResultsList[2].route
mapRoute = MapRoute(routeResultsList[2].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
}
return super.onMapObjectsSelected(p0)
}
}
This code keeps the originally selected route [0] highlighted and I thought it was supposed to just highlight the other route that was selected. I haven't worked making the originally selected route grey yet, or update the time and distance at the top of the screen, but when I use the code, it runs all of the Log calls in it.
I'm calling private var mapGestures: MapGesture.OnGestureListener? = null
within the initMapFragmentView() where I have my other calls for NavigationListener.
private fun initMapFragmentView() {
val path = File(getExternalFilesDir(null), ".here-map-data")
.absolutePath
MapSettings.setDiskCacheRootPath(path)
val mapFragment = supportFragmentManager.findFragmentById(R.id.mapFragment) as AndroidXMapFragment?
val context = ApplicationContext(this).apply {
setAppIdCode(s, s1)
setLicenseKey(s2)
}
mapFragment?.let { fragment ->
fragment.init(context) { error ->
when (error) {
OnEngineInitListener.Error.NONE -> {
map = fragment.map
map?.run {
setCenter(
GeoCoordinate(36.9566664, -94.7881218, 0.0),
Map.Animation.NONE
)
setZoomLevel((maxZoomLevel + minZoomLevel) / 2)
}
navigationManager = NavigationManager.getInstance()
navigationManager!!.distanceUnit = NavigationManager.UnitSystem.IMPERIAL_US
navigationManager!!.addRerouteListener(
WeakReference(
mNavigaionRerouteListener
)
)
fragment.mapGesture?.addOnGestureListener(mapG, 0, true)
navigationManager!!.realisticViewMode = NavigationManager.RealisticViewMode.DAY
navigationManager!!.addRealisticViewAspectRatio(NavigationManager.AspectRatio.AR_4x3)
navigationManager!!.addRealisticViewListener(
WeakReference(viewListener))
voiceNavigation()
initNavigation()
}
else -> {
val errorMessage = "Error: ${error}, SDK Version: ${Version.getSdkVersion()}"
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
}
}
}
Here is the link to the HERE Docs for the Gestures.
I'm not positive if the issue is with the onMapObjectsSelected for loop, or how I have the if statements set up, or something else? When I click on any either of the grey polyLines, then all 3 of them get highlighted instead of just the one that was selected.
UPDATE!!
I have made a few code changes trying to get this to work. In the process I have found out that the values are different for the same MapRoute. For example:
if (routeResults[0].route != null) {
route = routeResults[0].route
mapRoute = MapRoute(routeResults[0].route)
mapRoute0 = MapRoute(routeResults[0].route)
val toAdd = MapRoute(routeResults[0].route)
Log.d("onMapObjectsSelected", "mapRouteList0 $toAdd")
Log.d("onMapObjectsSelected", "mapRoute $mapRoute")
Log.d("onMapObjectsSelected", "mapRoute0 $mapRoute0")
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
mapRoute!!.tag = "0"
mapRouteList.add(toAdd)
map!!.addMapObject(mapRoute!!)
geoBoundingBox = routeResults[0].route.boundingBox
geoBoundingBox!!
map!!.zoomTo(
geoBoundingBox!!, Map.Animation.NONE, 5f
)
timeDistanceForCL()
if (onPause == 1) {
startNavigation()
}
}
This will print out these values:
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRouteList0 com.here.android.mpa.mapping.MapRoute#c5f698ff
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRoute com.here.android.mpa.mapping.MapRoute#c5f68e0f
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRoute0 com.here.android.mpa.mapping.MapRoute#c5f6988f
I'm now trying to find out why the values are different for the same MapRoute(routeResults[0].route) ??
Am I misunderstanding how the MapRoute is named? Because when I go to click on a Route on the map, it comes back with a different value as well. Is there a better way to determine which route a user has selected?
The mapRoute = MapRoute(routeResults[0].route) statement in the above snippet is equivalent to MapRoute m_mapRoute = new MapRoute(route). As you have called the same method thrice, so it has created three different object.
For more details about the MapRoute class, please refer the below API reference document.
https://developer.here.com/documentation/android-premium/3.18/api_reference_java/index.html?com%2Fhere%2Fandroid%2Fmpa%2Fmapping%2FMapRoute.html
Please refer the standard routing and map-gestures implementation examples from the below HERE github repository.
https://github.com/heremaps/here-android-sdk-examples

Update list in Kotlin

So here is my Data Class
data class Bestellung (var id:Int = 0, var anzahl:Int = 1, var speise:String? = null)
my List
private var bestellungList = ArrayList<Bestellung>()
Trying to update the list if "speise" equals "s" but its not working without any error..
if (bestellungList.contains(Bestellung(speise = s))) {
var i = bestellungList.indexOf(Bestellung(speise = s))
bestellungList.set(i, Bestellung(anzahl = +1))
problem is contains
pls try this if you want to check first:
if(bestellungList.any{ it.speise == "s" }) {
// do add logic
} else {
// do something else
}
The problem is your contains part. Replace it like this
val index = bestellungList.indexOfFirst {
it.speise == s
}
if (index >= 0) {
bestellungList[index] = Bestellung(anzahl = +1)
}
if (bestellungList.any{ it.speise == "s" }) {
bestellungList.addAll(Bestellung(anzahl = +1))
} else {
// handle else statement also
}

CRM Late Bound - Cleaner Approach

I have the following code and I'm trying to find a more elegant approach to this. activityParty is a DataCollection. I am basically trying to get a list of recipients for an email, which can be of type users or contacts.
I am familiar with early bound but in this scenario must use late bound.
Is there a better approach to this?
var recipientParty = activityParty.Where(x => x.GetAliasedValueOrDefault<OptionSetValue>("ap.participationtypemask").Value == 2).ToList();
var recipientList = new List<string>();
foreach (var to in recipientParty)
{
if (to.Attributes.Contains("u.internalemailaddress"))
{
recipientList.Add(to.GetAliasedValueOrDefault<string>("u.internalemailaddress"));
}
if (to.Attributes.Contains("c.emailaddress1"))
{
recipientList.Add(to.GetAliasedValueOrDefault<string>("c.emailaddress1"));
}
}
Have a look at AddressUsed property of ActivityParty entity. It should contain email address, regardless which entity is source of party involved.
So, in your code you can use to.AddressUsed instead whole if {...} statement.
Try this:
using (var serviceContext = new OrganizationServiceContext(this.OrganizationService)) // if you are writing custom code activity
//using (var serviceContext = new OrganizationServiceContext(localContext.OrganizationService)) // if you are writing plugin
{
var activityPartySet = serviceContext.CreateQuery<ActivityParty>();
var activityParties = activityPartySet.Where(
ap => ap.PartyId != null &&
ap.ParticipationTypeMask != null &&
ap.ParticipationTypeMask.Value == 2).ToList();
var userSet = serviceContext.CreateQuery<SystemUser>();
var contactSet = serviceContext.CreateQuery<Contact>();
var recipientList = new List<string>();
foreach (var ap in activityParties)
{
var partyRef = ap.PartyId;
if (partyRef.LogicalName == SystemUser.EntityLogicalName)
{
var user = (from u in userSet
where u.Id == partyRef.Id
select new SystemUser
{
InternalEMailAddress = u.InternalEMailAddress
}).FirstOrDefault();
if (user != null)
recipientList.Add(user.InternalEMailAddress);
}
else if (partyRef.LogicalName == Contact.EntityLogicalName)
{
var contact = (from c in contactSet
where c.Id == partyRef.Id
select new Contact
{
EMailAddress1 = c.EMailAddress1
}).FirstOrDefault();
if (contact != null)
recipientList.Add(contact.EMailAddress1);
}
}
}
Hope it helps!

How to unit test unformatted method in mvc?

I have to unit test one of the very big and unformatted method in ASP.NET MVC 4.0.
Below is the code of action method :-
public ActionResult GetDetails(string ProdName, int ProdArea, int ProdAreaId= 0)
{
if (ProdAreaId == 0 && ProdArea == 1 && System.Web.HttpContext.Current.Session["ResponseProdAreaId"] != null)
{
ProdAreaId = (int)System.Web.HttpContext.Current.Session["ResponseProdAreaId"];
}
if (string.IsNullOrEmpty(ProdName))
{
if (System.Web.HttpContext.Current.Session["ProdName"] == null)
{
ProdName = Guid.NewGuid().ToString();
System.Web.HttpContext.Current.Session["ProdName"] = ProdName;
}
else
{
ProdName = System.Web.HttpContext.Current.Session["ProdName"].ToString();
}
}
else
{
ProdName = ProdName.Replace("___", " ");
}
List<StateDetail> stateList = ProductService.GetAllStates().Where(n => n.FKCountryID == (int)Countries.UnitedStates).ToList();
ProductAddressViewModel model = new ProductAddressViewModel
{
ProdArea = ProdArea,
FKProductID = CurrentProductId
};
model.States = stateList != null ? new SelectList(stateList, "StateID", "StateCode") : null;
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null && ProdAreaId == 0 && ProdArea == 1)
{
List<ProductAddressDto> lstprodaddresses = (List<ProductAddressDto>)System.Web.HttpContext.Current.Session[“ProdAddresses”];
if (lstprodaddresses.Count > 0)
{
AddressDto addrDto = lstprodaddresses.First().Address;
//save address in DB
model.Address1 = addrDto.Address1;
model.Address2 = addrDto.Address2;
model.ProdArea = 1;
model.City = addrDto.City;
model.IsDefault = true;
model.ProdName = model.ProdName;
model.SelectedAddressTypeID = (int)AddressType.Street;
model.ZIPCode = addrDto.ZIPCode;
model.SelectedStateId = addrDto.FKStateID;
model.AddressTypes = GetAddressTypes();
}
}
else if (model.FKProductID > 0)
{
ToolDto tool = ToolService.GetToolDetails(model.FKProductID);
if (ProdAreaId > 0)
{
model.AddressTypes = GetAddressTypes();
ProductAddressDto prodaddr = tool.ToolAddresses.First(n => n.Tool_AddressID == ProdAreaId);
model.Address1 = prodaddr.Address.Address1;
model.Address2 = prodaddr.Address.Address2;
model.City = prodaddr.Address.City;
model.SelectedStateId = prodaddr.Address.FKStateID;
model.ZIPCode = prodaddr.Address.ZIPCode;
model.SelectedAddressTypeID = prodaddr.Address.FKAddressTypeID;
model.IsDefault = prodaddr.IsDefault;
model.FKAddressID = prodaddr.FKAddressID;
model.Tool_AddressID = prodaddr.Tool_AddressID;
model.FKProductID = prodaddr.FKProductID;
model.AddressTypes = GetAddressTypes();
}
else
{
//address types
List<int> excludeAddrTypes = new List<int>();
foreach (ProductAddressDto prodadrdto in tool.ToolAddresses)
{
if (prodadrdto.Tool_AddressID != ProdAreaId)
{
excludeAddrTypes.Add(prodadrdto.Address.FKAddressTypeID);
}
}
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null)
{
excludeAddrTypes.Add((int)AddressType.Street);
}
var addrtypes = from AddressType e in Enum.GetValues(typeof(AddressType))
where !excludeAddrTypes.Contains((int)e)
select new { Id = (int)e, Name = e.ToString() };
model.AddressTypes = addrtypes.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
if (tool.ToolAddresses.Count == 0)
{
model.IsDefault = (ProdArea == 1);
}
}
}
else
{
//filter out address types if responsed tool is there
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null)
{
List<int> excludeAddrTypes = new List<int>();
excludeAddrTypes.Add((int)AddressType.Street);
var addrtypes = from AddressType e in Enum.GetValues(typeof(AddressType))
where !excludeAddrTypes.Contains((int)e)
select new { Id = (int)e, Name = e.ToString() };
model.AddressTypes = addrtypes.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
}
else
{
model.AddressTypes = GetAddressTypes();
}
model.IsDefault = (ProdArea == 1);
}
model.ProdName = ProdName;
return PartialView("_AddUpdateAddress", model);
}
May be the method is not in correct format.But i have to do it's unit testing.I have do that in several different ways.But i am not sure about its correctness.
I want to know that how should we do unit test for such a big and unformatted method like this.
Can anyone help me out on this ?
Below is the piece of code of my unit testing method :-
[TestMethod]
public void GetDetailsTest_NotEmpty()
{
var ProdName = random.ToString();
var ProdArea = random.Next();
var ProdAreaId = 0;
var _toolServiceMock = new Mock<IToolService>();
var _lookupServiceMock = new Mock<ILookupService>();
var stateList = new List<StateDto> {
new StateDto() { StateID = random.Next(), StateCode = Guid.NewGuid().ToString(), Description = random.ToString(), FKCountryID = 1 },
new StateDto() { StateID = random.Next(), StateCode = Guid.NewGuid().ToString(), Description = random.ToString(), FKCountryID = random.Next() },
};
_lookupServiceMock.Setup(s => s.GetAllStates()).Returns(stateList); // .Returns(stateList);
//Arrange
CustomerDto cust = _toolService.LookupCustomers("", "").FirstOrDefault();
if (cust != null)
{
ToolDto tool = _toolService.GetToolDetails(cust.Tool.toolId);
if (tool.ToolAddresses.Count > 0 && tool.ToolAddresses.First().Address != null)
{
HttpContext.Current.Session["FKToolID"] = cust.FKToolID;
var controller = new ToolController(_toolServiceMock.Object);
PartialViewResult result = controller.SelectAddress(cust.Tool.Name, 1, tool.ToolAddresses.First().Tool_AddressID) as PartialViewResult;
var viewmodel = (ToolAddressViewModel)((ViewResultBase)(result)).Model;
if (viewmodel != null)
{
//Act
Assert.AreEqual(tool.ToolAddresses.First().Address.Address1, viewmodel.Address1);
Assert.AreEqual("_AddUpdateAddress", result.ViewName);
Assert.IsInstanceOfType(viewmodel, typeof(ToolAddressViewModel));
}
//Act
_lookupServiceMock.VerifyAll();
}
}
}
First of all, your method is a way too complicated and too long.
Make your actions short and with one responsability to respect SOLID principle (http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)).
It will make your methods easier to test.
To help you with your problem, you can do something quick with your code :
Split your actions in internal virtual methods with one concern each (internal to be testable in you unit test project, virtual to be able to mock them).
Put the [assembly: InternalsVisibleTo("YourTestAssembly")] on your controller
In your unit test project, you will be able to test any of your internal methods separately.
Finally, to test your action, use a mocking framework (RhinoMock, Moq) to mock all your internal virtual methods and test the logic of your action (proxies generated by mocking framework will let you mock virtual methods).
It's the easiest way you can test your logic without breaking the existing application.
The inconvenient is that your logic is in internal methods wich let the whole assembly able to use it.