Junit How to assert Or Condition SQL Query - junit

Or Condition Query Example
SELECT ID, NAME, CREATE_DATE, UPDATE_DATE
FROM USER
WHERE ( CREATE_DATE > SYSDATE-1 OR UPDATE_DATE > SYSDATE -1)
How to assert OR Condition Query with junit?
// this test is fail : createDate is "2022-07-28" || updateDate is null
String oneDaysAgo = "2022-07-27";
List<UserInfo> list = jpaRepository.findAllByCreateDateGreaterThanOrUpdateDateGreaterThan(oneDaysAgo,oneDaysAgo);
for (UserInfo vo : list) {
assertThat(vo.getCreatedDate(), is(greaterThan(oneDaysAgo)));
assertThat(vo.getUpdateDate(), is(greaterThan(oneDaysAgo)));
log.info(vo.toString());
}
Expect assert
( createDate > oneDaysAgo ) or ( updateDate > oneDaysAgo )
OneDaysAgo = "2022-07-27";
Create Date
Update Date
Expect Result
Actual Result
null or lessThan or equal
null or lessThan or equal
False
False
"2022-07-28"
null or lessThan or equal
True
False
null or lessThan or equal
"2022-07-28"
True
False
"2022-07-28"
"2022-07-28"
True
True

thx for comment #pL4Gu33
Solve problem with Add Helper Method
#Test
void test_orCondition() {
String oneDaysAgo = "2022-07-27";
List<UserVo> list = getUserVoList(oneDaysAgo);
assertThat(list.size(), is(greaterThan(0)));
for (UserVo vo : list) {
assertThat(
getOrConditionBoolean(LocalDate.parse(oneDaysAgo)
, vo.getCreateDate(), vo.getUpdateDate()), is(true));
}
}
private Boolean getOrConditionBoolean(LocalDate baseDate, LocalDate arg1, LocalDate arg2) {
if (checkDate(arg1, baseDate)) {
return true;
}
if (checkDate(arg2, baseDate)) {
return true;
}
return false;
}
private boolean checkDate(LocalDate argDate, LocalDate baseDate) {
if (argDate == null) {
return false;
}
if (baseDate.isEqual(argDate) || baseDate.isBefore(argDate)) {
return true;
}
return false;
}

Related

Best way to organize this logic?

I have a function with two parameters A and B which can both be null or not. I am looking for the most concise way of writing this function. The following is the rules for it.
case A == null, B == null : do nothing
case A == null, B != null : create new instance of A and call A.doStuff(B)
case A != null, B != null : call A.doStuff(B)
case A != null, B == null : call A.dostuff(B)
This is what I have come up with:
public void myFunction( MyObject A, MyObject b )
{
if (A == null)
{
if (B != null)
{
A = new A();
}
else
{
return;
}
}
A.doStuff( B );
}
Well, I think it is a matter of your personal preference. Honestly, I would write it like this:
public void myFunction( MyObject A, MyObject b ) {
if (A == null && B == null) return;
if (A == null) A = new A();
A.doStuff(b);
}
public void myFunction( MyObject A, MyObject b ) {
if (A){
A.doStuff(b);
}else if (B){
A = new A();
A.doStuff(b)
}
}
This would be how I would write it

bind date to inpubtbox with angular dart

I'm building a webapp in which are inputboxes with a date. How can I bind the data to my background code?
I tried just doing it directly but i guess an inputbox can only handle strings so a made a convert to string method and so on.
<div>
<label>start date: </label>
<input [(ngModel)]="item.stringStartDate" placeholder="start date" type="date">
</div>
void set startDate(DateTime startDate)
{
_startDate = startDate;
}
void set stringStartDate(String startDate)
{
this.startDate = parseDate(startDate);
}
static DateTime parseDate(String formattedString)
{
final RegExp r = new RegExp(
r'^(\d\d)-?(\d\d)-?([+-]?\d{4,6})');
Match match = r.firstMatch(formattedString);
if (match != null)
{
int years = int.parse(match[3]);
int month = int.parse(match[2]);
int day = int.parse(match[1]);
return new DateTime(years, month, day);
}
else
{
throw new FormatException("Invalid date format", formattedString);
}
}
static String dateToString(DateTime date){
if (date == null)
return null;
String ret = "${date.year}/";
if (date.month < 10)
ret+="0";
ret+="${date.month}/";
if (date.day < 10)
ret += "0";
ret+="${date.day}";
return ret;
}
it should be separated by - instead of /
you can use toIso8601String and substring
DateTime _date = new DateTime.now();
String get date => _date.toIso8601String().substring(0, 10);
void set date(value) {
if (value is DateTime) {
_date = value;
} else if (value is String) {
_date = DateTime.parse(value);
}
}

