Validation with CrudRepository - appscan

I'm using HCL cloud.appscan.com and I have Validation.Required warnings on things like this:
Source: com.fasterxml.jackson.databind.ObjectMapper.readValue(String;Class):Object via myclass...
Sink: org.springframework.data.repository.CrudRepository.save(Object):Object via myclass...
In my code I do validate it, but I have no idea how it determines if I am validating the objects before persisting. What code do I need to clear the warnings?
try {
Validate.isTrue(myObject.getMyInt() >= 0);
record.setMyObject(myObject.getMyInt());
save(record);
} catch (Exception e){
// do something
}

You can mark this warning as noise since you are already validating the input. If you really don't want to report this finding during the scan, you can create a validation method and add a #ValidatorMethod (Java) or [ValidatorMethod] (C#) using AppScan annotation library.
For Example
...
isValid(myObject);
record.setMyObject(myObject.getMyInt());
save(record);
...
#ValidationMethod
isValid(YourObject myObject) {
Validate.isTrue(myObject.getMyInt() >= 0);
}

Related

if the element is displayed then the test failed

I need a way to make the test fail with specific message. For example if the element isDisplayed() i want the test to fail
element(myElement).isDisplayed().then(function(result) {
if (result) {
//test fail
}
else {
//do something else
}
}
You could use jasmine's done.fail:
it('your test', (done) => {
element(myElement).isDisplayed().then((result) => {
if (result) {
done.fail('a custom error message');
}
//do something else
});
});
You can simply pass the failure message as an extra parameter to jasmine matchers for every failure.Look at below example.
expect(element(myElement).isDisplayed()).toBe(true,'myElement is not displayed in the page.')
Here the message myElement is not displayed on the page. will be displayed in console/reporter if the above expect statement fails.
If you are using jasmine for reporting then you can override custom error message by implementing custom matchers for expect statements.
Note: we can directly pass custom error message as an additional parameter for in expect statements but that is not documented feature so may not be supported in future.It is better approach to implement it as custom matcher.

How to get a CycAccess object?

There's a lisp function in ResearchCyc called random-assertion. I want to call that from some Java code. I'm using the Cyc Core API Suite v1.0.0-rc5 (from http://dev.cyc.com), but I don't see any way to call underlying Lisp code.
In the old OpenCyc API there was an object called CycAccess that you could use for this, but I can't figure out how to get one. If I could find it, I'd call this
access.converseObject("(random-assertion)");
At least in ResearchCyc, this would retrieve a pseudo-random assertion from the Cyc knowledge base. Not sure if it would work in OpenCyc, but it might also work there.
Can someone explain how to call lisp-code like this through Cyc's java API?
(Disclaimer: I am one of the developers of the Cyc APIs...)
The reference implementation of the Core API Suite is the Core Client, which is based on the Base Client... which, in turn, is derived from the old OpenCyc API. So, it's absolutely possible to call arbitrary lisp (SubL) code on ResearchCyc, in several different ways...
First of all, there already is a method which wraps random-assertion:
try {
CycAccess access = CycAccessManager.getCurrentAccess();
CycAssertion cycAssertion = access.getLookupTool().getRandomAssertion();
} catch (SessionException ex) {
// Do something with the exception...
} catch (CycConnectionException connEx) {
// Do something else...
}
Speaking to the general case, though, you'll find that the syntax is fairly similar to the OpenCyc API:
try {
CycAccess access = CycAccessManager.getCurrentAccess();
Object cycAssertion = access.converse().converseObject("(random-assertion)");
} catch (SessionException ex) {
// Do something with the exception...
} catch (CycConnectionException connEx) {
// Do something else...
}
Or, if it's safe to assume that the result will be a CycObject:
...
CycAccess access = CycAccessManager.getCurrentAccess();
CycObject cycAssertion = access.converse().converseCycObject("(random-assertion)");
...
However, the Base Client adds a new way of encapsulating SubL functions, via the com.cyc.baseclient.subl.SublFunction interface. The SublFunction interface itself is pretty minimal, but there are a number of classes under com.cyc.baseclient.subl.subtypes which provide implementations for you to extend. For example, if you're calling a no-arg function and expect back a CycObject, you could extend SublCycObjectNoArgFunction like so:
public static final SublCycObjectNoArgFunction RANDOM_ASSERTION_FUNCTION =
new SublCycObjectNoArgFunction("random-assertion");
...
try {
CycAccess access = CycAccessManager.getCurrentAccess();
CycObject cycAssertion = RANDOM_ASSERTION_FUNCTION.eval(access);
} catch (SessionException ex) {
// Do something with the exception...
} catch (CycConnectionException connEx) {
// Do something else...
}
(For other examples of this, see com.cyc.baseclient.subl.functions.* in the Base Client.)
This approach makes it fairly simple to define SubL functions as static fields without writing (or re-writing) much code. We expect the Core Client to gradually migrate towards this approach.
Lastly, you can use the implementation classes in the KB Client to convert your results to KBObjects. For example:
try {
CycAccess access = CycAccessManager.getCurrentAccess();
CycObject cycAssertion = RANDOM_ASSERTION_FUNCTION.eval(access);
// To convert to a com.cyc.kb.Assertion:
Assertion assertion = AssertionImpl.get(cycAssertion);
// Or, to convert to a more general KBObject:
KbObject kbObj = KbObjectImpl.get(cycAssertion);
} catch (SessionException ex) {
// Do something with the exception...
} catch (CycConnectionException connEx) {
// Do something else...
} catch (KbTypeException ex) {
// Potentially thrown by AssertionImpl#get() & KbObjectImpl#get()
} catch (CreateException ex) {
// Also potentially thrown by AssertionImpl#get() & KbObjectImpl#get()
}

Conditional exception raising in a flow block

While using Akka's data-flow DSL, I have twice encountered a need to throw an exception inside future, conditionally. This is how I am doing it:
flow {
// ...
if (someCond)
shiftUnit(throw new SomeException)
else
Future().apply()
// ...
}
Is this the correct way to do it? Or is there a better approach?
The approach seems correct (although my knowledge is a bit rusty), you can even leave out the other branch, the following works for me (Scala 2.10.1):
flow { if (x == 2) shiftUnit(throw new Exception) }
which results in a Future[Unit].

Entity Framework and Nested Lambda Expressions

I've just started using Lambda expressions, and really like the shortcut. I also like the fact that I have scope within the lambda of the encompassing method. One thing I am having trouble with is nesting lambdas. Here is what I am trying to do:
public void DoSomeWork()
{
MyContext context = new MyDomainContext();
context.GetDocumentTypeCount(ci.CustomerId, io =>
{
if (io.HasError)
{
// Handle error
}
// Do some work here
// ...
// make DB call to get data
EntityQuery<AppliedGlobalFilter> query =
from a in context.GetAppliedGlobalFiltersQuery()
where a.CustomerId == ci.CustomerId && a.FilterId == 1
select a;
context.Load<AppliedGlobalFilter>(query, lo =>
{
if (lo.HasError)
{
}
**// Do more work in this nested lambda.
// Get compile time error here**
}
}, null);
}, null);
}
The second lambda is where I get the following compile time error:
Cannot convert Lambda expression to type 'System.ServiceModel.DomainService.Client.LoadBehavior' because it is not a delegate type
The compiler is choosing the wrong overload for the Load method even though I am using the same override I did in the previous Lambda.
Is this because I am trying to nest? Or do I have something else wrong?
Thanks,
-Scott
Found the problem as described in my comment above. I'll head back to work now - red face and all....
I realize this is not the answer you want, but I suggest caution about lengthy and/or nested lambdas. They work, but they often make code harder to read / maintain by other developers. I try to limit my lambdas in length to three statements, with no nesting.

Is it possible to get information about which advice has been caught in an adviceexecution pointcut?

I have an aspect in my application that intercepts every advice execution on the system. I want to be able to identify which advice is being "intercepted" by my adviceexecution pointcut like this
//... some code in AdviceInspector.aj
before(): adviceexecution() && !within(AdviceInspector) {
System.out.println("advice execution being intercepted");
// TODO : get a way to know which advice execution has been intercepted
}
//... further code
Thanks in advance
You can get the Signature of the advice from the joinPoint. Signature has various methods to describe it. If it is just for debugging the toString() method describes it nicely
before(): adviceexecution() && !within(AdviceInspector) {
org.aspectj.lang.Signature sig = thisJoinPoint.getStaticPart().getSignature();
//It is also valid to do
//Signature sig = thisJoinPointStaticPart.getSignature();
System.out.println(sig);
}