Merge branch 'master' into BAEL-1479
This commit is contained in:
commit
7e6cb22c7d
|
@ -33,4 +33,4 @@
|
||||||
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
|
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
|
||||||
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
|
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
|
||||||
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
|
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
|
||||||
- [How to Wait for Threads to Finish in the ExecutorService](http://www.baeldung.com/java-executor-wait-for-threads)
|
- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.baeldung.trie;
|
||||||
|
|
||||||
|
public class Trie {
|
||||||
|
private TrieNode root;
|
||||||
|
|
||||||
|
Trie() {
|
||||||
|
root = new TrieNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(String word) {
|
||||||
|
TrieNode current = root;
|
||||||
|
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
current = current.getChildren().computeIfAbsent(word.charAt(i), c -> new TrieNode());
|
||||||
|
}
|
||||||
|
current.setEndOfWord(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean delete(String word) {
|
||||||
|
return delete(root, word, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsNode(String word) {
|
||||||
|
TrieNode current = root;
|
||||||
|
|
||||||
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
char ch = word.charAt(i);
|
||||||
|
TrieNode node = current.getChildren().get(ch);
|
||||||
|
if (node == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
current = node;
|
||||||
|
}
|
||||||
|
return current.isEndOfWord();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return root == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean delete(TrieNode current, String word, int index) {
|
||||||
|
if (index == word.length()) {
|
||||||
|
if (!current.isEndOfWord()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
current.setEndOfWord(false);
|
||||||
|
return current.getChildren().isEmpty();
|
||||||
|
}
|
||||||
|
char ch = word.charAt(index);
|
||||||
|
TrieNode node = current.getChildren().get(ch);
|
||||||
|
if (node == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
|
||||||
|
|
||||||
|
if (shouldDeleteCurrentNode) {
|
||||||
|
current.getChildren().remove(ch);
|
||||||
|
return current.getChildren().isEmpty();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.trie;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class TrieNode {
|
||||||
|
private Map<Character, TrieNode> children;
|
||||||
|
private boolean endOfWord;
|
||||||
|
|
||||||
|
public TrieNode() {
|
||||||
|
children = new HashMap<>();
|
||||||
|
endOfWord = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Character, TrieNode> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(Map<Character, TrieNode> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEndOfWord() {
|
||||||
|
return endOfWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEndOfWord(boolean endOfWord) {
|
||||||
|
this.endOfWord = endOfWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.baeldung.trie;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class TrieTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmptyTrie_thenNoElements() {
|
||||||
|
Trie trie = new Trie();
|
||||||
|
|
||||||
|
assertFalse(trie.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrie_whenAddingElements_thenTrieNotEmpty() {
|
||||||
|
Trie trie = createExampleTrie();
|
||||||
|
|
||||||
|
assertFalse(trie.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrie_whenAddingElements_thenTrieHasThoseElements() {
|
||||||
|
Trie trie = createExampleTrie();
|
||||||
|
|
||||||
|
assertFalse(trie.containsNode("3"));
|
||||||
|
assertFalse(trie.containsNode("vida"));
|
||||||
|
|
||||||
|
assertTrue(trie.containsNode("Programming"));
|
||||||
|
assertTrue(trie.containsNode("is"));
|
||||||
|
assertTrue(trie.containsNode("a"));
|
||||||
|
assertTrue(trie.containsNode("way"));
|
||||||
|
assertTrue(trie.containsNode("of"));
|
||||||
|
assertTrue(trie.containsNode("life"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrie_whenLookingForNonExistingElement_thenReturnsFalse() {
|
||||||
|
Trie trie = createExampleTrie();
|
||||||
|
|
||||||
|
assertFalse(trie.containsNode("99"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrie_whenDeletingElements_thenTreeDoesNotContainThoseElements() {
|
||||||
|
|
||||||
|
Trie trie = createExampleTrie();
|
||||||
|
|
||||||
|
assertTrue(trie.containsNode("Programming"));
|
||||||
|
trie.delete("Programming");
|
||||||
|
assertFalse(trie.containsNode("Programming"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Trie createExampleTrie() {
|
||||||
|
Trie trie = new Trie();
|
||||||
|
|
||||||
|
trie.insert("Programming");
|
||||||
|
trie.insert("is");
|
||||||
|
trie.insert("a");
|
||||||
|
trie.insert("way");
|
||||||
|
trie.insert("of");
|
||||||
|
trie.insert("life");
|
||||||
|
|
||||||
|
return trie;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.netty;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ChannelHandlerA extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private Logger logger = Logger.getLogger(getClass().getName());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
throw new Exception("Ooops");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||||
|
logger.info("Exception Occurred in ChannelHandler A");
|
||||||
|
ctx.fireExceptionCaught(cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.netty;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
public class ChannelHandlerB extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private Logger logger = Logger.getLogger(getClass().getName());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||||
|
logger.info("Exception Handled in ChannelHandler B");
|
||||||
|
logger.info(cause.getLocalizedMessage());
|
||||||
|
//do more exception handling
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.netty;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
|
import io.netty.channel.EventLoopGroup;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
|
||||||
|
public class NettyServerB {
|
||||||
|
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
private NettyServerB(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void run() throws Exception {
|
||||||
|
|
||||||
|
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||||
|
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ServerBootstrap b = new ServerBootstrap();
|
||||||
|
b.group(bossGroup, workerGroup)
|
||||||
|
.channel(NioServerSocketChannel.class)
|
||||||
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
|
public void initChannel(SocketChannel ch) throws Exception {
|
||||||
|
ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.option(ChannelOption.SO_BACKLOG, 128)
|
||||||
|
.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
ChannelFuture f = b.bind(port).sync(); // (7)
|
||||||
|
f.channel().closeFuture().sync();
|
||||||
|
} finally {
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
|
bossGroup.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new NettyServerB(8080).run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -59,10 +59,10 @@
|
||||||
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jackson.version>2.8.5</jackson.version>
|
<jackson.version>2.9.3</jackson.version>
|
||||||
<h2.version>1.4.193</h2.version>
|
<h2.version>1.4.193</h2.version>
|
||||||
<commons-dbcp2.version>2.1.1</commons-dbcp2.version>
|
<commons-dbcp2.version>2.1.1</commons-dbcp2.version>
|
||||||
<log4j-core.version>2.7</log4j-core.version>
|
<log4j-core.version>2.10.0</log4j-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.logging.log4j2.tests;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public class JSONLayoutTest {
|
||||||
|
|
||||||
|
private static Logger logger;
|
||||||
|
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
||||||
|
private PrintStream ps = new PrintStream(consoleOutput);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
// Redirect console output to our stream
|
||||||
|
System.setOut(ps);
|
||||||
|
logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
|
||||||
|
logger.debug("Debug message");
|
||||||
|
String currentLog = consoleOutput.toString();
|
||||||
|
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidJSON(String jsonInString) {
|
||||||
|
try {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.readTree(jsonInString);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +1,25 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude" status="WARN">
|
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
|
||||||
|
status="WARN">
|
||||||
<Appenders>
|
<Appenders>
|
||||||
<xi:include href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
|
<xi:include
|
||||||
|
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
|
||||||
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||||
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable" />
|
<PatternLayout
|
||||||
|
pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable" />
|
||||||
</Console>
|
</Console>
|
||||||
<Console name="ConsoleRedAppender" target="SYSTEM_OUT">
|
<Console name="ConsoleRedAppender" target="SYSTEM_OUT">
|
||||||
<PatternLayout pattern="%style{%message}{red}%n" />
|
<PatternLayout pattern="%style{%message}{red}%n" />
|
||||||
<MarkerFilter marker="CONN_TRACE" />
|
<MarkerFilter marker="CONN_TRACE" />
|
||||||
</Console>
|
</Console>
|
||||||
<Console name="ConsoleGreenAppender" target="SYSTEM_OUT">
|
<Console name="ConsoleGreenAppender" target="SYSTEM_OUT">
|
||||||
<PatternLayout pattern="%style{userId=%X{userId}:}{white} %style{%message}{green}%n" />
|
<PatternLayout
|
||||||
|
pattern="%style{userId=%X{userId}:}{white} %style{%message}{green}%n" />
|
||||||
|
</Console>
|
||||||
|
<Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
|
||||||
|
<JsonLayout complete="false" compact="false">
|
||||||
|
<KeyValuePair key="myCustomField" value="myCustomValue" />
|
||||||
|
</JsonLayout>
|
||||||
</Console>
|
</Console>
|
||||||
<File name="JSONLogfileAppender" fileName="target/logfile.json">
|
<File name="JSONLogfileAppender" fileName="target/logfile.json">
|
||||||
<JSONLayout compact="true" eventEol="true" />
|
<JSONLayout compact="true" eventEol="true" />
|
||||||
|
@ -19,53 +28,58 @@
|
||||||
<Async name="AsyncAppender" bufferSize="80">
|
<Async name="AsyncAppender" bufferSize="80">
|
||||||
<AppenderRef ref="JSONLogfileAppender" />
|
<AppenderRef ref="JSONLogfileAppender" />
|
||||||
</Async>
|
</Async>
|
||||||
<!--
|
<!-- <Syslog name="Syslog" format="RFC5424" host="localhost" port="514"
|
||||||
<Syslog name="Syslog" format="RFC5424" host="localhost" port="514" protocol="TCP" facility="local3" connectTimeoutMillis="10000" reconnectionDelayMillis="5000" mdcId="mdc" includeMDC="true" />
|
protocol="TCP" facility="local3" connectTimeoutMillis="10000" reconnectionDelayMillis="5000"
|
||||||
<Failover name="FailoverAppender" primary="Syslog">
|
mdcId="mdc" includeMDC="true" /> <Failover name="FailoverAppender" primary="Syslog">
|
||||||
<Failovers>
|
<Failovers> <AppenderRef ref="ConsoleAppender" /> </Failovers> </Failover> -->
|
||||||
<AppenderRef ref="ConsoleAppender" />
|
|
||||||
</Failovers>
|
|
||||||
</Failover>
|
|
||||||
-->
|
|
||||||
<JDBC name="JDBCAppender" tableName="logs">
|
<JDBC name="JDBCAppender" tableName="logs">
|
||||||
<ConnectionFactory class="com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory" method="getConnection" />
|
<ConnectionFactory
|
||||||
|
class="com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory"
|
||||||
|
method="getConnection" />
|
||||||
<Column name="when" isEventTimestamp="true" />
|
<Column name="when" isEventTimestamp="true" />
|
||||||
<Column name="logger" pattern="%logger" />
|
<Column name="logger" pattern="%logger" />
|
||||||
<Column name="level" pattern="%level" />
|
<Column name="level" pattern="%level" />
|
||||||
<Column name="message" pattern="%message" />
|
<Column name="message" pattern="%message" />
|
||||||
<Column name="throwable" pattern="%ex{full}" />
|
<Column name="throwable" pattern="%ex{full}" />
|
||||||
</JDBC>
|
</JDBC>
|
||||||
<RollingFile name="XMLRollingfileAppender" fileName="target/logfile.xml" filePattern="target/logfile-%d{yyyy-MM-dd}-%i.log.gz">
|
<RollingFile name="XMLRollingfileAppender" fileName="target/logfile.xml"
|
||||||
|
filePattern="target/logfile-%d{yyyy-MM-dd}-%i.log.gz">
|
||||||
<XMLLayout />
|
<XMLLayout />
|
||||||
<Policies>
|
<Policies>
|
||||||
<SizeBasedTriggeringPolicy size="17 kB" />
|
<SizeBasedTriggeringPolicy
|
||||||
|
size="17 kB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
</Appenders>
|
</Appenders>
|
||||||
<Loggers>
|
<Loggers>
|
||||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE" additivity="false">
|
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||||
|
additivity="false">
|
||||||
<AppenderRef ref="ConsoleRedAppender" />
|
<AppenderRef ref="ConsoleRedAppender" />
|
||||||
</Logger>
|
</Logger>
|
||||||
<Logger name="CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT" level="INFO" additivity="false">
|
<Logger name="CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT"
|
||||||
|
level="INFO" additivity="false">
|
||||||
<AppenderRef ref="ConsoleGreenAppender" />
|
<AppenderRef ref="ConsoleGreenAppender" />
|
||||||
<ThreadContextMapFilter>
|
<ThreadContextMapFilter>
|
||||||
<KeyValuePair key="userId" value="1000" />
|
<KeyValuePair key="userId" value="1000" />
|
||||||
</ThreadContextMapFilter>
|
</ThreadContextMapFilter>
|
||||||
</Logger>
|
</Logger>
|
||||||
<Logger name="ASYNC_JSON_FILE_APPENDER" level="INFO" additivity="false">
|
<Logger name="ASYNC_JSON_FILE_APPENDER" level="INFO"
|
||||||
|
additivity="false">
|
||||||
<AppenderRef ref="AsyncAppender" />
|
<AppenderRef ref="AsyncAppender" />
|
||||||
</Logger>
|
</Logger>
|
||||||
<!--
|
<!-- <Logger name="FAIL_OVER_SYSLOG_APPENDER" level="INFO" additivity="false">
|
||||||
<Logger name="FAIL_OVER_SYSLOG_APPENDER" level="INFO" additivity="false">
|
<AppenderRef ref="FailoverAppender" /> </Logger> -->
|
||||||
<AppenderRef ref="FailoverAppender" />
|
|
||||||
</Logger>
|
|
||||||
-->
|
|
||||||
<Logger name="JDBC_APPENDER" level="INFO" additivity="false">
|
<Logger name="JDBC_APPENDER" level="INFO" additivity="false">
|
||||||
<AppenderRef ref="JDBCAppender" />
|
<AppenderRef ref="JDBCAppender" />
|
||||||
</Logger>
|
</Logger>
|
||||||
<Logger name="XML_ROLLING_FILE_APPENDER" level="INFO" additivity="false">
|
<Logger name="XML_ROLLING_FILE_APPENDER" level="INFO"
|
||||||
|
additivity="false">
|
||||||
<AppenderRef ref="XMLRollingfileAppender" />
|
<AppenderRef ref="XMLRollingfileAppender" />
|
||||||
</Logger>
|
</Logger>
|
||||||
|
<Logger name="CONSOLE_JSON_APPENDER" level="TRACE"
|
||||||
|
additivity="false">
|
||||||
|
<AppenderRef ref="ConsoleJSONAppender" />
|
||||||
|
</Logger>
|
||||||
<Root level="DEBUG">
|
<Root level="DEBUG">
|
||||||
<AppenderRef ref="ConsoleAppender" />
|
<AppenderRef ref="ConsoleAppender" />
|
||||||
</Root>
|
</Root>
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<logback.version>1.2.3</logback.version>
|
<logback.version>1.2.3</logback.version>
|
||||||
|
<logback.contrib.version>0.1.5</logback.contrib.version>
|
||||||
|
<jackson.version>2.9.3</jackson.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -28,7 +30,21 @@
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
<version>${logback.version}</version>
|
<version>${logback.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback.contrib</groupId>
|
||||||
|
<artifactId>logback-json-classic</artifactId>
|
||||||
|
<version>${logback.contrib.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback.contrib</groupId>
|
||||||
|
<artifactId>logback-jackson</artifactId>
|
||||||
|
<version>${logback.contrib.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.logback;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public class JSONLayoutTest {
|
||||||
|
|
||||||
|
private static Logger logger;
|
||||||
|
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
||||||
|
private PrintStream ps = new PrintStream(consoleOutput);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
// Redirect console output to our stream
|
||||||
|
System.setOut(ps);
|
||||||
|
logger = LoggerFactory.getLogger("jsonLogger");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
|
||||||
|
logger.debug("Debug message");
|
||||||
|
String currentLog = consoleOutput.toString();
|
||||||
|
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidJSON(String jsonInString) {
|
||||||
|
try {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.readTree(jsonInString);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
<configuration debug="true">
|
<configuration debug="false">
|
||||||
|
|
||||||
|
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||||
|
|
||||||
<appender name="map" class="com.baeldung.logback.MapAppender">
|
<appender name="map" class="com.baeldung.logback.MapAppender">
|
||||||
<prefix>test</prefix>
|
<prefix>test</prefix>
|
||||||
|
@ -6,6 +8,21 @@
|
||||||
|
|
||||||
<appender name="badMap" class="com.baeldung.logback.MapAppender" />
|
<appender name="badMap" class="com.baeldung.logback.MapAppender" />
|
||||||
|
|
||||||
|
# JSON appender
|
||||||
|
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
|
||||||
|
<jsonFormatter
|
||||||
|
class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
|
||||||
|
<prettyPrint>true</prettyPrint>
|
||||||
|
</jsonFormatter>
|
||||||
|
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="jsonLogger" level="TRACE">
|
||||||
|
<appender-ref ref="json" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
<root level="debug">
|
<root level="debug">
|
||||||
<appender-ref ref="map" />
|
<appender-ref ref="map" />
|
||||||
<appender-ref ref="badMap" />
|
<appender-ref ref="badMap" />
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.kong;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/stock")
|
||||||
|
public class QueryController {
|
||||||
|
|
||||||
|
private static int REQUEST_COUNTER = 0;
|
||||||
|
|
||||||
|
@GetMapping("/reqcount")
|
||||||
|
public int getReqCount(){
|
||||||
|
return REQUEST_COUNTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{code}")
|
||||||
|
public String getStockPrice(@PathVariable String code){
|
||||||
|
REQUEST_COUNTER++;
|
||||||
|
if("BTC".equalsIgnoreCase(code))
|
||||||
|
return "10000";
|
||||||
|
else return "N/A";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.kong;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class StockApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(StockApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
server.port=8080
|
server.port=9090
|
||||||
server.contextPath=/springbootapp
|
server.contextPath=/springbootapp
|
||||||
management.port=8081
|
management.port=8081
|
||||||
management.address=127.0.0.1
|
management.address=127.0.0.1
|
||||||
|
|
|
@ -0,0 +1,165 @@
|
||||||
|
package com.baeldung.kong;
|
||||||
|
|
||||||
|
import com.baeldung.kong.domain.APIObject;
|
||||||
|
import com.baeldung.kong.domain.ConsumerObject;
|
||||||
|
import com.baeldung.kong.domain.KeyAuthObject;
|
||||||
|
import com.baeldung.kong.domain.PluginObject;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
|
||||||
|
public class KongAdminAPILiveTest {
|
||||||
|
|
||||||
|
private String getStockPrice(String code) {
|
||||||
|
try {
|
||||||
|
return restTemplate.getForObject(new URI("http://localhost:8080/stock/" + code), String.class);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() {
|
||||||
|
String response = getStockPrice("btc");
|
||||||
|
assertEquals("10000", response);
|
||||||
|
|
||||||
|
response = getStockPrice("eth");
|
||||||
|
assertEquals("N/A", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
|
||||||
|
restTemplate.delete("http://localhost:8001/apis/stock-api");
|
||||||
|
|
||||||
|
APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/");
|
||||||
|
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
|
||||||
|
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.CREATED, addAPIResp.getStatusCode());
|
||||||
|
|
||||||
|
addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||||
|
assertEquals(HttpStatus.CONFLICT, addAPIResp.getStatusCode());
|
||||||
|
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||||
|
|
||||||
|
assertTrue(apiListResp.contains("stock-api"));
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Host", "stock.api");
|
||||||
|
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||||
|
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
|
||||||
|
assertEquals("10000", stockPriceResp.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenKongAdminAPI_whenAddAPIConsumer_thenAdded() {
|
||||||
|
restTemplate.delete("http://localhost:8001/consumers/eugenp");
|
||||||
|
|
||||||
|
ConsumerObject consumer = new ConsumerObject("eugenp");
|
||||||
|
HttpEntity<ConsumerObject> addConsumerEntity = new HttpEntity<>(consumer);
|
||||||
|
ResponseEntity<String> addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers/", addConsumerEntity, String.class);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.CREATED, addConsumerResp.getStatusCode());
|
||||||
|
|
||||||
|
addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers", addConsumerEntity, String.class);
|
||||||
|
assertEquals(HttpStatus.CONFLICT, addConsumerResp.getStatusCode());
|
||||||
|
|
||||||
|
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
|
||||||
|
assertTrue(consumerListResp.contains("eugenp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAPI_whenEnableAuth_thenAnonymousDenied() throws Exception {
|
||||||
|
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||||
|
if (!apiListResp.contains("stock-api")) {
|
||||||
|
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginObject authPlugin = new PluginObject("key-auth");
|
||||||
|
ResponseEntity<String> enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class);
|
||||||
|
|
||||||
|
assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode());
|
||||||
|
|
||||||
|
String pluginsResp = restTemplate.getForObject("http://localhost:8001/apis/stock-api/plugins", String.class);
|
||||||
|
assertTrue(pluginsResp.contains("key-auth"));
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Host", "stock.api");
|
||||||
|
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||||
|
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
assertEquals(HttpStatus.UNAUTHORIZED, stockPriceResp.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception {
|
||||||
|
String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class);
|
||||||
|
if (!apiListResp.contains("stock-api")) {
|
||||||
|
givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong();
|
||||||
|
}
|
||||||
|
|
||||||
|
String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class);
|
||||||
|
if (!consumerListResp.contains("eugenp")) {
|
||||||
|
givenKongAdminAPI_whenAddAPIConsumer_thenAdded();
|
||||||
|
}
|
||||||
|
|
||||||
|
final String consumerKey = "eugenp.pass";
|
||||||
|
KeyAuthObject keyAuth = new KeyAuthObject(consumerKey);
|
||||||
|
ResponseEntity<String> keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class);
|
||||||
|
|
||||||
|
assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode());
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Host", "stock.api");
|
||||||
|
headers.set("apikey", consumerKey);
|
||||||
|
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||||
|
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
|
||||||
|
assertEquals("10000", stockPriceResp.getBody());
|
||||||
|
|
||||||
|
headers.set("apikey", "wrongpass");
|
||||||
|
requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||||
|
stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAdminAPIProxy_whenAddAPIViaProxy_thenAPIAdded() throws Exception {
|
||||||
|
APIObject adminAPI = new APIObject("admin-api", "admin.api", "http://localhost:8001", "/admin-api");
|
||||||
|
HttpEntity<APIObject> apiEntity = new HttpEntity<>(adminAPI);
|
||||||
|
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||||
|
|
||||||
|
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Host", "admin.api");
|
||||||
|
APIObject baeldungAPI = new APIObject("baeldung-api", "baeldung.com", "http://ww.baeldung.com", "/");
|
||||||
|
RequestEntity<APIObject> requestEntity = new RequestEntity<>(baeldungAPI, headers, HttpMethod.POST, new URI("http://localhost:8000/admin-api/apis"));
|
||||||
|
addAPIResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
|
||||||
|
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.baeldung.kong;
|
||||||
|
|
||||||
|
import com.baeldung.kong.domain.APIObject;
|
||||||
|
import com.baeldung.kong.domain.TargetObject;
|
||||||
|
import com.baeldung.kong.domain.UpstreamObject;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class)
|
||||||
|
public class KongLoadBalanceLiveTest {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
|
||||||
|
UpstreamObject upstream = new UpstreamObject("stock.api.service");
|
||||||
|
ResponseEntity<String> addUpstreamResp = restTemplate.postForEntity("http://localhost:8001/upstreams", new HttpEntity<>(upstream), String.class);
|
||||||
|
assertTrue(HttpStatus.CREATED == addUpstreamResp.getStatusCode() || HttpStatus.CONFLICT == addUpstreamResp.getStatusCode());
|
||||||
|
|
||||||
|
TargetObject testTarget = new TargetObject("localhost:8080", 10);
|
||||||
|
ResponseEntity<String> addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(testTarget), String.class);
|
||||||
|
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
|
||||||
|
|
||||||
|
TargetObject releaseTarget = new TargetObject("localhost:9090", 40);
|
||||||
|
addTargetResp = restTemplate.postForEntity("http://localhost:8001/upstreams/stock.api.service/targets", new HttpEntity<>(releaseTarget), String.class);
|
||||||
|
assertTrue(HttpStatus.CREATED == addTargetResp.getStatusCode() || HttpStatus.CONFLICT == addTargetResp.getStatusCode());
|
||||||
|
|
||||||
|
APIObject stockAPI = new APIObject("balanced-stock-api", "balanced.stock.api", "http://stock.api.service", "/");
|
||||||
|
HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI);
|
||||||
|
ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class);
|
||||||
|
assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode());
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Host", "balanced.stock.api");
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc"));
|
||||||
|
ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
assertEquals("10000", stockPriceResp.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
int releaseCount = restTemplate.getForObject("http://localhost:9090/stock/reqcount", Integer.class);
|
||||||
|
int testCount = restTemplate.getForObject("http://localhost:8080/stock/reqcount", Integer.class);
|
||||||
|
|
||||||
|
assertTrue(Math.round(releaseCount * 1.0 / testCount) == 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class APIObject {
|
||||||
|
|
||||||
|
public APIObject() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIObject(String name, String hosts, String upstream_url, String uris) {
|
||||||
|
this.name = name;
|
||||||
|
this.hosts = hosts;
|
||||||
|
this.upstream_url = upstream_url;
|
||||||
|
this.uris = uris;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String hosts;
|
||||||
|
private String upstream_url;
|
||||||
|
private String uris;
|
||||||
|
|
||||||
|
public String getUris() {
|
||||||
|
return uris;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUris(String uris) {
|
||||||
|
this.uris = uris;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUpstream_url() {
|
||||||
|
return upstream_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUpstream_url(String upstream_url) {
|
||||||
|
this.upstream_url = upstream_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHosts() {
|
||||||
|
return hosts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHosts(String hosts) {
|
||||||
|
this.hosts = hosts;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class ConsumerObject {
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
private String custom_id;
|
||||||
|
|
||||||
|
public ConsumerObject(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConsumerObject(String username, String custom_id) {
|
||||||
|
this.username = username;
|
||||||
|
this.custom_id = custom_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCustom_id() {
|
||||||
|
return custom_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustom_id(String custom_id) {
|
||||||
|
this.custom_id = custom_id;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class KeyAuthObject {
|
||||||
|
|
||||||
|
public KeyAuthObject(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class PluginObject {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String consumer_id;
|
||||||
|
|
||||||
|
public PluginObject(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConsumer_id() {
|
||||||
|
return consumer_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsumer_id(String consumer_id) {
|
||||||
|
this.consumer_id = consumer_id;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class TargetObject {
|
||||||
|
|
||||||
|
public TargetObject(String target, int weight) {
|
||||||
|
this.target = target;
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String target;
|
||||||
|
private int weight;
|
||||||
|
|
||||||
|
public String getTarget() {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTarget(String target) {
|
||||||
|
this.target = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeight(int weight) {
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.kong.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aiet
|
||||||
|
*/
|
||||||
|
public class UpstreamObject {
|
||||||
|
|
||||||
|
public UpstreamObject(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,10 +43,6 @@
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-jwt</artifactId>
|
<artifactId>spring-security-jwt</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.code.gson</groupId>
|
|
||||||
<artifactId>gson</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|
|
@ -6,16 +6,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.baeldung.model.Person;
|
import com.baeldung.model.Person;
|
||||||
import com.google.gson.Gson;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class PersonInfoController {
|
public class PersonInfoController {
|
||||||
|
|
||||||
@GetMapping("/personResource")
|
@GetMapping("/personResource")
|
||||||
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
|
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
|
||||||
public @ResponseBody String personInfo() {
|
public @ResponseBody Person personInfo() {
|
||||||
Gson gson = new Gson();
|
return new Person("abir", "Dhaka", "Bangladesh", 29, "Male");
|
||||||
String person = gson.toJson(new Person("abir", "Dhaka", "Bangladesh", 29, "Male"));
|
|
||||||
return person;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,10 +11,9 @@ public class Person {
|
||||||
private Date dateOfBirth;
|
private Date dateOfBirth;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(int age, String fullName, Date dateOfBirth) {
|
Person(int age, String fullName, Date dateOfBirth) {
|
||||||
super();
|
super();
|
||||||
this.age = age;
|
this.age = age;
|
||||||
this.fullName = fullName;
|
this.fullName = fullName;
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -78,12 +79,9 @@ public class ElasticSearchManualTest {
|
||||||
SearchHit[] searchHits = response
|
SearchHit[] searchHits = response
|
||||||
.getHits()
|
.getHits()
|
||||||
.getHits();
|
.getHits();
|
||||||
List<Person> results = new ArrayList<>();
|
List<Person> results = Arrays.stream(searchHits)
|
||||||
for (SearchHit hit : searchHits) {
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
String sourceAsString = hit.getSourceAsString();
|
.collect(Collectors.toList());
|
||||||
Person person = JSON.parseObject(sourceAsString, Person.class);
|
|
||||||
results.add(person);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -125,11 +123,10 @@ public class ElasticSearchManualTest {
|
||||||
.actionGet();
|
.actionGet();
|
||||||
response2.getHits();
|
response2.getHits();
|
||||||
response3.getHits();
|
response3.getHits();
|
||||||
List<SearchHit> searchHits = Arrays.asList(response
|
|
||||||
.getHits()
|
final List<Person> results = Arrays.stream(response.getHits().getHits())
|
||||||
.getHits());
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
final List<Person> results = new ArrayList<>();
|
.collect(Collectors.toList());
|
||||||
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
package com.baeldung.elasticsearch;
|
package com.baeldung.elasticsearch;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
@ -15,6 +10,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||||
import org.elasticsearch.common.unit.DistanceUnit;
|
import org.elasticsearch.common.unit.DistanceUnit;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -24,14 +20,19 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.baeldung.spring.data.es.config.Config;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
public class GeoQueriesTest {
|
public class GeoQueriesTest {
|
||||||
|
|
||||||
public static final String WONDERS_OF_WORLD = "wonders-of-world";
|
private static final String WONDERS_OF_WORLD = "wonders-of-world";
|
||||||
public static final String WONDERS = "Wonders";
|
private static final String WONDERS = "Wonders";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
private ElasticsearchTemplate elasticsearchTemplate;
|
||||||
|
|
||||||
|
@ -73,9 +74,7 @@ public class GeoQueriesTest {
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(tajMahalId));
|
assertTrue(ids.contains(tajMahalId));
|
||||||
}
|
}
|
||||||
|
@ -103,9 +102,7 @@ public class GeoQueriesTest {
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(pyramidsOfGizaId));
|
assertTrue(ids.contains(pyramidsOfGizaId));
|
||||||
}
|
}
|
||||||
|
@ -133,9 +130,7 @@ public class GeoQueriesTest {
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
||||||
}
|
}
|
||||||
|
@ -164,9 +159,7 @@ public class GeoQueriesTest {
|
||||||
.actionGet();
|
.actionGet();
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits()
|
List<String> ids = Arrays.stream(searchResponse.getHits()
|
||||||
.getHits())
|
.getHits())
|
||||||
.map(hit -> {
|
.map(SearchHit::getId)
|
||||||
return hit.getId();
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(greatRannOfKutchid));
|
assertTrue(ids.contains(greatRannOfKutchid));
|
||||||
}
|
}
|
||||||
|
@ -175,5 +168,4 @@ public class GeoQueriesTest {
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
|
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
package com.baeldung.spring.data.es;
|
package com.baeldung.spring.data.es;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
import com.baeldung.spring.data.es.model.Article;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
|
import com.baeldung.spring.data.es.model.Author;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
import com.baeldung.spring.data.es.service.ArticleService;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -22,10 +16,15 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.baeldung.spring.data.es.config.Config;
|
import java.util.List;
|
||||||
import com.baeldung.spring.data.es.model.Article;
|
|
||||||
import com.baeldung.spring.data.es.model.Author;
|
import static java.util.Arrays.asList;
|
||||||
import com.baeldung.spring.data.es.service.ArticleService;
|
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -72,21 +71,24 @@ public class ElasticSearchIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
||||||
|
|
||||||
final Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
|
final Page<Article> articleByAuthorName = articleService
|
||||||
|
.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
|
||||||
assertEquals(2L, articleByAuthorName.getTotalElements());
|
assertEquals(2L, articleByAuthorName.getTotalElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
|
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
|
||||||
|
|
||||||
final Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
|
final Page<Article> articleByAuthorName = articleService
|
||||||
|
.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
|
||||||
assertEquals(3L, articleByAuthorName.getTotalElements());
|
assertEquals(3L, articleByAuthorName.getTotalElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
||||||
|
.build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
|
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
|
@ -112,7 +114,8 @@ public class ElasticSearchIntegrationTest {
|
||||||
|
|
||||||
final String articleTitle = "Spring Data Elasticsearch";
|
final String articleTitle = "Spring Data Elasticsearch";
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
final long count = articleService.count();
|
final long count = articleService.count();
|
||||||
|
@ -124,7 +127,8 @@ public class ElasticSearchIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,9 @@
|
||||||
package com.baeldung.spring.data.es;
|
package com.baeldung.spring.data.es;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
import static java.util.stream.Collectors.toList;
|
import com.baeldung.spring.data.es.model.Article;
|
||||||
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
import com.baeldung.spring.data.es.model.Author;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
import com.baeldung.spring.data.es.service.ArticleService;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.unit.Fuzziness;
|
import org.elasticsearch.common.unit.Fuzziness;
|
||||||
|
@ -22,6 +11,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.search.aggregations.Aggregation;
|
import org.elasticsearch.search.aggregations.Aggregation;
|
||||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
||||||
|
@ -35,10 +25,20 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.baeldung.spring.data.es.config.Config;
|
import java.util.Collections;
|
||||||
import com.baeldung.spring.data.es.model.Article;
|
import java.util.List;
|
||||||
import com.baeldung.spring.data.es.model.Author;
|
import java.util.Map;
|
||||||
import com.baeldung.spring.data.es.service.ArticleService;
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -86,14 +86,16 @@ public class ElasticSearchQueryIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "Engines Solutions")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
assertEquals("Search engines", articles.get(0).getTitle());
|
assertEquals("Search engines", articles.get(0).getTitle());
|
||||||
|
@ -101,18 +103,21 @@ public class ElasticSearchQueryIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "elasticsearch data")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(3, articles.size());
|
assertEquals(3, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
||||||
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
|
|
||||||
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")).build();
|
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||||
|
.build();
|
||||||
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(0, articles.size());
|
assertEquals(0, articles.size());
|
||||||
}
|
}
|
||||||
|
@ -130,38 +135,48 @@ public class ElasticSearchQueryIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
|
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
|
||||||
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
||||||
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
|
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation)
|
||||||
|
.execute().actionGet();
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
|
final List<String> keys = topTags.getBuckets().stream()
|
||||||
Collections.sort(keys);
|
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
||||||
|
.sorted()
|
||||||
|
.collect(toList());
|
||||||
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
|
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
|
||||||
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
|
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags")
|
||||||
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
|
.order(Terms.Order.aggregation("_count", false));
|
||||||
|
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation)
|
||||||
|
.execute().actionGet();
|
||||||
|
|
||||||
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
final Map<String, Aggregation> results = response.getAggregations().asMap();
|
||||||
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
final StringTerms topTags = (StringTerms) results.get("top_tags");
|
||||||
|
|
||||||
final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
|
final List<String> keys = topTags.getBuckets().stream()
|
||||||
|
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
|
||||||
|
.collect(toList());
|
||||||
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
|
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE)
|
||||||
|
.prefixLength(3)).build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.size());
|
||||||
|
@ -169,7 +184,9 @@ public class ElasticSearchQueryIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
|
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(multiMatchQuery("tutorial").field("title").field("tags")
|
||||||
|
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||||
assertEquals(2, articles.size());
|
assertEquals(2, articles.size());
|
||||||
|
|
Loading…
Reference in New Issue