Linq + nested GroupBy

We are using mySQL with WCF Restful service, we want to fetch our database content PACKAGEID wise...as mentioned below:
PackageId=2
{
StartRange = 1;
EndRange = 100;
IsDiscountAndChargeInPer = 1;
Discount =10;
ServiceCharge = 20;![enter image description here][1]
},
{
StartRange =101;
EndRange = 500;
IsDiscountAndChargeInPer = 1;
Discount =10;
ServiceCharge = 20;
}
PackageId=3
{
StartRange = 1;
EndRange = 100;
IsDiscountAndChargeInPer = 1;
Discount =10;
ServiceCharge = 20;
}
We have created a Result class to return result from out wcf service...for above service we have result class defined as below
[DataContract]
public class PackagePointRangeConfigurationResult : ResultBase
{
[DataMember]
public List<PackagePointRangeResult> Result;
}
public class PackagePointRangeResult
{
public int PackageId { get; set; }
public List<PointPlanInfo> Result { get; set; }
}
For fetching record we use following linq query:
var result = (from planinfo in db.mst_pointplan_info
join entityType in db.mst_entity_type
on planinfo.entityId equals entityType.id
where planinfo.entityId == entityId
&& planinfo.is_deleted != true
&& planinfo.system_id == systemId
&& entityType.enity_enum_id == entityId
group planinfo by planinfo.package_id into gplan
select new PackagePointRangeConfigurationResult
{
Result = (from planinfo in gplan
select new PackagePointRangeResult
{
PackageId = planinfo.package_id,
PointPlanInfo = (from pointPlanInfo in gplan
select new PointPlanInfo
{
StartRange = planinfo.start_range,
EndRange = planinfo.end_range,
IsDiscountAndChargeInPer = planinfo.is_discount_and_charge_in_per,
Discount = planinfo.discount,
ServiceCharge = planinfo.servicecharge,
AtonMerchantShare =planinfo.aton_merchant_share,
CommunityShare = planinfo.community_share
}).ToList()
}).ToList()
}).FirstOrDefault();
But it gives me below error:
LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[PointPlanInfo] ToList[PointPlanInfo](System.Collections.Generic.IEnumerable1[PointPlanInfo])' method, and this method cannot be translated into a store expression.

Jackson bug (or feature!?) when using java.util.Set - mySet.size() is always 1

I am using Jackson 2.2.0 and Spring 3.2.0 with Hibernate 4.2.2.
I recently had to send an array of objects via POST to the server:
{"cancelationDate":"2013-06-05",
"positions":[
{"price":"EUR 12.00",
"count":1},
{"price":"EUR 99.00",
"count":1}
]
}
My classes look like this:
public class Bill extends {
LocalDate cancelationDate;
Set<Position> positions;
...
}
and:
public class Position {
Integer count;
BigMoney price;
#JsonIgnore
Bill bill;
...
}
When I call bill.getPositions().size() it tells me 1.
If I use List<Position> instead of Set<Position> it works nice. So what's the problem with Set?
Thank you :)
equals and hashCode:
public int hashCode() {
final int prime = 31;
int result = 1;
result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
return result;
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Position))
return false;
Position equalCheck = (Position) obj;
if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
return false;
if (id != null && !id.equals(equalCheck.id))
return false;
return true;
}
Since id is null for the Jackson deserialized Positions, hashCode returns the same value for the different objects, and equals returns true. A Set cannot contain to elements which are equal. Fix your equals/hashcode implentation and everything will work as it should.
Suggested new hashCode/equals:
public int hashCode() {
final int prime = 31;
int result = 1;
result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
result = (int) (prime * result + ((price== null) ? 0 : price.hashCode()));
return result;
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Position))
return false;
Position equalCheck = (Position) obj;
if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
return false;
if (id != null && !id.equals(equalCheck.id))
return false;
if ((price== null && equalCheck.price != null) || (price != null && equalCheck.price == null))
return false;
if (price!= null && !price.equals(equalCheck.idprice)
return false;
return true;
}
the Set use equals and hashcode methods when it insert a row, have you overriden them?
I had also some bug with jackson 2.2.1 (with Maps) , you should upgrade to jackson 2.2.2

LINQ2SQL A problem with return items based on a parameter

I occured a strange problem. I have that method
public static void ProcessCategories(int? myID)
{
var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID).ToList();
}
when myID == null (the parameter), the tmpList doesn't contain any elements, but if I type
x.IdParentCategory == null then some items are returned. Why ?
Try this:
public static void ProcessCategories(int? myID)
{
var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID || (myID == null && x.IdParentCategory == null)).ToList();
}