Friday, November 16, 2018
Wednesday, October 3, 2018
Unit testing using TestRestRemplate spring boot
Sample code to use TestRestTemplate in JUnit Test.
package com.example.rest;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.CouchBaseApplication;
import com.example.model.Student;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CouchBaseApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ViewControllerTest {
@LocalServerPort
private int port;
private TestRestTemplate testRestTemplate = new TestRestTemplate();
List<Student> students = new ArrayList<>();
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
@Before
public void setup() {
//MockitoAnnotations.initMocks(this);
Student student = new Student("101", "Sapthika", 2, null);
students.add(student);
Student student2 = new Student("101", "Sapthika", 2, null);
students.add(student2);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
@Test
public void testGetSecondHighestAge() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Integer> response = testRestTemplate.exchange(
createURLWithPort("/agetest"),
HttpMethod.GET, entity, Integer.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+response.getBody());
assertEquals(Integer.valueOf(44), response.getBody());
}
@Test
public void testGetCount() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Long> response = testRestTemplate.exchange(
createURLWithPort("/count"),
HttpMethod.GET, entity, Long.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Count value : "+response.getBody());
assertEquals(Long.valueOf(5), response.getBody());
}
@Test
public void testGetByCourseName() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/name/{courseName}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"Java");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
assertEquals(2, response.getBody().size());
}
@Test
public void testGetByCourseId() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/id/{courseId}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"100");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
//assertEquals(2, response.getBody().size());
assertEquals("Asserting reponse body size", 2, response.getBody().size());
}
@Test
public void testRetrieveStudentCourse() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<Student> entity = new HttpEntity<>(new Student("101", "rja", 5, null));
ResponseEntity<String> response = testRestTemplate.exchange(
createURLWithPort("/test"),
HttpMethod.GET, entity, String.class);
Student student = new Student("101", "Sapthika", 2, null);
ResponseEntity<Student> response1 = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.POST, entity, Student.class);
HttpEntity<String> entity1 = new HttpEntity<String>("42", headers);
ResponseEntity<Student> response2 = testRestTemplate.exchange(
createURLWithPort("/students/{id}"),
HttpMethod.PUT,entity1,Student.class,"42");
String expected = "{id:Course1,name:Spring,description:10 Steps}";
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("Response from API", response.getBody());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
@Test
public void testPutMethod() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "John", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
}
@Test
public void testPutMethod1() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "xxxx", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
public void testAddNew() throws JSONException {
ResponseEntity<Iterable<Student>> response = testRestTemplate.exchange(
createURLWithPort("/get/{name}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<Iterable<Student>>(){},"John");
System.out.println("Body : "+response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
List<Student> studentList = new ArrayList<>();
response.getBody().forEach(studentList::add);
assertEquals(5,studentList.size());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
}
package com.example.rest;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.CouchBaseApplication;
import com.example.model.Student;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CouchBaseApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ViewControllerTest {
@LocalServerPort
private int port;
private TestRestTemplate testRestTemplate = new TestRestTemplate();
List<Student> students = new ArrayList<>();
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
@Before
public void setup() {
//MockitoAnnotations.initMocks(this);
Student student = new Student("101", "Sapthika", 2, null);
students.add(student);
Student student2 = new Student("101", "Sapthika", 2, null);
students.add(student2);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
@Test
public void testGetSecondHighestAge() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Integer> response = testRestTemplate.exchange(
createURLWithPort("/agetest"),
HttpMethod.GET, entity, Integer.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+response.getBody());
assertEquals(Integer.valueOf(44), response.getBody());
}
@Test
public void testGetCount() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Long> response = testRestTemplate.exchange(
createURLWithPort("/count"),
HttpMethod.GET, entity, Long.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Count value : "+response.getBody());
assertEquals(Long.valueOf(5), response.getBody());
}
@Test
public void testGetByCourseName() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/name/{courseName}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"Java");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
assertEquals(2, response.getBody().size());
}
@Test
public void testGetByCourseId() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/id/{courseId}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"100");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
//assertEquals(2, response.getBody().size());
assertEquals("Asserting reponse body size", 2, response.getBody().size());
}
@Test
public void testRetrieveStudentCourse() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<Student> entity = new HttpEntity<>(new Student("101", "rja", 5, null));
ResponseEntity<String> response = testRestTemplate.exchange(
createURLWithPort("/test"),
HttpMethod.GET, entity, String.class);
Student student = new Student("101", "Sapthika", 2, null);
ResponseEntity<Student> response1 = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.POST, entity, Student.class);
HttpEntity<String> entity1 = new HttpEntity<String>("42", headers);
ResponseEntity<Student> response2 = testRestTemplate.exchange(
createURLWithPort("/students/{id}"),
HttpMethod.PUT,entity1,Student.class,"42");
String expected = "{id:Course1,name:Spring,description:10 Steps}";
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("Response from API", response.getBody());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
@Test
public void testPutMethod() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "John", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
}
@Test
public void testPutMethod1() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "xxxx", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
public void testAddNew() throws JSONException {
ResponseEntity<Iterable<Student>> response = testRestTemplate.exchange(
createURLWithPort("/get/{name}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<Iterable<Student>>(){},"John");
System.out.println("Body : "+response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
List<Student> studentList = new ArrayList<>();
response.getBody().forEach(studentList::add);
assertEquals(5,studentList.size());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
}
Tuesday, September 4, 2018
Importance of equals and hashcode method override
Importance of equals and hashcode method override:
Both equals and hashcode method present in java.lang.Object, hence these methods available for all java classes.
When comparing the objects, these methods are used.
Default implentation:
equals -> checks two objects are pointing same memory location (== check)
hashcode -> returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won't stay the same.
Note :
"If two objects are equal according to the equals(Object) method, then calling the hashcode() method on each of the two objects must produce the same integer result."
Example:
We will define Student class with and without overriding equals & hashcode.
Student.java
public class Student {
private int id;
private String name;
public Student(int id, String name) {
Student1.java -> Here we are override equals and hashcode methods.
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
@Override
public boolean equals(Object o) { // 3 if's and final comparison
if (o == null ) {
return false;
}
if(!(o instanceof Student1)) {
return false;
}
if(o == this) {
return true;
}
Student1 student = (Student1)o;
return this.getId() == student.getId() && this.getName() == student.getName();
}
Test:
package com.raj.basics;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import com.raj.model.Student;
import com.raj.model.Student1;
public class EqualsHashCodeOverride {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(); // List will allow duplication and maintains order.
List<Student1> student1s = new ArrayList<>();
Student student1 = new Student(1,"Raj kumar");
Student student2 = new Student(1,"Raj kumar");
Student1 student11 = new Student1(1,"Raj kumar");
Student1 student12 = new Student1(1,"Raj kumar");
students.add(student1);students.add(student2);
student1s.add(student11);student1s.add(student12);
System.out.println("Students size : "+ students.size()); //2
System.out.println("Student1s size : "+ student1s.size()); //2
System.out.println("Students check : "+ students.contains(new Student(1,"Raj kumar"))); //false// Object's equals method considered for comparison(just memory location check)
System.out.println("Student1s check : "+ student1s.contains(new Student1(1,"Raj kumar"))); //true// Student1's equal method considered for comparison (deep check - logic done by us)
HashSet <Student> studentSet = new HashSet<>(); // HashSet wont allow duplicates.It will use hashcode for storing elements. Hashcode will vary for objects.
HashSet <Student1> student1Set = new HashSet<>();
studentSet.add(student1);studentSet.add(student2);
student1Set.add(student11);student1Set.add(student12);
System.out.println("Students set size : "+ studentSet.size()); // 2 because Object's hashcode taken for comparison
System.out.println("Student1s set size : "+ student1Set.size()); // 1 because Student1's equal method taken for consideraton.
System.out.println("HashSet contains = " + studentSet.contains(new Student(1, "Raj kumar"))); // false
System.out.println("HashSet contains = " + student1Set.contains(new Student1(1, "Raj kumar"))); // true
}
}
Output:
Students size : 2
Student1s size : 2
Students check : false
Student1s check : true
Students set size : 2
Student1s set size : 1
HashSet contains = false
HashSet contains = true
Both equals and hashcode method present in java.lang.Object, hence these methods available for all java classes.
When comparing the objects, these methods are used.
Default implentation:
equals -> checks two objects are pointing same memory location (== check)
hashcode -> returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won't stay the same.
Note :
"If two objects are equal according to the equals(Object) method, then calling the hashcode() method on each of the two objects must produce the same integer result."
Example:
We will define Student class with and without overriding equals & hashcode.
Student.java
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
}
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
@Override
public boolean equals(Object o) { // 3 if's and final comparison
if (o == null ) {
return false;
}
if(!(o instanceof Student1)) {
return false;
}
if(o == this) {
return true;
}
Student1 student = (Student1)o;
return this.getId() == student.getId() && this.getName() == student.getName();
}
}
}Test:
package com.raj.basics;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import com.raj.model.Student;
import com.raj.model.Student1;
public class EqualsHashCodeOverride {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(); // List will allow duplication and maintains order.
List<Student1> student1s = new ArrayList<>();
Student student1 = new Student(1,"Raj kumar");
Student student2 = new Student(1,"Raj kumar");
Student1 student11 = new Student1(1,"Raj kumar");
Student1 student12 = new Student1(1,"Raj kumar");
students.add(student1);students.add(student2);
student1s.add(student11);student1s.add(student12);
System.out.println("Students size : "+ students.size()); //2
System.out.println("Student1s size : "+ student1s.size()); //2
System.out.println("Students check : "+ students.contains(new Student(1,"Raj kumar"))); //false// Object's equals method considered for comparison(just memory location check)
System.out.println("Student1s check : "+ student1s.contains(new Student1(1,"Raj kumar"))); //true// Student1's equal method considered for comparison (deep check - logic done by us)
HashSet <Student> studentSet = new HashSet<>(); // HashSet wont allow duplicates.It will use hashcode for storing elements. Hashcode will vary for objects.
HashSet <Student1> student1Set = new HashSet<>();
studentSet.add(student1);studentSet.add(student2);
student1Set.add(student11);student1Set.add(student12);
System.out.println("Students set size : "+ studentSet.size()); // 2 because Object's hashcode taken for comparison
System.out.println("Student1s set size : "+ student1Set.size()); // 1 because Student1's equal method taken for consideraton.
System.out.println("HashSet contains = " + studentSet.contains(new Student(1, "Raj kumar"))); // false
System.out.println("HashSet contains = " + student1Set.contains(new Student1(1, "Raj kumar"))); // true
}
}
Students size : 2
Student1s size : 2
Students check : false
Student1s check : true
Students set size : 2
Student1s set size : 1
HashSet contains = false
HashSet contains = true
Thursday, June 14, 2018
Automatic Switch Between LAN & Wifi in Windows 10
Steps to switch between LAN and Wifi Automatically.
1.
Go to Control Panel -> Network & Sharing
Centre -> Change Adapter Settings
2.
Git ‘Alt’ button to get Menu items
3.
Click Advanced -> Advanced Settings
4.
Change the order like below.
Note : First preference will be LAN if
connected.
Wednesday, June 13, 2018
First Non Repeating Character Using Java 8
Hi Friends,
In this post, we will see the program for FirstNonRepeatingCharacter in java.
package com.raj.programs;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FirstNonRepeatingCharacter {
static Character getFirstNonRepeatingCharacter(String input) {
Map<Character, Integer> table = new LinkedHashMap<>(input.length());
input.chars() // IntStream
.mapToObj(c -> (char) c) // converting into char
.forEach(ch -> table.put(ch, table.containsKey(ch) ? table.get(ch) + 1 : 1)); // putting in map
return table.entrySet().stream() // getting stream from map entries
.filter(entry -> entry.getValue() == 1) // getting only the entries which presented only one time
.map(Entry::getKey) // getting the entry keys alone
.findFirst() // getting first element in the key list
.orElse(null); // If none found, null will be returned
}
public static void main(String[] args) {
System.out.println("FirstNonRepeatingCharacter :"+FirstNonRepeatingCharacter.getFirstNonRepeatingCharacter("ABCABD"));
}
}
In this post, we will see the program for FirstNonRepeatingCharacter in java.
package com.raj.programs;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FirstNonRepeatingCharacter {
static Character getFirstNonRepeatingCharacter(String input) {
Map<Character, Integer> table = new LinkedHashMap<>(input.length());
input.chars() // IntStream
.mapToObj(c -> (char) c) // converting into char
.forEach(ch -> table.put(ch, table.containsKey(ch) ? table.get(ch) + 1 : 1)); // putting in map
return table.entrySet().stream() // getting stream from map entries
.filter(entry -> entry.getValue() == 1) // getting only the entries which presented only one time
.map(Entry::getKey) // getting the entry keys alone
.findFirst() // getting first element in the key list
.orElse(null); // If none found, null will be returned
}
public static void main(String[] args) {
System.out.println("FirstNonRepeatingCharacter :"+FirstNonRepeatingCharacter.getFirstNonRepeatingCharacter("ABCABD"));
}
}
Output:
FirstNonRepeatingCharacter :C
First Recurring Character Java Program
Hi Folks,
Below the source code for FirstRecurringCharacter in java.
package com.raj.programs;
import java.util.Hashtable;
public class FirstRecurringCharacter {
public static String firstRecurring(String inputString) {
Hashtable<String, String> table = new Hashtable<>(inputString.length());
for(char c : inputString.toCharArray()) {
if(table.containsKey(String.valueOf(c))){
System.out.println("First Recurring character is : "+c);
return String.valueOf(c);
}else {
table.put(String.valueOf(c), String.valueOf(c));
}
}
System.out.println("Not found");
return null;
}
public static void main(String[] args) {
FirstRecurringCharacter.firstRecurring("ABCDEFA");
FirstRecurringCharacter.firstRecurring("ABCDEF");
}
}
Below the source code for FirstRecurringCharacter in java.
package com.raj.programs;
import java.util.Hashtable;
public class FirstRecurringCharacter {
public static String firstRecurring(String inputString) {
Hashtable<String, String> table = new Hashtable<>(inputString.length());
for(char c : inputString.toCharArray()) {
if(table.containsKey(String.valueOf(c))){
System.out.println("First Recurring character is : "+c);
return String.valueOf(c);
}else {
table.put(String.valueOf(c), String.valueOf(c));
}
}
System.out.println("Not found");
return null;
}
public static void main(String[] args) {
FirstRecurringCharacter.firstRecurring("ABCDEFA");
FirstRecurringCharacter.firstRecurring("ABCDEF");
}
}
Monday, May 7, 2018
json editor - reorder
https://github.com/josdejong/jsoneditor/
https://jsoneditoronline.org/
https://codebeautify.org
https://jsoneditoronline.org/
https://codebeautify.org
Java - How to get all the field names from JSON Schema
public void getKey(String response) {
List<String> keyList = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject schema = jsonObject.getJSONObject("schema");
JSONObject properties = schema.getJSONObject("properties");
Iterator iterator = properties.keys();
while (iterator.hasNext()) {
String key = iterator.next().toString();
keyList.add(key);
}
String[] arr = (String[]) keyList.toArray(new String[keyList.size()]);
} catch (JSONException e) {
e.printStackTrace();
}
}
Replace newline character in notepad ++
Then select Search → Replace from the menu, or hit Ctrl-H. The Replace dialog will show up. Therein make sure that you fill out the following values; Find what: \\n (yes, two back-slashes), Replace with: \n (yes, just one back-slash), and the most important, make sure you select the Extended (\n, \r, \t, \0, \x, …) radio button.
Sunday, May 6, 2018
Handlebar
{{def 'fifteenYear' (math birthYear '+' 15)}}
{{#ifb (or
(and
(compare (math fifteenYear '%' 4) '==' 0)
(compare (math fifteenYear '%' 100) '!=' 0)
)
(compare (math fifteenYear '%' 400) '==' 0)
)
}}
Your fifteenth anniversary was in a leap year!
{{else}}
Your fifteenth anniversary was in a non-leap year!
{{/ifb}}
https://github.com/beryx/handlebars-java-helpers
birthYear: 1997
http://handlebars-java-helpers.beryx.org/releases/latest/
Tuesday, May 1, 2018
RSql Queries
's0==a0;s1==a1;s2==a2' | and(eq('s0','a0'), eq('s1','a1'), eq('s2','a2'))
's0==a0,s1=out=(a10,a11),s2==a2' | or(eq('s0','a0'), out('s1','a10', 'a11'), eq('s2','a2'))
's0==a0,s1==a1;s2==a2,s3==a3' | or(eq('s0','a0'), and(eq('s1','a1'), eq('s2','a2')), eq('s3','a3'))
'(s0==a0,s1==a1);s2==a2' | and(or(eq('s0','a0'), eq('s1','a1')), eq('s2','a2'))
'(s0==a0,s1=out=(a10,a11));s2==a2,s3==a3'| or(and(or(eq('s0','a0'), out('s1','a10', 'a11')), eq('s2','a2')), eq('s3','a3'))
'((s0==a0,s1==a1);s2==a2,s3==a3);s4==a4' | and(or(and(or(eq('s0','a0'), eq('s1','a1')), eq('s2','a2')), eq('s3','a3')), eq('s4','a4'))
'(s0==a0)' | eq('s0', 'a0')
'((s0==a0));s1==a1' | and(eq('s0', 'a0'), eq('s1','a1'))
's0==a0,s1=out=(a10,a11),s2==a2' | or(eq('s0','a0'), out('s1','a10', 'a11'), eq('s2','a2'))
's0==a0,s1==a1;s2==a2,s3==a3' | or(eq('s0','a0'), and(eq('s1','a1'), eq('s2','a2')), eq('s3','a3'))
'(s0==a0,s1==a1);s2==a2' | and(or(eq('s0','a0'), eq('s1','a1')), eq('s2','a2'))
'(s0==a0,s1=out=(a10,a11));s2==a2,s3==a3'| or(and(or(eq('s0','a0'), out('s1','a10', 'a11')), eq('s2','a2')), eq('s3','a3'))
'((s0==a0,s1==a1);s2==a2,s3==a3);s4==a4' | and(or(and(or(eq('s0','a0'), eq('s1','a1')), eq('s2','a2')), eq('s3','a3')), eq('s4','a4'))
'(s0==a0)' | eq('s0', 'a0')
'((s0==a0));s1==a1' | and(eq('s0', 'a0'), eq('s1','a1'))
Monday, April 30, 2018
RSQL / FIQL parser
//https://github.com/jirutka/rsql-parser
RSQL is a query language for parametrized filtering of entries in RESTful APIs. It’s based on FIQL (Feed Item Query Language) – an URI-friendly syntax for expressing filters across the entries in an Atom Feed. FIQL is great for use in URI; there are no unsafe characters, so URL encoding is not required. On the other side, FIQL’s syntax is not very intuitive and URL encoding isn’t always that big deal, so RSQL also provides a friendlier syntax for logical operators and some of the comparison operators.
For example, you can query your resource like this: /movies?query=name=="Kill Bill";year=gt=2003 or /movies?query=director.lastName==Nolan and year)=2000. See examples below.
This is a complete and thoroughly tested parser for RSQL written in JavaCC and Java. Since RSQL is a superset of the FIQL, it can be used for parsing FIQL as well.
Related libraries
RSQL-parser can be used with:
rsql-jpa to convert RSQL into JPA2 CriteriaQuery,
rsql-mongodb to convert RSQL into MongoDB query using Spring Data MongoDB,
q-builders to build (not only) RSQL query in type-safe manner,
your own library…
It’s very easy to write a converter for RSQL using its AST. Take a look at very simple and naive converter to JPA2 in less than 100 lines of code here. You may also read a blog article about RSQL by Eugen Paraschiv.
Grammar and semantic
The following grammar specification is written in EBNF notation (ISO 14977).
RSQL expression is composed of one or more comparisons, related to each other with logical operators:
Logical AND : ; or and
Logical OR : , or or
By default, the AND operator takes precedence (i.e. it’s evaluated before any OR operators are). However, a parenthesized expression can be used to change the precedence, yielding whatever the contained expression yields.
input = or, EOF;
or = and, { "," , and };
and = constraint, { ";" , constraint };
constraint = ( group | comparison );
group = "(", or, ")";
Comparison is composed of a selector, an operator and an argument.
comparison = selector, comparison-op, arguments;
Selector identifies a field (or attribute, element, …) of the resource representation to filter by. It can be any non empty Unicode string that doesn’t contain reserved characters (see below) or a white space. The specific syntax of the selector is not enforced by this parser.
selector = unreserved-str;
Comparison operators are in FIQL notation and some of them has an alternative syntax as well:
Equal to : ==
Not equal to : !=
Less than : =lt= or (
Less than or equal to : =le= or ⇐
Greater than operator : =gt= or )
Greater than or equal to : =ge= or )=
In : =in=
Not in : =out=
You can also simply extend this parser with your own operators (see the next section).
comparison-op = comp-fiql | comp-alt;
comp-fiql = ( ( "=", { ALPHA } ) | "!" ), "=";
comp-alt = ( ")" | "(" ), [ "=" ];
Argument can be a single value, or multiple values in parenthesis separated by comma. Value that doesn’t contain any reserved character or a white space can be unquoted, other arguments must be enclosed in single or double quotes.
arguments = ( "(", value, { "," , value }, ")" ) | value;
value = unreserved-str | double-quoted | single-quoted;
unreserved-str = unreserved, { unreserved }
single-quoted = "'", { ( escaped | all-chars - ( "'" | "\" ) ) }, "'";
double-quoted = '"', { ( escaped | all-chars - ( '"' | "\" ) ) }, '"';
reserved = '"' | "'" | "(" | ")" | ";" | "," | "=" | "!" | "~" | "(" | ")";
unreserved = all-chars - reserved - " ";
escaped = "\", all-chars;
all-chars = ? all unicode characters ?;
If you need to use both single and double quotes inside a quoted argument, then you must escape one of them using \ (backslash). If you want to use \ literally, then double it as \\. Backslash has a special meaning only inside a quoted argument, not in unquoted argument.
Examples
Examples of RSQL expressions in both FIQL-like and alternative notation:
- name=="Kill Bill";year=gt=2003
- name=="Kill Bill" and year)2003
- genres=in=(sci-fi,action);(director=='Christopher Nolan',actor==*Bale);year=ge=2000
- genres=in=(sci-fi,action) and (director=='Christopher Nolan' or actor==*Bale) and year)=2000
- director.lastName==Nolan;year=ge=2000;year=lt=2010
- director.lastName==Nolan and year)=2000 and year(2010
- genres=in=(sci-fi,action);genres=out=(romance,animated,horror),director==Que*Tarantino
- genres=in=(sci-fi,action) and genres=out=(romance,animated,horror) or director==Que*Tarantino
How to use
Nodes are visitable, so to traverse the parsed AST (and convert it to SQL query maybe), you can implement the provided RSQLVisitor interface or simplified NoArgRSQLVisitorAdapter.
Node rootNode = new RSQLParser().parse("name==RSQL;version=ge=2.0");
rootNode.accept(yourShinyVisitor);
How to add custom operators
Need more operators? The parser can be simply enhanced by custom FIQL-like comparison operators, so you can add your own.
Set(ComparisonOperator) operators = RSQLOperators.defaultOperators();
operators.add(new ComparisonOperator("=all=", true));
Node rootNode = new RSQLParser(operators).parse("genres=all=('thriller','sci-fi')");
Maven
Released versions are available in The Central Repository. Just add this artifact to your project:
(dependency)
(groupId)cz.jirutka.rsql(/groupId)
(artifactId)rsql-parser(/artifactId)
(version)2.1.0(/version)
(/dependency)
However if you want to use the last snapshot version, you have to add the JFrog OSS repository:
(repository)
(id)jfrog-oss-snapshot-local(/id)
(name)JFrog OSS repository for snapshots(/name)
(url)https://oss.jfrog.org/oss-snapshot-local(/url)
(snapshots)
(enabled)true(/enabled)
(/snapshots)
(/repository)
RSQL is a query language for parametrized filtering of entries in RESTful APIs. It’s based on FIQL (Feed Item Query Language) – an URI-friendly syntax for expressing filters across the entries in an Atom Feed. FIQL is great for use in URI; there are no unsafe characters, so URL encoding is not required. On the other side, FIQL’s syntax is not very intuitive and URL encoding isn’t always that big deal, so RSQL also provides a friendlier syntax for logical operators and some of the comparison operators.
For example, you can query your resource like this: /movies?query=name=="Kill Bill";year=gt=2003 or /movies?query=director.lastName==Nolan and year)=2000. See examples below.
This is a complete and thoroughly tested parser for RSQL written in JavaCC and Java. Since RSQL is a superset of the FIQL, it can be used for parsing FIQL as well.
Related libraries
RSQL-parser can be used with:
rsql-jpa to convert RSQL into JPA2 CriteriaQuery,
rsql-mongodb to convert RSQL into MongoDB query using Spring Data MongoDB,
q-builders to build (not only) RSQL query in type-safe manner,
your own library…
It’s very easy to write a converter for RSQL using its AST. Take a look at very simple and naive converter to JPA2 in less than 100 lines of code here. You may also read a blog article about RSQL by Eugen Paraschiv.
Grammar and semantic
The following grammar specification is written in EBNF notation (ISO 14977).
RSQL expression is composed of one or more comparisons, related to each other with logical operators:
Logical AND : ; or and
Logical OR : , or or
By default, the AND operator takes precedence (i.e. it’s evaluated before any OR operators are). However, a parenthesized expression can be used to change the precedence, yielding whatever the contained expression yields.
input = or, EOF;
or = and, { "," , and };
and = constraint, { ";" , constraint };
constraint = ( group | comparison );
group = "(", or, ")";
Comparison is composed of a selector, an operator and an argument.
comparison = selector, comparison-op, arguments;
Selector identifies a field (or attribute, element, …) of the resource representation to filter by. It can be any non empty Unicode string that doesn’t contain reserved characters (see below) or a white space. The specific syntax of the selector is not enforced by this parser.
selector = unreserved-str;
Comparison operators are in FIQL notation and some of them has an alternative syntax as well:
Equal to : ==
Not equal to : !=
Less than : =lt= or (
Less than or equal to : =le= or ⇐
Greater than operator : =gt= or )
Greater than or equal to : =ge= or )=
In : =in=
Not in : =out=
You can also simply extend this parser with your own operators (see the next section).
comparison-op = comp-fiql | comp-alt;
comp-fiql = ( ( "=", { ALPHA } ) | "!" ), "=";
comp-alt = ( ")" | "(" ), [ "=" ];
Argument can be a single value, or multiple values in parenthesis separated by comma. Value that doesn’t contain any reserved character or a white space can be unquoted, other arguments must be enclosed in single or double quotes.
arguments = ( "(", value, { "," , value }, ")" ) | value;
value = unreserved-str | double-quoted | single-quoted;
unreserved-str = unreserved, { unreserved }
single-quoted = "'", { ( escaped | all-chars - ( "'" | "\" ) ) }, "'";
double-quoted = '"', { ( escaped | all-chars - ( '"' | "\" ) ) }, '"';
reserved = '"' | "'" | "(" | ")" | ";" | "," | "=" | "!" | "~" | "(" | ")";
unreserved = all-chars - reserved - " ";
escaped = "\", all-chars;
all-chars = ? all unicode characters ?;
If you need to use both single and double quotes inside a quoted argument, then you must escape one of them using \ (backslash). If you want to use \ literally, then double it as \\. Backslash has a special meaning only inside a quoted argument, not in unquoted argument.
Examples
Examples of RSQL expressions in both FIQL-like and alternative notation:
- name=="Kill Bill";year=gt=2003
- name=="Kill Bill" and year)2003
- genres=in=(sci-fi,action);(director=='Christopher Nolan',actor==*Bale);year=ge=2000
- genres=in=(sci-fi,action) and (director=='Christopher Nolan' or actor==*Bale) and year)=2000
- director.lastName==Nolan;year=ge=2000;year=lt=2010
- director.lastName==Nolan and year)=2000 and year(2010
- genres=in=(sci-fi,action);genres=out=(romance,animated,horror),director==Que*Tarantino
- genres=in=(sci-fi,action) and genres=out=(romance,animated,horror) or director==Que*Tarantino
How to use
Nodes are visitable, so to traverse the parsed AST (and convert it to SQL query maybe), you can implement the provided RSQLVisitor interface or simplified NoArgRSQLVisitorAdapter.
Node rootNode = new RSQLParser().parse("name==RSQL;version=ge=2.0");
rootNode.accept(yourShinyVisitor);
How to add custom operators
Need more operators? The parser can be simply enhanced by custom FIQL-like comparison operators, so you can add your own.
Set(ComparisonOperator) operators = RSQLOperators.defaultOperators();
operators.add(new ComparisonOperator("=all=", true));
Node rootNode = new RSQLParser(operators).parse("genres=all=('thriller','sci-fi')");
Maven
Released versions are available in The Central Repository. Just add this artifact to your project:
(dependency)
(groupId)cz.jirutka.rsql(/groupId)
(artifactId)rsql-parser(/artifactId)
(version)2.1.0(/version)
(/dependency)
However if you want to use the last snapshot version, you have to add the JFrog OSS repository:
(repository)
(id)jfrog-oss-snapshot-local(/id)
(name)JFrog OSS repository for snapshots(/name)
(url)https://oss.jfrog.org/oss-snapshot-local(/url)
(snapshots)
(enabled)true(/enabled)
(/snapshots)
(/repository)
JSON Schena Validation FGE
(dependency)
(groupId)com.github.java-json-tools(/groupId)
(artifactId)json-schema-validator(/artifactId)
(version)2.2.8(/version)
(/dependency)
ObjectMapper objectMapper = new ObjectMapper();
// this line will generate JSON schema from your class
JsonNode schemaNode = objectMapper.generateJsonSchema(StageDetail.class).getSchemaNode();
// make your JSON to JsonNode
JsonNode jsonToValidate = JsonLoader.fromString(JSON_TO_VALIDATE);
// validate it against the schema
ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(jsonToValidate);
// validate.messages contains error massages
System.out.println("Valid? " + validate.isSuccess());
https://github.com/java-json-tools/json-schema-validator
(groupId)com.github.java-json-tools(/groupId)
(artifactId)json-schema-validator(/artifactId)
(version)2.2.8(/version)
(/dependency)
ObjectMapper objectMapper = new ObjectMapper();
// this line will generate JSON schema from your class
JsonNode schemaNode = objectMapper.generateJsonSchema(StageDetail.class).getSchemaNode();
// make your JSON to JsonNode
JsonNode jsonToValidate = JsonLoader.fromString(JSON_TO_VALIDATE);
// validate it against the schema
ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(jsonToValidate);
// validate.messages contains error massages
System.out.println("Valid? " + validate.isSuccess());
https://github.com/java-json-tools/json-schema-validator
Monday, April 23, 2018
Mongo Import and Export
records = [];
var cursor = db.getCollection('foo').find({}, {});
while(cursor.hasNext()) {
records.push(cursor.next())
}
print(tojson(records));
mongoimport --db bala --collection book1 --type json --file C:\Users\balaji\Desktop\test1.json --jsonArray
mongo
is the command-line shell that connects to a specific instance of mongod
mongo import query will not work inside the shell
When I try to import my json data file into my local instance of mongodb, I get an error. The code that I am using is shown below.
> mongoimport --db cities --collection zips --type json --file C:/MongoDB/data/zips.json
This is the error that I get.
2014-11-29T20:27:33.803-0800 SyntaxError: Unexpected identifier
what seems to be to problem here?
I just found out that
mongoimport
is used from terminal/command line(cmd), and NOT within the mongo shell.
Subscribe to:
Posts (Atom)
உப்பு மாங்காய்
சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...
-
கந்தன் வேலைக்குச் சென்று கிட்டத்தட்ட பத்து ஆண்டுகளுக்கு பிறகு சொந்த ஊர் திரும்பி இருந்தான். காளிக் கோயிலைத் தாண்டி தான் அவன் வீட்ட...
-
பிரேமாவின் மூத்த ஆண் குழந்தைக்கு முன் பிறந்த இளைய பெண் குழந்தை அவள். வயலும் சேறும் இரண்டற கலந்த ஊர். முழுதாய் மூன்றாம் வகுப்பைத் ...