diff options
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java | 22 | ||||
| -rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java | 9 | 
2 files changed, 14 insertions, 17 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 6dd120c..78f8f85 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -29,22 +29,20 @@ public class CupboardService {       * @throws DuplicateKeyException If there already exists a need with the same name       */      public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { -         -        Need need = new Need(name, goalType, maxGoal); -        if (need.getMaxGoal() <= 0) { +        if (maxGoal <= 0) {              throw new IllegalArgumentException("Max Goal must be greater than zero"); -        } else { -            if (cupboardDAO.getNeeds().length > 0) { -                for (Need searchNeed : cupboardDAO.getNeeds()) { -                    if (need.getName().equalsIgnoreCase(searchNeed.getName())) { -                        throw new DuplicateKeyException("Duplicate names are not allowed"); -                    } -                } +        } + +        for (Need searchNeed : cupboardDAO.getNeeds()) { +            if (searchNeed.getName().equalsIgnoreCase(name)) { +                throw new DuplicateKeyException("Duplicate names are not allowed");              } -            return cupboardDAO.addNeed(need);          } -         + +        Need need = new Need(name, goalType, maxGoal); +        return cupboardDAO.addNeed(need); +      }      /** diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java index 09d7c89..ceef215 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java @@ -1,15 +1,14 @@  package com.ufund.api.ufundapi.service;  import java.io.IOException; -import java.util.TreeMap;  import static org.junit.jupiter.api.Assertions.assertEquals;  import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.*; +  import org.junit.jupiter.api.BeforeEach;  import org.junit.jupiter.api.Tag;  import org.junit.jupiter.api.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when;  import com.ufund.api.ufundapi.DuplicateKeyException;  import com.ufund.api.ufundapi.model.Need; @@ -24,7 +23,7 @@ public class CupboardServiceTest {      private CupboardService cupboardService;      @BeforeEach -    public void setupCupboardService() throws IOException { +    public void setupCupboardService() {          mockCupboardDAO = mock(CupboardDAO.class);          mockAuthService = mock(AuthService.class);          cupboardService = new CupboardService(mockCupboardDAO); @@ -42,7 +41,7 @@ public class CupboardServiceTest {          // When the same id is passed in, our mock User DAO will return the User object          when(mockCupboardDAO.getNeed(id)).thenReturn(need); -        when(mockCupboardDAO.addNeed(need)).thenReturn(need); +        when(mockCupboardDAO.addNeed(any())).thenReturn(need);          when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);  | 
