PAN-11 wrote the GetAllDefinitions() method
This commit is contained in:
+4
-4
@@ -6,7 +6,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -16,16 +16,16 @@ import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.AdditionalMatchers.not;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = ServiceTestConfig.class)
|
||||
@TestPropertySource(locations = "classpath:test.properties")
|
||||
public class DefinitionServiceIntegrationTest {
|
||||
@Autowired private DefinitionService definitionService;
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition() {
|
||||
public void testCreateDefinition() {
|
||||
final Definition testDefinition = createDefinition();
|
||||
final Optional<Definition> createdDefinition = definitionService.createNewDefinition(testDefinition);
|
||||
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertTrue(createdDefinition.isPresent());
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
public class ServiceTestConfig {
|
||||
|
||||
@Bean
|
||||
@Autowired public DefinitionService definitionService(final RestService restService) {
|
||||
@Autowired
|
||||
public DefinitionService definitionService(final RestService restService) {
|
||||
return new DefinitionService(restService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@@ -25,25 +26,51 @@ public class DefinitionService {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Definition> createNewDefinition(final Definition definition) {
|
||||
public Optional<List<Definition>> getAllDefinitions() {
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<List<Definition>> typeToken = new TypeToken<List<Definition>>(){};
|
||||
final Optional<List<Definition>> persistenceApiResponse =
|
||||
restService.get(persistenceApiBaseUrl + "definitions/",
|
||||
typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to get list of definitions");
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error getting list of definitions! {}", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Get all definitions request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Definition> createDefinition(final Definition definition) {
|
||||
if (definition == null) {
|
||||
LOG.error("Given null definition, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
LOG.info("Sending request to insert definition {}", definition);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Definition> definitionTypeToken = new TypeToken<Definition>() {};
|
||||
final Optional<Definition> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/",
|
||||
final Optional<Definition> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "definitions/",
|
||||
new GsonBuilder().create().toJson(definition),
|
||||
definitionTypeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if(persistenceApiResponse.isPresent()) {
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to create new definition {}", definition.toString());
|
||||
LOG.info("Unable to create new definition {}", definition);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
|
||||
@@ -24,14 +24,14 @@ public class DefinitionServiceTest {
|
||||
@InjectMocks private DefinitionService definitionService;
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition() {
|
||||
public void testCreateDefinition() {
|
||||
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);
|
||||
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertTrue(createdDefinition.isPresent());
|
||||
@@ -40,14 +40,14 @@ public class DefinitionServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition_unableToCreateNewDefinition() {
|
||||
public void testCreateDefinition_unableToCreateDefinition() {
|
||||
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);
|
||||
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertFalse(createdDefinition.isPresent());
|
||||
@@ -55,14 +55,14 @@ public class DefinitionServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNewDefinition_restServiceThrowsException() {
|
||||
public void testCreateDefinition_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);
|
||||
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
||||
|
||||
assertNotNull(createdDefinition);
|
||||
assertFalse(createdDefinition.isPresent());
|
||||
|
||||
Reference in New Issue
Block a user