Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from microservices-demo/test/coverage
Browse files Browse the repository at this point in the history
Increase code coverage. Remove unnecessary config. Move files.
  • Loading branch information
Phil Winder authored Aug 11, 2016
2 parents 8006f5d + b89def0 commit 83b9c30
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 65 deletions.
45 changes: 0 additions & 45 deletions src/main/java/works/weave/socks/ShippingServiceApplication.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package works.weave.socks.shipping;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShippingServiceApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ShippingServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package works.weave.socks;
package works.weave.socks.shipping.configuration;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
Expand All @@ -12,10 +12,11 @@

@Configuration
public class RabbitMqConfiguration {
final static String queueName = "shipping-task";

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("rabbitmq");
// CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.99.104");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
Expand All @@ -37,4 +38,19 @@ public RabbitTemplate rabbitTemplate() {
template.setMessageConverter(jsonMessageConverter());
return template;
}

@Bean
Queue queue() {
return new Queue(queueName, false);
}

@Bean
TopicExchange exchange() {
return new TopicExchange("shipping-task-exchange");
}

@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package works.weave.socks;
package works.weave.socks.shipping.controllers;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import works.weave.socks.shipping.entities.Shipment;

@RestController
public class ShippingController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package works.weave.socks;
package works.weave.socks.shipping.entities;

import java.util.UUID;

Expand Down Expand Up @@ -27,6 +27,22 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Shipment shipment = (Shipment) o;

return getId() != null ? getId().equals(shipment.getId()) : shipment.getId() == null;

}

@Override
public int hashCode() {
return getId() != null ? getId().hashCode() : 0;
}

public String getId() {
return id;
}
Expand Down
15 changes: 0 additions & 15 deletions src/test/java/works/weave/socks/UnitShipment.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package works.weave.socks.shipping.controllers;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import works.weave.socks.shipping.entities.Shipment;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ITShippingController {
@MockBean
private RabbitTemplate rabbitTemplate;

@Autowired
private ShippingController shippingController;

@Test
public void getShipment() throws Exception {
String shipping = shippingController.getShipping();
assertThat(shipping, is(notNullValue()));
}

@Test
public void getShipmentById() throws Exception {
String shipping = shippingController.getShippingById("id");
assertThat(shipping, is(notNullValue()));
}

@Test
public void newShipment() throws Exception {
Shipment original = new Shipment("someName");
Shipment saved = shippingController.postShipping(original);
verify(rabbitTemplate, times(1)).convertAndSend(anyString(), any(Shipment.class));
assertThat(original, is(equalTo(saved)));
}

@Test
public void doNotCrashWhenNoQueue() throws Exception {
doThrow(new AmqpException("test error")).when(rabbitTemplate).convertAndSend(anyString(), any(Shipment.class));
Shipment original = new Shipment("someName");
Shipment saved = shippingController.postShipping(original);
verify(rabbitTemplate, times(1)).convertAndSend(anyString(), any(Shipment.class));
assertThat(original, is(equalTo(saved)));
}
}
77 changes: 77 additions & 0 deletions src/test/java/works/weave/socks/shipping/entities/UnitPojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package works.weave.socks.shipping.entities;

import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoClassFilter;
import com.openpojo.reflection.filters.FilterClassName;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.validation.Validator;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.affirm.Affirm;
import com.openpojo.validation.rule.impl.*;
import com.openpojo.validation.test.impl.GetterTester;
import com.openpojo.validation.test.impl.SetterTester;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class UnitPojo {
// Configured for expectation, so we know when a class gets added or removed.
private static final int EXPECTED_CLASS_COUNT = 1;

// The package to test
private static final String POJO_PACKAGE = "works.weave.socks.shipping.entities";

private final PojoClassFilter filter = new FilterClassName("^((?!Unit).)*$");

@Test
public void ensureExpectedPojoCount() {
List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(POJO_PACKAGE, filter);
Affirm.affirmEquals("Classes added / removed?", EXPECTED_CLASS_COUNT, pojoClasses.size());
}

@Test
public void testPojoStructureAndBehavior() {
Validator validator = ValidatorBuilder.create()
// Add Rules to validate structure for POJO_PACKAGE
// See com.openpojo.validation.rule.impl for more ...
.with(new GetterMustExistRule())
.with(new SetterMustExistRule())
// Add Testers to validate behaviour for POJO_PACKAGE
// See com.openpojo.validation.test.impl for more ...
.with(new SetterTester())
.with(new GetterTester())
// Static fields must be final
.with(new NoStaticExceptFinalRule())
// Don't shadow parent's field names.
.with(new NoFieldShadowingRule())
// What about public fields, use one of the following rules
// allow them only if they are static and final.
.with(new NoPublicFieldsExceptStaticFinalRule())
.build();

validator.validate(POJO_PACKAGE, filter);
}

@Test
public void testEquals() throws Exception {
assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "name"))));
assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "another"))));
assertThat(new Shipment("id", "name"), is(not(equalTo(new Shipment("another", "name")))));
}

@Test
public void testHashcode() throws Exception {
assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "name").hashCode())));
assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "another").hashCode())));
assertThat(new Shipment("id", "name").hashCode(), is(not(equalTo(new Shipment("aa", "name").hashCode()))));
}

@Test
public void testString() throws Exception {
assertThat(new Shipment("id").toString(), is(notNullValue()));

}
}

0 comments on commit 83b9c30

Please sign in to comment.