JHipster default test cases failing - unit-testing

I've created application using JHipster, it has default test cases which compares with its default values declared in variable.
Ex.
private static final String DEFAULT_NAME = "AAAAAAAAAA";
private static final String UPDATED_NAME = "BBBBBBBBBB";
Now, what I understand from console log is since application has data populated and when default test cases gets response, its actual application/custom data which does not matches with the expected data.
Ex.
without data population, DEFAULT_NAME(AAAAAAAAAA) matches with UPDATED_NAME(BBBBBBBBBB) but now since data is there, DEFAULT_NAME(AAAAAAAAAA) not matches with UPDATED_NAME(INDIA).

If you customized the logic in your application (such as the entity service or resource files), you will need to update the tests to test that logic. For example, you force the name to INDIA in the code, then update the tests to check that instead of BBBBBBBBBB

Related

Camunda Modeler Decision Table : Can we source the predefined values (InputValues) for a string input from an external endpoint?

I have an api endpoint which returns a set of values. I want to use these values as a source for predefined values for the input fields in the decision table.
As of now, I can only see the possibility of adding these values as static values in the Modeler. Checked the camunda documentation, could not find anything relevant to this requirement.
Any pointers would be helpful.
You have a few different options to pull values from an external source for use in a decision table. For the purposes of this response, I'll assume your external source is a REST API endpoint. Here are those options:
You could call the decision table from a process definition, and you could source the external values in the process definition prior to the call to the decision table. For example, you could use a Service Task configured as an 'http-connector' to pull the values.
You could expand your decision table such that it becomes a DRD (Decision Requirements Diagram) and utilize a Decision Literal Expression to pull that data.
Finally, you could embed your code to pull data from the external source within a Java bean that is known to the process engine in the current context and call that bean from within your decision table.
I know there's a lot there; if any of it sounds foreign, please review the Camunda documentation at https://docs.camunda.org.
Let me focus on #2 above for a moment and give you a specific example... If you chose that route, your Decision Literal Expression could have the following code within it:
//Get access to the Connectors and Spin Objects.
var Connectors = Java.type('org.camunda.connect.Connectors');
var Spin = Java.type('org.camunda.spin.Spin');
//Create an instance of the HTTP Connector and make the request.
var httpConnector = Connectors.http();
var resp = httpConnector.createRequest()
.post()
.url('http://localhost:1027/creditscore')
.contentType('application/json')
.payload('{"ssn":\"' + ssn + '\"}')
.execute()
.getResponse();
//Retrieve the credit score from the response.
var creditScore = Spin.JSON(resp).prop('creditScore').numberValue();
//Return the credit score, setting it to the variable name specified here.
creditScore;
In that example, I've set the variable name to "creditScore", the type of the variable to "long" and the expression language to "javascript". It requires one variable as input, with that being "ssn". You would be able to use that variable "creditScore" in any decision tables that depend on that decision literal expression in your DRD.

Database unit test

I am hoping to get some advice on a unit test I am writing for to test some db entries.
The function I am testing seeds the database if no records are found.
func Seed(db *gorm.DB) {
var data []Data
db.Find(&data)
if len(data) == 0 {
// do seed default data
}
}
What I can't quite seem to get going is the test for that if len test. I am using a test db so I can nuke it whenever so it is not an issue if I just need to force an empty DB on the function.
The function itself works and I just want to make sure I get that covered.
Any advice would be great.
Thanks!
It really depends, there are so many ways of addressing this based on your risk level and the amount of time you want to invest to mitigate those risks.
You could write a unit test that asserts your able to detect and act on users logic (ie seeding when empty and ignoring when full) without any database.
If you would like to test the logic as well as your programs ability to speak to mysql correctly through the gorm library you could:
Have a test where you call Seed with no users in the DB, after calling it your test could select from Users and make sure there are the expected entries created from len(users) == 0 conditional
Have a test where the test creates a single entry and calls Seed, after which asserting that underlying tables are empty.
It can get more complicated. If Seed is selecting a subset of data than your test could insert 2 users, one that qualifies and one that doesnt', and make sure that no new users are Seeded.

Can I write unit test in case exceptions are handled in Boundary classes?

