Sunday, 15 August 2021

ArangoDB: toArray: Convert the collection into an array

'collection.toArray()' method convert the collection into an array of documents. Since it creates a copy of collection in the system RAM do not use this method in production environments.

127.0.0.1:8529@abc_org> db.user.toArray()
[ 
  { 
    "_key" : "17089", 
    "_id" : "user/17089", 
    "_rev" : "_cRe1GKq---", 
    "id" : 1, 
    "firstName" : "Sailu", 
    "lastName" : "Ptr", 
    "age" : 32 
  }, 
  { 
    "_key" : "17091", 
    "_id" : "user/17091", 
    "_rev" : "_cRe1GKu---", 
    "id" : 2, 
    "firstName" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 33 
  }, 
  { 
    "_key" : "17093", 
    "_id" : "user/17093", 
    "_rev" : "_cRe1GKy---", 
    "id" : 3, 
    "firstName" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  }, 
  { 
    "_key" : "17095", 
    "_id" : "user/17095", 
    "_rev" : "_cRe1H-6---", 
    "id" : 4, 
    "firstName" : "Venkat", 
    "lastName" : "Ptr", 
    "age" : 35 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

ArangoDB: count: Count number of live documents

'collection.count()' method return number of liver documents in a collection.

127.0.0.1:8529@abc_org> db.user.count()
4

 

 

Previous                                                    Next                                                    Home

Neo4j: Match the nodes that has any relationship with other nodes

Below statement return all the nodes that has any relationship with other nodes.

MATCH (n1)--(n2) RETURN n1, n2

 

(): Represent node

--: Represent relationship between nodes

 

Let’s see it with an example.

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 


Step 2: Insert data by executing below query.

CREATE (p1:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true, designation: "CEO"}),
(p2:Person{name: "Murali", age: 32, city: "Hyderabad", male: true, designation: "Vice President"}),
(p3:Person{name: "Gopi", age: 34, city: "Chennai", male: true, designation: "Vice President"}),
(p4:Person{name: "Kiran", age: 28, city: "Bangalore", male: true, designation: "Director"}),
(p5:Person{name: "Siva", age: 45, city: "Hyderabad", male: true, designation: "Director"}),
(p6:Person{name: "Kiran", age: 36, city: "Hyderabad", male: true, designation: "Manager"}),
(p7:Person{name: "Ritweek", age: 23, city: "Bangalore", male: true, designation: "Manager"}),
(p8:Person{name: "Sudheer", age: 24, city: "Chennai", male: true, designation: "Manager"}),
(p9:Person{name: "Naveen", age: 37, city: "Bangalore", male: true, designation: "Employee"}),
(p10:Person{name: "Sailaja", age: 31, city: "Hyderabad", female: true, designation: "Employee"}),
(p11:Person{name: "Harika", age: 36, city: "Bangalore", female: true, designation: "Employee"}),
(p12:Person{name: "Brahmam", age: 41, city: "Bangalore", male: true, designation: "Employee"}),
(office1:Location{name: "Pluto", city: "Bangalore"}),
(office2:Location{name: "Mars", city: "Chennai"})
MERGE (p2)-[:REPORT_TO]->(p1)
MERGE (p3)-[:REPORT_TO]->(p1)
MERGE (p4)-[:REPORT_TO]->(p2)
MERGE (p5)-[:REPORT_TO]->(p3)
MERGE (p6)-[:REPORT_TO]->(p4)
MERGE (p7)-[:REPORT_TO]->(p4)
MERGE (p8)-[:REPORT_TO]->(p5)
MERGE (p9)-[:REPORT_TO]->(p6)
MERGE (p10)-[:REPORT_TO]->(p6)
MERGE (p11)-[:REPORT_TO]->(p7)
MERGE (p12)-[:REPORT_TO]->(p8)
RETURN p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, office1, office2

 

 


Step 3: Execute below query to get all the nodes that has any relationship with other node.

MATCH (n1)--(n2) RETURN n1, n2

 




Previous                                                    Next                                                    Home

Neo4j: Get all the nodes which are labelled with Person and whose designation is Manager

 

MATCH (n:Person{designation: "Manager"}) RETURN n

 


 

Previous                                                    Next                                                    Home

Junit5: RepetitionInfo: Retrieve information about current repetition

If a method parameter in a @RepeatedTest, @BeforeEach, or @AfterEach method is of type RepetitionInfo, the RepetitionInfoParameterResolver will supply an instance of RepetitionInfo. RepetitionInfo can then be used to retrieve information about the current repetition and the total number of repetitions for the corresponding @RepeatedTest.

 

RepetitionInfoDemo.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;

public class RepetitionInfoDemo {
	
	private static void printRepetitionInfo(RepetitionInfo repetitionInfo) {
		int currentRepetition = repetitionInfo.getCurrentRepetition();
		int totalRepetitions = repetitionInfo.getTotalRepetitions();
		System.out.println("currentRepetition : " + currentRepetition + " , " + "totalRepetitions : " + totalRepetitions);
	}
	
	@BeforeEach
	public void setup(RepetitionInfo repetitionInfo) {
		System.out.println("\nInside Setup method");
		printRepetitionInfo(repetitionInfo);
	}
	
	@DisplayName("TestCase1")
	@RepeatedTest(value = 3, name = "{displayName} {currentRepetition}/{totalRepetitions}")
	void test1(RepetitionInfo repetitionInfo) {
		System.out.println("Inside test1 method");
		printRepetitionInfo(repetitionInfo);
		assertTrue(true);
	}
	
	@AfterEach
	public void cleanup(RepetitionInfo repetitionInfo) {
		System.out.println("Inside cleanup method");
		printRepetitionInfo(repetitionInfo);
	}
	


}

 

When you ran above test, you will see below messages in console.

Inside Setup method
currentRepetition : 1 , totalRepetitions : 3
Inside test1 method
currentRepetition : 1 , totalRepetitions : 3
Inside cleanup method
currentRepetition : 1 , totalRepetitions : 3

Inside Setup method
currentRepetition : 2 , totalRepetitions : 3
Inside test1 method
currentRepetition : 2 , totalRepetitions : 3
Inside cleanup method
currentRepetition : 2 , totalRepetitions : 3

Inside Setup method
currentRepetition : 3 , totalRepetitions : 3
Inside test1 method
currentRepetition : 3 , totalRepetitions : 3
Inside cleanup method
currentRepetition : 3 , totalRepetitions : 3

 


 

 

  

Previous                                                    Next                                                    Home

Junit5: TestInfo: Get information about the test

TestInfo class is used to inject information about the current test or container into a @Test, @RepeatedTest, @ParameterizedTest,  @TestFactory, @BeforeEach, @AfterEach, @BeforeAll, and @AfterAll methods.

 

If a constructor or method parameter is of type TestInfo, the TestInfoParameterResolver will supply an instance of TestInfo corresponding to the current container or test as the value for the parameter.

 

Using TestInfo instance, you can retrieve information about the current container or test such as the display name, the test class, the test method, and associated tags

 

TestInfoDemo.java

package com.sample.app;

import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

public class TestInfoDemo {

	public TestInfoDemo(TestInfo testInfo) {
		System.out.println("\nInside Constructor");
		String displayName = testInfo.getDisplayName();
		System.out.println("\t" + displayName);
	}

	@BeforeEach
	void init(TestInfo testInfo) {
		System.out.println("\nInside init method");
		String displayName = testInfo.getDisplayName();
		System.out.println("\t" + displayName);
	}

	@Test
	@DisplayName("TEST 1")
	@Tag("demoTest")
	@Tag("helloworld")
	void test1(TestInfo testInfo) {
		System.out.println("\nInside test1 method");

		String displayName = testInfo.getDisplayName();
		Set<String> tags = testInfo.getTags();

		System.out.println("\t" + displayName);
		System.out.println("\t" + tags);

	}

	@Test
	void test2() {
		System.out.println("\nInside test2 method");
	}
}

 

 

  

Previous                                                    Next                                                    Home

Thursday, 12 August 2021

Junit5: TestReporterParameterResolver: Get data about current test run

If a constructor or method parameter is of type TestReporter, the TestReporterParameterResolver will supply an instance of TestReporter. The TestReporter can be used to publish additional data about the current test run.

 

Find the below working applications.

 

TestReporterDemo.java

package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;

public class TestReporterDemo {
	@Test
	void reportSingleValue(TestReporter testReporter) {
		System.out.println("\nInside reportSingleValue method");
		testReporter.publishEntry("a status message");
	}

	@Test
	void reportKeyValuePair(TestReporter testReporter) {
		System.out.println("\nInside reportKeyValuePair method");
		testReporter.publishEntry("methodName", "reportKeyValuePair");
	}

	@Test
	void reportMultipleKeyValuePairs(TestReporter testReporter) {
		System.out.println("\nInside reportMultipleKeyValuePairs method");
		
		Map<String, String> values = new HashMap<>();
		testReporter.publishEntry("methodName", "reportMultipleKeyValuePairs");
		testReporter.publishEntry("demo", "TestReporter");

		testReporter.publishEntry(values);
	}
}

Run above test class, you will see below messages in console.

Inside reportSingleValue method
TestIdentifier [reportSingleValue(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.408, value = 'a status message']

Inside reportMultipleKeyValuePairs method
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416, methodName = 'reportMultipleKeyValuePairs']
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416, demo = 'TestReporter']
TestIdentifier [reportMultipleKeyValuePairs(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.416]

Inside reportKeyValuePair method
TestIdentifier [reportKeyValuePair(TestReporter)]
ReportEntry [timestamp = 2020-07-04T12:35:26.418, methodName = 'reportKeyValuePair']

 

Junit test report window is given below.

 



 

  

Previous                                                    Next                                                    Home