Skip to content

Commit 53afd1f

Browse files
committed
Fixed tet coverage and fixed some edge cases
1 parent f833ad3 commit 53afd1f

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

two-step-view/src/main/java/com/iluwatar/Book.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,25 @@
2323
* THE SOFTWARE.
2424
*/
2525
package com.iluwatar;
26-
import lombok.AllArgsConstructor;
2726
import lombok.Data;
2827

2928
/** Represents a Book. */
30-
@AllArgsConstructor
3129
@Data
3230
public class Book {
3331
String name;
3432
double price;
3533
boolean discount;
3634
int stock;
35+
Book(String name, double price, boolean discount, int stock) {
36+
if (!name.isEmpty()) { // Make sure is not empty
37+
this.name = name;
38+
}
39+
if (price > 0) { // Make sure price is set with a realistic value
40+
this.price = price;
41+
}
42+
if (stock >= 0) { // Make sure stock is not negative
43+
this.stock = stock;
44+
}
45+
this.discount = discount;
46+
}
3747
}

two-step-view/src/main/java/com/iluwatar/BookStore.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ public class BookStore {
3535
double price;
3636
double discountPrice; // Calculates price if the book is discounted
3737
boolean inStock; // Indicates if the book is in stock
38+
3839
}

two-step-view/src/test/java/com/iluwatar/BookStoreTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import static org.junit.Assert.*;
66

77

8+
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
import java.util.logging.Logger;
12+
13+
814
public class BookStoreTest {
915

1016
@Test
@@ -40,4 +46,68 @@ public void testWithOutOfStock() {
4046
assertFalse(bookStoreOutOfStock.isInStock());
4147
}
4248

49+
@Test
50+
public void testPrepareZeroPrice() {
51+
Book book = new Book("Zero Price Book", 0.00, true, 5);
52+
BookStore bookStore = DataPreparation.prepareBook(book);
53+
54+
assertNotNull(bookStore);
55+
assertEquals(0.00, bookStore.getDiscountPrice(), 0.01);
56+
assertTrue(bookStore.isInStock());
57+
}
58+
59+
@Test
60+
public void testPrepareNegativeStock() {
61+
Book book = new Book("Negative Stock Book", 10.00, false, -1);
62+
BookStore bookStore = DataPreparation.prepareBook(book);
63+
64+
assertNotNull(bookStore);
65+
assertFalse(bookStore.isInStock());
66+
}
67+
68+
@Test
69+
public void testPresentInStock() {
70+
BookStore bookInStock = new BookStore("Wonder Woman Vol. 1", 15.00, 12.00, true);
71+
String htmlOutput = Presentation.presentBook(bookInStock);
72+
73+
assertNotNull(htmlOutput);
74+
assertTrue(htmlOutput.contains("<h1>Wonder Woman Vol. 1</h1>"));
75+
assertTrue(htmlOutput.contains("<p>Price: $15.0</p>"));
76+
assertTrue(htmlOutput.contains("<p>Discounted Price: $12.0</p>"));
77+
assertTrue(htmlOutput.contains("<p>Status: In Stock</p>"));
78+
}
79+
80+
@Test
81+
public void testPresentOutOfStock() {
82+
BookStore bookOutOfStock = new BookStore("Wonder Woman Vol. 1", 15.00, 12.00, false);
83+
String htmlOutput = Presentation.presentBook(bookOutOfStock);
84+
85+
assertNotNull(htmlOutput);
86+
assertTrue(htmlOutput.contains("<h1>Wonder Woman Vol. 1</h1>"));
87+
assertTrue(htmlOutput.contains("<p>Price: $15.0</p>"));
88+
assertTrue(htmlOutput.contains("<p>Discounted Price: $12.0</p>"));
89+
assertTrue(htmlOutput.contains("<p>Status: Out of Stock</p>"));
90+
}
91+
92+
@Test
93+
public void testMain() {
94+
// Redirect System.out to capture logger output
95+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
96+
PrintStream printStream = new PrintStream(outputStream);
97+
Logger.getLogger("").addHandler(new java.util.logging.ConsoleHandler() {
98+
{
99+
setOutputStream(printStream);
100+
}
101+
});
102+
103+
// Run the main method
104+
App.main(new String[]{});
105+
106+
// Check output contains expected HTML
107+
String loggedOutput = outputStream.toString();
108+
assertTrue(loggedOutput.contains("<h1>Batman Vol. 1: The Court of Owls</h1>"));
109+
assertTrue(loggedOutput.contains("<p>Price: $11.6</p>"));
110+
assertTrue(loggedOutput.contains("<p>Discounted Price: $8.7</p>"));
111+
assertTrue(loggedOutput.contains("<p>Status: In Stock</p>"));
112+
}
43113
}

0 commit comments

Comments
 (0)