So, I'm writing unit test for a function that add a new Patient record to db. Let's say I have some test cases: Add patient successfully, add patient with inputted age that is not numberic, add patient with only age inputted. I've done handling the blank textfields and non-numberic age in my boundary class, which means the errors are prevented right in the boundary class, and the adding function in patientDAO class always insert a valid new patient into db. So my question is, do I have to write unit test to test the last two test cases, and if I do, how can I do it? Because my adding function doesn't do the handling job.
I'm writing unit test for a function that add a new Patient record to db
Simple - your function responsibility is to save given patient data to the database.
You can write tests which check that given data successfully saved into database.
But, because your tests will save it to the real database, that mean it is not unit test anymore - you can call it integration tests.
Because responsibility of your function is only saving provided data, you don't need to write tests for input values validation. Your function will save anything you will provide as an argument.

Laravel 5/Faker - Factory data changes

Been using Faker in combination with sqLite in-memory database for testing in Laravel lately and I have this strange issue where my model has a factory and everything except the first variable (which happens to also be the primary key of the table) gets set correctly.
Let's explain further, the variable I'm pointing at uses the following rule in the factory:
'kvk' => strval($faker->randomNumber(9)),
So it should become a string containing a 9 digit number. Now the next step is calling the factory from my controller test and I also have another User model which uses this 'kvk' variable from my company as a foreign key reference:
$this->_company = factory(Company::class)->create([ 'address_id' => $this->_address->id ]);
$this->_user = factory(User::class)->create([ 'kvk' => $this->_company->kvk ]);
But when I put an echo $this->_company->kvk; in between, it shows me that 'kvk is set to 1, which should not be possible because of the rule I put in my factory.
Finally the user is used to mock the session in the test and is also used to check wether I should have the rights to edit an address using the following check:
$user = Auth::user();
$company = Company::where('kvk', $user->kvk)->first();
$address = Address::whereId($id)->first();
if($company->address_id != $address->id)
{
return abort(403);
}
So first I get the current logged in user (which is mocked to the created user above, and this works perfectly. Next I get the company associated with this user (which should be the one created above, since I used the company->kvk as a foreign key reference), however when I output the saved Company to my log I see that the kvk is set to a 9 digit string like it's supposed to.
I really can't put my finger on why at first the kvk is set to 1 and afterwards in my test it seems perfectly fine the way it should be, but my test fails because the wrong reference is set in my User so it can't find the right Company. Do you have any idea what could be the reason for this? I've also tried setting the 'kvk' explicitly while creating with the factory, but this also does not work and will stil output 1.
After diving into the source of Laravel, I found out it had to with my models. While I did set the primary key attribute in my models, I forgot to set the auto increment boolean to false. This caused my models to cast the primary key to an auto increment integer every time it was saved to the database. This solved the issue.

BIRT - using multiple webservices to get the data

I am trying to generate a report using Eclipse BIRT report designer.
The scenario is this:
There are 2 web service data sources. There are 2 datasets for webservices 'WS1' and 'WS2' respectively.
The output element 'COUNTRYID' of one webservice 'WS1' would go as input for another webservice 'WS2'.
What I did:
Created a parameter COUNTRYID.
Created a dummy Computed Column in the dataset of the web service 'WS1' with the expression:
params["COUNTRYID"].value=row["COUNTRYID"]
Now the input parameters for the 'WS2' dataset is related to the global paramter 'COUNTRYID'.
When I run the report, I see that the global parameter contains the value from the 'WS1' output.
But the report does not display the values from the response of the web service 'WS2'
My questions:
How can I see, if the webservice got fired or not?
How can I see, if the webservice got fired with correct values ?
WS1 is not fired unless it is explicitely bound to a report element. Typically, to achieve this we apply following steps:
insert a data element at the beginning of the report body
turn the property visibility of this new element to false (or let it visible during testing)
bind it to the first dataset WS1
It will force a silent execution of WS1, and therefore this will populate your parameter COUNTRYID before WS2 runs.
However this approach would not work if:
WS2 dataset has to be used to populate selection items of a report parameter (which does not seem to be the case here)
If COUNTRYID parameter is used at render time. This point is much more annoying, if you need this parameter in chart expressions for example. If so, i would recommend to store WS1 in a report variable instead of (or why not in addition to) a report parameter. See this topic to see how to create a report variable.
You can initialize it at the same place you did for the report parameter with:
vars["COUNTRYID"]=row["COUNTRYID"];
and use it anywhere with
vars["COUNTRYID"];
Report variables are available from the palette of expressions editor :