View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.web.actions;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.when;
21  
22  import java.util.ArrayList;
23  
24  import net.sourceforge.stripes.action.ActionBeanContext;
25  import net.sourceforge.stripes.action.Message;
26  import net.sourceforge.stripes.action.Resolution;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.mybatis.jpetstore.domain.Cart;
31  
32  class CartActionBeanTest {
33  
34    private CartActionBean cartActionBean;
35    private ActionBeanContext mockContext;
36  
37    @BeforeEach
38    void setUp() {
39      cartActionBean = new CartActionBean();
40      cartActionBean.setCart(new Cart());
41  
42      // Mock ActionBeanContext to avoid NPE in setMessage()
43      mockContext = mock(ActionBeanContext.class);
44      when(mockContext.getMessages()).thenReturn(new ArrayList<Message>());
45      cartActionBean.setContext(mockContext);
46    }
47  
48    @Test
49    void constructorOutputNotNull() {
50      final CartActionBean actual = new CartActionBean();
51  
52      assertThat(actual).isNotNull();
53      assertThat(actual.getCart()).isNotNull();
54      assertThat(actual.getContext()).isNull();
55    }
56  
57    @Test
58    void getCartOutputNotNull() {
59      final CartActionBean bean = new CartActionBean();
60  
61      assertThat(bean.getCart()).isNotNull();
62    }
63  
64    @Test
65    void addItemToCart_WithNullWorkingItemId_ShouldReturnError() {
66      cartActionBean.setWorkingItemId(null);
67  
68      Resolution resolution = cartActionBean.addItemToCart();
69  
70      assertThat(resolution).isNotNull();
71      assertThat(resolution.toString()).contains("Error.jsp");
72    }
73  
74    @Test
75    void addItemToCart_WithEmptyWorkingItemId_ShouldReturnError() {
76      cartActionBean.setWorkingItemId("");
77  
78      Resolution resolution = cartActionBean.addItemToCart();
79  
80      assertThat(resolution).isNotNull();
81      assertThat(resolution.toString()).contains("Error.jsp");
82    }
83  
84    @Test
85    void addItemToCart_WithBlankWorkingItemId_ShouldReturnError() {
86      cartActionBean.setWorkingItemId("   ");
87  
88      Resolution resolution = cartActionBean.addItemToCart();
89  
90      assertThat(resolution).isNotNull();
91      assertThat(resolution.toString()).contains("Error.jsp");
92    }
93  
94    @Test
95    void removeItemFromCart_WithNullWorkingItemId_ShouldReturnError() {
96      cartActionBean.setWorkingItemId(null);
97  
98      Resolution resolution = cartActionBean.removeItemFromCart();
99  
100     assertThat(resolution).isNotNull();
101     assertThat(resolution.toString()).contains("Error.jsp");
102   }
103 
104   @Test
105   void removeItemFromCart_WithEmptyWorkingItemId_ShouldReturnError() {
106     cartActionBean.setWorkingItemId("");
107 
108     Resolution resolution = cartActionBean.removeItemFromCart();
109 
110     assertThat(resolution).isNotNull();
111     assertThat(resolution.toString()).contains("Error.jsp");
112   }
113 
114   @Test
115   void removeItemFromCart_WithBlankWorkingItemId_ShouldReturnError() {
116     cartActionBean.setWorkingItemId("   ");
117 
118     Resolution resolution = cartActionBean.removeItemFromCart();
119 
120     assertThat(resolution).isNotNull();
121     assertThat(resolution.toString()).contains("Error.jsp");
122   }
123 
124   @Test
125   void removeItemFromCart_WithNonExistentItem_ShouldReturnError() {
126     cartActionBean.setWorkingItemId("NON_EXISTENT_ITEM");
127 
128     Resolution resolution = cartActionBean.removeItemFromCart();
129 
130     assertThat(resolution).isNotNull();
131     assertThat(resolution.toString()).contains("Error.jsp");
132   }
133 
134   @Test
135   void clearShouldResetCartAndWorkingItemId() {
136     cartActionBean.setWorkingItemId("EST-1");
137 
138     cartActionBean.clear();
139 
140     assertThat(cartActionBean.getCart()).isNotNull();
141     assertThat(cartActionBean.getCart().getNumberOfItems()).isZero();
142   }
143 }