Posts
Showing posts from 2018
Unit testing using TestRestRemplate spring boot
- Get link
- X
- Other Apps
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.c...
Importance of equals and hashcode method override
- Get link
- X
- Other Apps
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; ...
Automatic Switch Between LAN & Wifi in Windows 10
- Get link
- X
- Other Apps
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. 5. Click Ok. Note : First preference will be LAN if connected.
First Non Repeating Character Using Java 8
- Get link
- X
- Other Apps
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) { ...
First Recurring Character Java Program
- Get link
- X
- Other Apps
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"); } }
Java - How to get all the field names from JSON Schema
- Get link
- X
- Other Apps
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 ++
- Get link
- X
- Other Apps
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.
Handlebar
- Get link
- X
- Other Apps
{{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/
RSql Queries
- Get link
- X
- Other Apps
'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'), e...
RSQL / FIQL parser
- Get link
- X
- Other Apps
//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-...
JSON Schena Validation FGE
- Get link
- X
- Other Apps
(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
Mongo Import and Export
- Get link
- X
- Other Apps
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.