PAN-15 Fixed unit tests

This commit is contained in:
dantanxiaotian
2019-03-24 14:54:06 -06:00
parent 9695f5082a
commit 89f1c55f51
5 changed files with 79 additions and 32 deletions
@@ -56,19 +56,21 @@ public class UserService {
}
}
public Optional<Account> updatePassword(final Account account , final String password){
if(account ==null){
LOG.error("user not exist, returning{}");
public Optional<Account> updateAccount(final Account account) {
if (account == null) {
LOG.error("User does not exist, returning {}");
return Optional.empty();
}
final Integer id = account.getId();
account.setPassword(password);
if (account.getId() == 0) {
LOG.error("No user ID specified! Returning {}");
return Optional.empty();
}
final int id = account.getId();
final Instant start = Instant.now();
try{
final String auth = "";
try {
final TypeToken<Account> typeToken = new TypeToken<Account>(){};
final Optional<Account> persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/" + id,
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
@@ -79,15 +81,15 @@ public class UserService {
if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get());
} else {
LOG.info("Unable to update password for account {}",account.toString());
LOG.info("Unable to update user {} with id", account.getId());
}
return persistenceApiResponse;
} catch (final Exception e) {
LOG.error("Error updating account {}", e);
LOG.error("Error updating user {}", e);
return Optional.empty();
} finally {
LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis());
LOG.info("Update user request took {} ms", Duration.between(start, Instant.now()).toMillis());
}
}
@@ -0,0 +1,47 @@
package edu.msudenver.tsp.services;
import com.google.gson.reflect.TypeToken;
import edu.msudenver.tsp.services.dto.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.text.ParseException;
import java.util.Date;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {
@Mock
private RestService restService;
@InjectMocks
private UserService userService;
@Test
public void testCreateNewAccount() throws ParseException {
final Account account = new Account();
account.setUsername("Test username");
account.setPassword("test password");
account.setAdministratorStatus(false);
account.setLastLogin(new Date());
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()))
.thenReturn(Optional.of(account));
final Optional<Account> response = userService.createNewAccount(account);
assertTrue(response.isPresent());
assertEquals(account, response.get());
}
}