PAN-11 created the deleteDefinition method

This commit is contained in:
2019-03-24 22:27:27 -06:00
parent 5a44b5b793
commit 8724d6c2f8
2 changed files with 46 additions and 2 deletions
@@ -6,6 +6,7 @@ import edu.msudenver.tsp.services.dto.Definition;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
@@ -97,7 +98,7 @@ public class DefinitionService {
socketTimeoutMilliseconds); socketTimeoutMilliseconds);
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Creation successful. Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to create new definition {}", definition); LOG.info("Unable to create new definition {}", definition);
} }
@@ -134,7 +135,7 @@ public class DefinitionService {
socketTimeoutMilliseconds); socketTimeoutMilliseconds);
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Update successful. Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to update definition {}", definition); LOG.info("Unable to update definition {}", definition);
} }
@@ -147,4 +148,39 @@ public class DefinitionService {
LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
} }
} }
public boolean deleteDefinition(final Definition definition) {
if (definition == null) {
LOG.error("Given null definition, returning false");
return false;
}
if (definition.getId() == 0) {
LOG.error("Given invalid id 0, returning false");
return false;
}
LOG.info("Sending request to delete definition {}", definition);
final Instant start = Instant.now();
try {
final boolean deleteIsSuccessful = restService.delete(persistenceApiBaseUrl + "/" + definition.getId(),
connectionTimeoutMilliseconds,
socketTimeoutMilliseconds,
HttpStatus.NO_CONTENT);
if (deleteIsSuccessful) {
LOG.info("Deletion successful. Returning true");
} else {
LOG.info("Unable to delete definition {}", definition);
}
return deleteIsSuccessful;
} catch (final Exception e) {
LOG.error("Error when deleting definition {}", e);
return false;
} finally {
LOG.info("Delete definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
}
}
} }
@@ -124,6 +124,14 @@ public class DefinitionServiceTest {
verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt()); verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt());
} }
@Test
public void testCreateDefinition_NullDefinition() {
final Optional<Definition> nullDefinition = definitionService.createDefinition(null);
assertFalse(nullDefinition.isPresent());
verifyZeroInteractions(restService);
}
@Test @Test
public void testCreateDefinition_UnableToCreateDefinition() { public void testCreateDefinition_UnableToCreateDefinition() {
final Definition testDefinition = createDefinition(); final Definition testDefinition = createDefinition();