Consul discoveryClient in test - consul

I am using trying to setup project with Consul. I can start my SpringBoot with Consul running. But how can I do test in Maven build? The build server does not have Consul install and running
package com.example.demo2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
#SpringBootTest
class Demo2ApplicationTests {
#Autowired
private ConsulDiscoveryClient discoveryClient;
#Test
void contextLoads() {
}
#Test
public void testClient() {
List<ServiceInstance> instances = this.discoveryClient.getInstances("testConsulDiscovery");
assertThat(instances).as("instances was null").isNotNull();
assertThat(instances.isEmpty()).as("instances was empty").isFalse();
ServiceInstance instance = instances.get(0);
assertThat(instance.isSecure()).as("instance was secure (https)").isFalse();
assertIpAddress(instance);
assertThat(instance.getMetadata()).containsEntry("foo", "bar");
}
}
The following error was received
com.ecwid.consul.transport.TransportException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8500 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information

Related

'url' should start with a path or be a complete HTTP URL:

I did write an application that pulls customer info.
When I run the application in postman it works fine.
But when trying to run some initial tests but it gives bean error ,
The exact same configuration with the same annotations works fine in another component .
Thanks in advance
'url' should start with a path or be a complete HTTP URL: v1/customers/2503427
java.lang.IllegalArgumentException: 'url' should start with a path or be a complete HTTP URL: v1/customers/2503427
package az.iba.ms.customer.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
#RunWith(SpringRunner.class)
#AutoConfigureMockMvc
#SpringBootTest
public class CustomerControllerTest {
String endpoint = "v1/customers/";
String cifs = "2503427";
#Autowired
private MockMvc mockMvc;
#Autowired
private CustomerController customerController;
#Test
public void controllerInitializedCorrectly() {
Assertions.assertThat(customerController).isNotNull();
}
#Test
public void whenValidInput_providedToCustomerQueryThenReturns200() throws Exception {
mockMvc.perform(get(endpoint + cifs)
.contentType("application/json"))
.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK.value()));
}
#Test
void whenValidNotInput_providedToCustomerQueryThenReturns400() throws Exception {
mockMvc.perform(get(endpoint)
.contentType("application/json"))
.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST.value()));
}
#Test
void whenValidNotMethod_providedToCustomerQueryThenReturns405() throws Exception {
mockMvc.perform(post(endpoint + cifs)
.contentType("application/json"))
.andExpect(MockMvcResultMatchers.status().is(HttpStatus.METHOD_NOT_ALLOWED.value()));
}
}
I was fixing the same error now... Error arise because endpoint must start with /.
Change your's variable endpoint from v1/customers/ to /v1/customers/.

Unable to fetch random port when server.port is 0

I need to fetch port number on which undertow was Started by my Sprint boot app. I have defined server.port=0 in application.properties. I cannot use fix port numbers like 8080.
package com.aggregate.application;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
#Configuration
#ComponentScan(basePackages = {"com.aggregate"})
#EnableAutoConfiguration
public class GServiceApplication extends SpringBootServletInitializer
implements ApplicationListener<ApplicationReadyEvent> {
#Autowired
private ApplicationContext applicationContext;
#Override
public void onApplicationEvent(ApplicationReadyEvent event) {
try {
String ip = InetAddress.getLocalHost().getHostAddress();
String port = applicationContext.getBean(Environment.class).getProperty("server.port");
System.out.printf("ip:port=" +ip+ ":"+port);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws UnknownHostException
{
SpringApplication application = new SpringApplication(GServiceApplication.class);
Properties properties = new Properties();
properties.put("server.port", 0);
properties.put("server.address", InetAddress.getLocalHost().getHostAddress());
application.setDefaultProperties(properties);
application.run(args);
}
}
Undertow started:- o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 55646 (http) with context path '' as printed in console
Expected result:- ip:port=xx.xx.x.1x1:55646
Actual result:- ip:port=xx.xx.x.1x1:0
Passing in the port number 0 is a trick that the Java core ServerSocket class can do. Undertow isn't aware of this; it just assumes that the port is always fixed. So there's no official API to read the port number that is actually used; but if you find the Undertow object, you can do:
// assuming you only have one:
ListenerInfo listenerInfo = undertow.getListenerInfo().iterator().next();
InetSocketAddress socketAddress = (InetSocketAddress) listenerInfo.getAddress();
URI uri = URI.create(listenerInfo.getProtcol() + "://" + socketAddress.getHostString() + ":" + socketAddress.getPort());
HTH

Reliable libraries out there for Spring boot redis integration tests

This is more of a question for a tool - googling around I haven't really had much luck.
So basically I have a standard spring boot app - and I have a unit test redis cache configuration. What I am looking to do is run the app context autowire some spring configs and test against a embedded redis cache if possible.
Closest I have come is this https://github.com/kstyrc/embedded-redis.
Problem with that is the lack of robust logging is making it difficult to run - its working locally, but when I push it up, Unix server build machine, its failing and no idea why.
If anyone has any idea of how to run integration tests this way - it would be great.
thanks,
Stefan
I am using embedded-redis for my integration testing with redisson java client.
Here is my dependency
compile group: 'org.redisson', name: 'redisson', version: '3.6.5'
testCompile group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2'
Start embedded redis server before class and stop it in after class.
Redis property:
spring.redis.host=localhost
spring.redis.port=6379
Sample integration test.
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import redis.embedded.RedisServer;
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class RedisTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisTest.class);
private static RedisServer REDISSERVER = new RedisServer(6379);
#LocalServerPort
private int port;
#Autowired
private RedissonClient redissonClient;
#BeforeClass
public static final void before() {
REDISSERVER.start();
}
#AfterClass
public static final void after() {
REDISSERVER.stop();
}
#Test
public void testRedis() throws InterruptedException {
//map
RMap<String, String> map = redissonClient.getMap("user");
map.put("name", "Redis Server");
Assert.assertTrue(map.get("name").equals("Redis Server"));
//mapcache
RMapCache<String, String> mapCache = redissonClient.getMapCache("tempUser");
mapCache.put("name", "Redis Server", 5, TimeUnit.SECONDS);
Assert.assertTrue(mapCache.get("name").equals("Redis Server"));
Thread.sleep(7000); //wait for 7 sec.
Assert.assertTrue(mapCache.get("name") == null);
}
}

