I am using defrecord on a simple example, say:
(defrecord Person [fname lname address])
I am using a tool that requires a 0-params constructor, so trying to use the above on the JVM leads to:
CompilerException java.lang.RuntimeException:
Couldn't find 0-params constructor for class:
class user.Hello, compiling:(form-init5503453667732926891.clj:1:13)
What's the quickest/most beautiful way to get that 0-param constructor generated ?
You should think of this record as of this class:
class Person {
final Object fname;
final Object lname;
final Object address;
public Person(final Object fname, final Object lname, final Object address) {
this.fname = fname;
this.lname = lname;
this.address = address;
}
}
It is immutable, with all fields final etc.
Have a look at this flowchart for choosing the right Clojure type definition form.
Related
if i have a extension method that converts an Person object to a PersonDTO then
should i unit test that the conversion occurs correctly
if so and i use fluentassertions how do i assert that the conversion is correct
my extension method is:
public static PersonDTO ToDto (this Person person)
{
if (person == null) return null;
return new PersonDTO
{
FirstName = person.FirstName,
LastName = person.LastName,
Dob = person.Dob,
Identifier= person.Id
};
}
my person object has other properties that are not being mapped.
to get the personDTO object i would therefore do something similar to below:
var newPerson = new Person{ //set properties here };
var personDto = newPerson.ToDto();
First, yes, this code should be tested.
In order to check that the conversion is correct, you have to build the expected result manually, and then assert that the result of the function is the same:
var person = new Person{ ... };
var expectedPersonDto = new PersonDto{ ... };
person.ToDto().Should().BeEquivalentTo(expectedPersonDto);
See this for more information on comparing object graphs.
I am new to springbootneo4j. I have difficulties making general purpose queries. I want to be able to make any kind of query and get result without domain entity.
I am making a query like this in repository class:
#Query("MATCH (p:Employee) RETURN ID(p) as id, p.name as name, p.salary as salary ")
that is not working, but the following query is working:
#Query("MATCH (p:Employee) RETURN p ")
My domain entity class is something like this:
#NodeEntity
public class Employee {
#Id
#GeneratedValue
private Long id;
private String name;
private int salary;
#Relationship(type = "IS_BOSSOF", direction = Relationship.UNDIRECTED) Set<Employee> reporties = new HashSet<>();
public Employee() {}
// some more code
}
Create a command is like this:
(laksmi:Employee{name:"Laksmi",salary:200}),(ashwini:Employee{name:"AshwiniV",salary:300}), (harish:Employee{name:"Harish",salary:400}), (jay)-[:IS_BOSSOF]->(mukesh), (xyz)-[:IS_BOSSOF]->(mukesh), (harish)-[:IS_BOSSOF]->(ashwini),
Whenever you are distributing properties you need to use #QueryResult annotation on your class
SDN
Is it a good idea to override setters and getters of properties in domain class? Assume we have a domain class with name property and capitalizedName where we store clean up name:
class Person {
String name
String capitalizedName
String setName(String name){
this.name = name
this.searchName = name.replaceAll("[^A-Za-z0-9 ]", "").trim().toUpperCase()
}
}
If I override setter and in a unit test try to use dynamic finder:
Person.findByName('Whatever')
I got
java.lang.IllegalArgumentException: Property [name] is not a valid property of class [com.test.Person]
But in runtime it works pretty fine.
Can I modify getters and setters of a domain class? What is the best way to achieve behaviour as I described above?
A setter should have a return type of void
void setName(String name){
this.name = name
this.searchName = name.trim().replaceAll("[^A-Za-z0-9 ]", "").replaceAll(" +", " ").toUpperCase()
}
I'm working on an Android app. I created (in a separate .java file) an object like so:
class RRS_Location {
String tagname;
String href;
// Constructor
public RRS_Location(String tagname, String href) {
this.tagname = tagname;
this.href = href;
}
public String getTagname() {
return tagname;
}
public String getHref() {
return href;
}
}
Within an activity, I've declared a List of these items
List<RRS_Location> rrs_list;
I'm getting a NullPointerException when I try to add an RRS_Location object to the list. I'm doing so using this code
rrs_list.add(new RRS_Location(e1, e2));
I've used Toast to echo back to me that I have valid Strings e1 and e2. Any ideas on why I'm getting the exception? TIA!
Are you instantiating rrs_list before making the call to add?
List<RRS_Location> rrs_list = new ArrayList<RRS_Location>();
If not, this is why you are getting a NullPointerException, you are attempting to invoke a method on a null object.
My team is designing a library that is wrapping calls to Active Directory to search and return a list of people.
We have a person class that wraps up the information for a person found. We then use the List to wrap them up. When we call the search it uses the internal System.Directory library and returns a SearchResultCollection object. We then iterate through it to build up the list<> and return that.
We have designed the person class to only have read only (get) properties since we don't want the callee to change the person info. We pass in the SearchResult object from the System.Directory library on the constructor of the person.
My issue is we can't test this easily.
My thoughts so far have been:
Pass variables into the person constructor for each property needing to be set.
Unfortunately, this will make for a very long constructor parameter list.... Smells bad to me.
Allow the person class to have setters on the properties.
Again, smells bad to me since we can't control the callee from using this.
Refactor:
I have looked at the extract to interface and adapt parameter techniques. It seems the adapt parameter has the most promise? Adapt parameter seems nice because it does help break the dependency I have on the Directory Library's SearchResult object. So if in the future I wanted to do some other kind of search we are in good shape. At least I think we are?
Sub class the person object and create a test Person with setters....
Seems like it would work but not sure if it's the right way to go?
Mock it
Haven't done any mocking yet so again not sure on this one.
EDIT: If mocking is best idea please let me know... However, I would be interested to know how this would be done without mocking also (or perhaps it really isn't do able without mocking)....
I would appreciate guidance on this one.
Here's a snippet of the code:
public class PeopleSearcher
{
.... declarations left out....
public List<Person> FindPerson(string FirstName, string LastName, string Login)
{
...filter setup left out for brevity....
_peopleFound = _directoryToSearch.FindAll();
//Convert to list of persons....
int cnt = 0;
_listOfPeople = new List<Person>();
while (cnt < _peopleFound.Count)
{
Person p = new Person(_peopleFound[0]);
_listOfPeople.Add(p);
cnt++;
}
return _listOfPeople;
}
}
public class Person
{
private string sn;
....further declarations left out for brevity....
public Person(SearchResult PersonFound)
{
sn = PersonFound.Properties["sn"].Count == 0 ? string.Empty : PersonFound.Properties["sn"][0].ToString();
givenName = PersonFound.Properties["givenName"].Count == 0 ? string.Empty : PersonFound.Properties["givenName"][0].ToString();
sAMAccountName = PersonFound.Properties["sAMAccountName"].Count == 0 ? string.Empty : PersonFound.Properties["sAMAccountName"][0].ToString();
adsPath = PersonFound.Path == null ? string.Empty : PersonFound.Path;
}
public string LastName
{
get
{
return sn;
}
}
.... more getters...
}
}
"Mocking" is a word that is usually used for all kinds of test doubles. And most times people or not "mocking", they're faking or stubbing. Anyway, your 4th option (subclass and add setters) sounds to me like the easiest way to go given your codebase assuming you want Person objects to pass toother methods. Because I don't think you're talking about testing that the person object gets the properties set correct by the constructor, right?
Mock it. This is the sort of situation that mocking was invented for. I've only done mocking in Ruby, so I'm not sure of the state of the art for .net, but it should work well.
When mocking it you might realize some areas that should be refactored. This is also a good plan.
In your mock (by framework or otherwise), you're still going to end up having to create Person objects with values, which leads you right back to your original problem.
Fortunately, there are two excellent solutions:
1) Go ahead and add setters to the Person class, but make them protected. This means your mock and test code would have to be in the same package, but would block other users from mutating your Persons. (and we don't want mutants running around - there's been enough of that in the movies lately).
2) Use a Builder class (as Joshua Bloch describes in Effective Java). You'd create a public static PersonBuilder class inside Person, which would export a build method and chainable parameter specifiers (like setters, but not separately callable):
public class Person ....
public static class PersonBuilder {
public PersonBuilder (String firstName, String lastName) {...} // my sample has two required values
public Person build() { ... }
public PersonBuilder ssn (String value) { ... }
public PersonBuilder adsPath (String value) { ... }
...
}
...
}
The chainable value specifiers look like this:
public PersonBuilder ssn (String value) {
this.sn = value;
return this;
}
Then a call to create a Person looks like this:
Person thisPerson = new Person.PersonBuilder ("John", "Smith").ssn("123-45-6789").adsPath("whatever");
This method completely hides the methods which can set the values (indeed, you have no "setters"), but keeps you from having to deal with long constructor argument lists (which makes it easier to deal with optional values).
Incidentally, you also probably want to make Person's constructor private.