From 978b58fe2cf5991ebb5ce6b2f1465d2705ca3519 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Sun, 24 Mar 2019 20:48:25 -0600 Subject: [PATCH] PAN-11 wrote the GetAllDefinitions() method --- .../scripts/mysql/local_development.sql | 23 ++++++------ .../persistence/ProofsIntegrationTest.java | 3 +- .../DefinitionServiceIntegrationTest.java | 8 ++--- .../tsp/services/ServiceTestConfig.java | 9 ++--- .../tsp/services/DefinitionService.java | 35 ++++++++++++++++--- .../tsp/services/DefinitionServiceTest.java | 12 +++---- 6 files changed, 56 insertions(+), 34 deletions(-) diff --git a/persistence/scripts/mysql/local_development.sql b/persistence/scripts/mysql/local_development.sql index 27005d4..fbd9dac 100644 --- a/persistence/scripts/mysql/local_development.sql +++ b/persistence/scripts/mysql/local_development.sql @@ -34,17 +34,16 @@ referenced_theorems json, proven_status boolean default false, version int default 1 ); -CREATE TABLE proofs +create table proofs ( - id INT NOT NULL AUTO_INCREMENT, - theorem_name VARCHAR(512) NOT NULL, - proof VARCHAR(4096) NOT NULL, - branch VARCHAR(512) NOT NULL, - theorem INT NOT NULL, - referenced_definitions JSON, - referenced_theorems JSON, - date_added DATE, - last_updated DATE, - version INT DEFAULT 1, - PRIMARY KEY (id) + id int not null auto_increment primary key unique, + theorem_name varchar(512) not null, + proof varchar(4096) not null, + branch varchar(512) not null, + theorem int not null, + referenced_definitions json, + referenced_theorems json, + date_added date, + last_updated date, + version int default 1 ); \ No newline at end of file diff --git a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java index b18c47d..9fa7edb 100644 --- a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java +++ b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java @@ -18,8 +18,7 @@ import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = PersistenceTestConfig.class) public class ProofsIntegrationTest { - @Autowired - private ProofRepository proofRepository; + @Autowired private ProofRepository proofRepository; @Test public void testCRUDFunctionality() { diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java index 3e4bc99..7f424e3 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java @@ -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 createdDefinition = definitionService.createNewDefinition(testDefinition); + final Optional createdDefinition = definitionService.createDefinition(testDefinition); assertNotNull(createdDefinition); assertTrue(createdDefinition.isPresent()); diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/ServiceTestConfig.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/ServiceTestConfig.java index 93ae8e1..1d9d54e 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/ServiceTestConfig.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/ServiceTestConfig.java @@ -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); } } diff --git a/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java b/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java index 23b177a..f4bd91a 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java @@ -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 createNewDefinition(final Definition definition) { + public Optional> getAllDefinitions() { + final Instant start = Instant.now(); + + try { + final TypeToken> typeToken = new TypeToken>(){}; + final Optional> 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 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 definitionTypeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/", + final Optional 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; diff --git a/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java index b712ccd..673affd 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java @@ -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 createdDefinition = definitionService.createNewDefinition(testDefinition); + final Optional 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 createdDefinition = definitionService.createNewDefinition(testDefinition); + final Optional 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 createdDefinition = definitionService.createNewDefinition(testDefinition); + final Optional createdDefinition = definitionService.createDefinition(testDefinition); assertNotNull(createdDefinition); assertFalse(createdDefinition.isPresent());