Spring Boot Websocket Throws "Connection Refused" in SockJS Client Test

I am working on a Spring Boot Server Project which offered simple REST resources until now. In order to push notifications to the client I want to add a websocket connection. To test this connection I have written a Integration Test using a SockJS Client based on this tutorial :
http://rafaelhz.github.io/testing-websockets/
Problem is that the Connection is refused with the following error:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:9090/websocket/info": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
My Websocket Configuration is as follows:
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/websocket")
.setAllowedOrigins("*")
.withSockJS();
}
}
I can see in the that the socket endpoint is mapped int the log:
2017-07-14 15:22:59.561 INFO 13765 --- [ main] o.s.w.s.s.s.WebSocketHandlerMapping : Mapped URL path [/websocket/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler]
The Server Port is set to 9090 in the application.yml file:
server:
port: 9090
The following unit test is not able to connect to the socket:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import java.lang.reflect.Type;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles("test")
//#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class WebSocketConnectionTest {
static final String WEBSOCKET_URI = "ws://localhost:9090/websocket";
static final String WEBSOCKET_TOPIC = "/topic";
BlockingQueue<String> blockingQueue;
WebSocketStompClient stompClient;
#Before
public void setup() {
blockingQueue = new LinkedBlockingDeque<>();
stompClient = new WebSocketStompClient(new SockJsClient(
asList(new WebSocketTransport(new StandardWebSocketClient()))));
System.out.println(WEBSOCKET_URI);
}
#Test
public void shouldReceiveAMessageFromTheServer() throws Exception {
StompSession session = stompClient
.connect(WEBSOCKET_URI, new StompSessionHandlerAdapter() {})
.get(1, SECONDS);
session.subscribe(WEBSOCKET_TOPIC, new DefaultStompFrameHandler());
String message = "MESSAGE TEST";
session.send(WEBSOCKET_TOPIC, message.getBytes());
Assert.assertEquals(message, blockingQueue.poll(1, SECONDS));
}
class DefaultStompFrameHandler implements StompFrameHandler {
#Override
public Type getPayloadType(StompHeaders stompHeaders) {
return byte[].class;
}
#Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
blockingQueue.offer(new String((byte[]) o));
}
}
}
The connection is refused. Im fairly certain that this happens because the URI endpoint does not exist, but I don't know why. Does somebody know if there is a error in the URI or if something else leads to the refused connection ?
I found out the cause of the problem. The endpoint did not exist on PORT 9090. That is because the #SpringBootTest Annotation sets the WebEnvironment to WebEnvironment.MOCK by default. In this configuration No Embedded Servlet is started and therefor and no port exists, only MockMvc-based testing is possible. In order to start an Embedded servlet
the Environment has to be set to WebEnvironment.RANDOM_PORT or WebEnvironment.DEFINED_PORT. I set it to DEFINED_PORT so that the port 9090 from my application.yml is used. By Setting the Environment the test runs correctly.
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)//!!!!!
#ActiveProfiles("test")
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class WebSocketConnectionTest {
String WEBSOCKET_URI = "ws://localhost:9090/websocket";
String WEBSOCKET_TOPIC = "/topic";
.
.
.

Integration test with jersey and spring boot 1.4.0.RELEASE

I am trying to write integration test with jersey, Spring boot 1.4 and Spring data jpa.I am able to start embedded server but getting error from jersey side , any help will be appreciated.
Integration test
import static org.assertj.core.api.Assertions.assertThat;
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.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, classes=Application.class)
public class ContactServiceIT {
#Autowired
private TestRestTemplate restTemplate;
#Autowired
private ContactDao contactDao;
#Test
public void mergeContactsTest() {
String body = this.restTemplate.getForObject("/contacts/merge", String.class);
assertThat(body).isEqualTo("contacts merged");
}
}
Contact Resource
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
#Path("/contacts")
public class ContactResource {
#Autowired
private ContactService contactService;
#GET
#Path("merge")
public Response mergeContacts() throws IOException {
contactService.mergeContacts();
return Response.status(Response.Status.CREATED)
.entity("contacts merged").build();
}
}
Stack trace:
java.lang.NoSuchMethodError: org.glassfish.jersey.CommonProperties.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
at org.glassfish.jersey.jackson.JacksonFeature.configure(JacksonFeature.java:73) ~[jersey-media-json-jackson-2.23.1.jar:na]
at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:680) ~[jersey-common-2.7.jar:na]
at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:610) ~[jersey-common-2.7.jar:na]
at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:800) ~[jersey-server-2.7.jar:na]
Please let me know if I am missing something.
Thanks.

Resources