forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-8232] - Moved non-string related code to core-java and copied P…
…izzaStatusEnum class and PizzaUnitTest relavent TCs to java-strings module
- Loading branch information
Showing
8 changed files
with
147 additions
and
76 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions
100
core-java/src/test/java/com/baeldung/enums/PizzaUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.baeldung.enums; | ||
|
||
import com.baeldung.enums.Pizza.PizzaStatusEnum; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class PizzaUnitTest { | ||
|
||
@Test | ||
public void givenPizaOrder_whenReady_thenDeliverable() { | ||
Pizza testPz = new Pizza(); | ||
testPz.setStatus(Pizza.PizzaStatusEnum.READY); | ||
assertTrue(testPz.isDeliverable()); | ||
} | ||
|
||
@Test | ||
public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() { | ||
List<Pizza> pzList = new ArrayList<>(); | ||
Pizza pz1 = new Pizza(); | ||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED); | ||
|
||
Pizza pz2 = new Pizza(); | ||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED); | ||
|
||
Pizza pz3 = new Pizza(); | ||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED); | ||
|
||
Pizza pz4 = new Pizza(); | ||
pz4.setStatus(Pizza.PizzaStatusEnum.READY); | ||
|
||
pzList.add(pz1); | ||
pzList.add(pz2); | ||
pzList.add(pz3); | ||
pzList.add(pz4); | ||
|
||
List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList); | ||
assertTrue(undeliveredPzs.size() == 3); | ||
} | ||
|
||
@Test | ||
public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() { | ||
|
||
List<Pizza> pzList = new ArrayList<>(); | ||
Pizza pz1 = new Pizza(); | ||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED); | ||
|
||
Pizza pz2 = new Pizza(); | ||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED); | ||
|
||
Pizza pz3 = new Pizza(); | ||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED); | ||
|
||
Pizza pz4 = new Pizza(); | ||
pz4.setStatus(Pizza.PizzaStatusEnum.READY); | ||
|
||
pzList.add(pz1); | ||
pzList.add(pz2); | ||
pzList.add(pz3); | ||
pzList.add(pz4); | ||
|
||
EnumMap<Pizza.PizzaStatusEnum, List<Pizza>> map = Pizza.groupPizzaByStatus(pzList); | ||
assertTrue(map.get(Pizza.PizzaStatusEnum.DELIVERED).size() == 1); | ||
assertTrue(map.get(Pizza.PizzaStatusEnum.ORDERED).size() == 2); | ||
assertTrue(map.get(Pizza.PizzaStatusEnum.READY).size() == 1); | ||
} | ||
|
||
@Test | ||
public void whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() { | ||
Pizza pz = new Pizza(); | ||
pz.setStatus(Pizza.PizzaStatusEnum.READY); | ||
pz.deliver(); | ||
assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED); | ||
} | ||
|
||
@Test | ||
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() { | ||
String pizzaEnumValue = "READY"; | ||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue); | ||
assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void whenConvertedIntoEnum_thenThrowsException() { | ||
String pizzaEnumValue = "rEAdY"; | ||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void givenInvalidEnumValueContentWiseAsString_whenConvertedIntoEnum_thenThrowsException() { | ||
String pizzaEnumValue = "invalid"; | ||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue); | ||
} | ||
|
||
|
||
} |
File renamed without changes.
44 changes: 44 additions & 0 deletions
44
java-strings/src/main/java/com/baeldung/enums/PizzaStatusEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.baeldung.enums; | ||
|
||
public enum PizzaStatusEnum { | ||
ORDERED(5) { | ||
@Override | ||
public boolean isOrdered() { | ||
return true; | ||
} | ||
}, | ||
READY(2) { | ||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
}, | ||
DELIVERED(0) { | ||
@Override | ||
public boolean isDelivered() { | ||
return true; | ||
} | ||
}; | ||
|
||
private int timeToDelivery; | ||
|
||
public boolean isOrdered() { | ||
return false; | ||
} | ||
|
||
public boolean isReady() { | ||
return false; | ||
} | ||
|
||
public boolean isDelivered() { | ||
return false; | ||
} | ||
|
||
public int getTimeToDelivery() { | ||
return timeToDelivery; | ||
} | ||
|
||
PizzaStatusEnum(int timeToDelivery) { | ||
this.timeToDelivery = timeToDelivery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters