PAN-11 corrected build.gradle inter-project dependencies
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import edu.msudenver.tsp.services.dto.Definition;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.AdditionalMatchers.not;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ServiceTestConfig.class)
|
||||
public class DefinitionServiceIntegrationTest {
|
||||
@Autowired private DefinitionService definitionService;
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition() {
|
||||
final Definition testDefinition = createDefinition();
|
||||
final Optional<Definition> createdDefinition = definitionService.createNewDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertTrue(createdDefinition.isPresent());
|
||||
assertThat(createdDefinition.get().getId(), is(not(0)));
|
||||
assertThat(createdDefinition.get().getVersion(), is(0));
|
||||
assertThat(createdDefinition.get().getName(), is("Test Name"));
|
||||
assertNotNull(createdDefinition.get().getDefinition());
|
||||
assertThat(createdDefinition.get().getDefinition().size(), is(1));
|
||||
assertThat(createdDefinition.get().getDefinition().get(0), is("Test definition 1"));
|
||||
assertNotNull(createdDefinition.get().getNotation());
|
||||
assertThat(createdDefinition.get().getNotation().size(), is(1));
|
||||
assertThat(createdDefinition.get().getNotation().get(0), is("\\testLaTeX"));
|
||||
}
|
||||
|
||||
private Definition createDefinition() {
|
||||
final List<String> definitionList = new ArrayList<>();
|
||||
definitionList.add("Test definition 1");
|
||||
|
||||
final List<String> notationList = new ArrayList<>();
|
||||
notationList.add("\\testLaTeX");
|
||||
|
||||
final Definition definition = new Definition();
|
||||
definition.setName("Test Name");
|
||||
definition.setDefinition(definitionList);
|
||||
definition.setNotation(notationList);
|
||||
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
|
||||
@TestPropertySource("classpath:test.properties")
|
||||
public class ServiceTestConfig {
|
||||
|
||||
@Bean
|
||||
@Autowired public DefinitionService definitionService(final RestService restService) {
|
||||
return new DefinitionService(restService);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
persistence.api.connection.timeout.milliseconds = 5000
|
||||
persistence.api.socket.timeout.milliseconds = 10000
|
||||
persistence.api.base.url = http://localhost:8090/
|
||||
@@ -0,0 +1,59 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import edu.msudenver.tsp.services.dto.Definition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
|
||||
public class DefinitionService {
|
||||
private final RestService restService;
|
||||
@Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds;
|
||||
@Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds;
|
||||
@Value("${persistence.api.base.url}") private String persistenceApiBaseUrl;
|
||||
|
||||
@Autowired
|
||||
public DefinitionService(final RestService restService) {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Definition> createNewDefinition(final Definition definition) {
|
||||
if (definition == null) {
|
||||
LOG.error("Given null definition, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Definition> definitionTypeToken = new TypeToken<Definition>() {};
|
||||
final Optional<Definition> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/",
|
||||
new GsonBuilder().create().toJson(definition),
|
||||
definitionTypeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if(persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to create new definition {}", definition.toString());
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error creating new definition {}", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Create new definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
public class ServiceConfig {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package edu.msudenver.tsp.services.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Definition extends BaseDto implements Serializable {
|
||||
@NotBlank(groups = Insert.class, message = "A name must be specified")
|
||||
@Size(min = 1, max = 200, message = "Must be between 1 and 200 characters")
|
||||
private String name;
|
||||
@NotBlank(groups = Insert.class, message = "At least one (1) definition must be specified")
|
||||
private List<String> definition;
|
||||
private List<String> notation;
|
||||
|
||||
private static final long serialVersionUID = 3412178112996807691L;
|
||||
|
||||
public interface Insert {}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import edu.msudenver.tsp.services.dto.Definition;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefinitionServiceTest {
|
||||
@Mock private RestService restService;
|
||||
@InjectMocks private DefinitionService definitionService;
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition() {
|
||||
final Definition testDefinition = createDefinition();
|
||||
final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition);
|
||||
|
||||
when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt()))
|
||||
.thenReturn(Optional.of(testDefinition));
|
||||
|
||||
final Optional<Definition> createdDefinition = definitionService.createNewDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertTrue(createdDefinition.isPresent());
|
||||
assertEquals(testDefinition, createdDefinition.get());
|
||||
verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition_unableToCreateNewDefinition() {
|
||||
final Definition testDefinition = createDefinition();
|
||||
final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition);
|
||||
|
||||
when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt()))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
final Optional<Definition> createdDefinition = definitionService.createNewDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertFalse(createdDefinition.isPresent());
|
||||
verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition_restServiceThrowsException() {
|
||||
final Definition testDefinition = createDefinition();
|
||||
final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition);
|
||||
|
||||
when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt()))
|
||||
.thenThrow(new UnsupportedOperationException("test exception"));
|
||||
|
||||
final Optional<Definition> createdDefinition = definitionService.createNewDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertFalse(createdDefinition.isPresent());
|
||||
verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt());
|
||||
}
|
||||
|
||||
private Definition createDefinition() {
|
||||
final List<String> definitionList = new ArrayList<>();
|
||||
definitionList.add("Test definition 1");
|
||||
|
||||
final List<String> notationList = new ArrayList<>();
|
||||
notationList.add("\\testLaTeX");
|
||||
|
||||
final Definition definition = new Definition();
|
||||
definition.setName("Test Name");
|
||||
definition.setDefinition(definitionList);
|
||||
definition.setNotation(notationList);
|
||||
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user