1+ package com .example .potionsapi .controller ;
2+ import org .springframework .test .web .servlet .MockMvc ;
3+ import org .springframework .boot .test .mock .mockito .MockBean ;
4+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
5+ import org .junit .jupiter .api .Test ;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
7+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import com .example .potionsapi .service .PotionService ;
10+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
11+ import com .example .potionsapi .model .Potion ;
12+ import java .util .Arrays ;
13+ import java .util .List ;
14+ import java .util .UUID ;
15+ import static org .mockito .Mockito .when ;
16+ import static org .mockito .Mockito .verify ;
17+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
18+ import static org .hamcrest .Matchers .hasSize ;
19+ import com .fasterxml .jackson .databind .ObjectMapper ;
20+ import org .springframework .http .MediaType ;
21+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
22+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
23+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
24+ import static org .mockito .ArgumentMatchers .any ;
25+ import static org .mockito .ArgumentMatchers .eq ;
26+ import static org .assertj .core .api .Assertions .assertThat ;
27+ import org .mockito .ArgumentCaptor ;
28+
29+
30+
31+ @ WebMvcTest (PotionController .class )
32+ public class PotionControllerTests {
33+ // Add test methods here
34+
35+ // Test generated using Keploy
36+ @ Autowired
37+ private MockMvc mockMvc ;
38+
39+ @ MockBean
40+ private PotionService potionService ;
41+
42+ @ Test
43+ void shouldReturnHelloWorldHtml_whenHomePageEndpointCalled_Fix1 () throws Exception {
44+ // Arrange (No specific arrangement needed for this simple endpoint)
45+
46+ // Act & Assert
47+ mockMvc .perform (get ("/" ))
48+ .andExpect (status ().isOk ())
49+ .andExpect (content ().string ("<h1> Hello world </h1>" ));
50+ }
51+
52+ // Test generated using Keploy
53+ private com .fasterxml .jackson .databind .ObjectMapper objectMapper = new com .fasterxml .jackson .databind .ObjectMapper ();
54+
55+ @ Test
56+ void shouldReturnListOfPotions_whenGetAllPotionEndpointCalled_A1B2C3 () throws Exception {
57+ // Arrange
58+ Potion potion1 = new Potion ();
59+ potion1 .setId (UUID .randomUUID ());
60+ potion1 .setName ("Potion of Healing" );
61+ potion1 .setDescription ("Restores health" );
62+ // Assuming Potion model does not have setIngredients or getIngredients as per error
63+ // potion1.setIngredients("Water, Herb");
64+
65+ Potion potion2 = new Potion ();
66+ potion2 .setId (UUID .randomUUID ());
67+ potion2 .setName ("Potion of Strength" );
68+ potion2 .setDescription ("Increases strength" );
69+ // potion2.setIngredients("Blood, Root");
70+
71+ List <Potion > potions = Arrays .asList (potion1 , potion2 );
72+ when (potionService .getAllPotion ()).thenReturn (potions );
73+
74+ // Act & Assert
75+ mockMvc .perform (get ("/potions" ))
76+ .andExpect (status ().isOk ())
77+ .andExpect (jsonPath ("$" , hasSize (2 )))
78+ .andExpect (jsonPath ("$[0].name" ).value ("Potion of Healing" ))
79+ .andExpect (jsonPath ("$[1].name" ).value ("Potion of Strength" ));
80+
81+ verify (potionService ).getAllPotion ();
82+ }
83+
84+ // Test generated using Keploy
85+ @ Test
86+ void shouldReturnPotion_whenGetPotionByIdEndpointCalled_D3E4F5 () throws Exception {
87+ // Arrange
88+ UUID potionId = UUID .fromString ("8ab097b9-1a2f-46ab-8825-74313d9eb53c" );
89+ Potion potion = new Potion ();
90+ potion .setId (potionId );
91+ potion .setName ("Potion of Invisibility" );
92+ potion .setDescription ("Makes the drinker invisible" );
93+ // Assuming Potion model does not have setIngredients as per error
94+ // potion.setIngredients("Moon dew, Shadow silk");
95+
96+ when (potionService .getPotionById (potionId )).thenReturn (potion );
97+
98+ // Act & Assert
99+ mockMvc .perform (get ("/potions/" + potionId ))
100+ .andExpect (status ().isOk ())
101+ .andExpect (jsonPath ("$.id" ).value (potionId .toString ()))
102+ .andExpect (jsonPath ("$.name" ).value ("Potion of Invisibility" ))
103+ .andExpect (jsonPath ("$.description" ).value ("Makes the drinker invisible" ));
104+
105+ verify (potionService ).getPotionById (potionId );
106+ }
107+
108+
109+ // Test generated using Keploy
110+ @ Test
111+ void shouldCreatePotion_whenCreatePotionEndpointCalled_G5H6I7 () throws Exception {
112+ // Arrange
113+ Potion potionToCreate = new Potion ();
114+ potionToCreate .setName ("Mana Potion" );
115+ potionToCreate .setDescription ("Restores mana" );
116+ // Assuming Potion model does not have setIngredients or getIngredients as per error
117+ // potionToCreate.setIngredients("Crystal Water, Starflower");
118+
119+ Potion createdPotionWithId = new Potion ();
120+ UUID generatedId = UUID .randomUUID (); // This will be the ID set by the controller
121+ createdPotionWithId .setId (generatedId );
122+ createdPotionWithId .setName (potionToCreate .getName ());
123+ createdPotionWithId .setDescription (potionToCreate .getDescription ());
124+ // createdPotionWithId.setIngredients(potionToCreate.getIngredients());
125+
126+
127+ // Capture the argument passed to the service to check if ID was set by controller
128+ ArgumentCaptor <Potion > potionCaptor = ArgumentCaptor .forClass (Potion .class );
129+ // We expect the service to be called with a Potion object that has an ID set by the controller.
130+ // The service then returns this Potion object (or a representation of it after persistence).
131+ when (potionService .createPotion (potionCaptor .capture ())).thenAnswer (invocation -> {
132+ Potion p = invocation .getArgument (0 );
133+ // Simulate service returning the potion with the ID it received
134+ createdPotionWithId .setId (p .getId ()); // Ensure the ID captured is used in the response
135+ return createdPotionWithId ;
136+ });
137+
138+
139+ // Act & Assert
140+ mockMvc .perform (post ("/potions" )
141+ .contentType (MediaType .APPLICATION_JSON )
142+ .content (objectMapper .writeValueAsString (potionToCreate )))
143+ .andExpect (status ().isOk ())
144+ .andExpect (jsonPath ("$.id" ).exists ()) // Check that an ID is present
145+ .andExpect (jsonPath ("$.name" ).value ("Mana Potion" ));
146+
147+ verify (potionService ).createPotion (any (Potion .class ));
148+ Potion capturedPotion = potionCaptor .getValue ();
149+ assertThat (capturedPotion .getId ()).isNotNull (); // ID should be set by controller before calling service
150+ assertThat (capturedPotion .getName ()).isEqualTo (potionToCreate .getName ());
151+ assertThat (capturedPotion .getDescription ()).isEqualTo (potionToCreate .getDescription ());
152+ }
153+
154+
155+ // Test generated using Keploy
156+ @ Test
157+ void shouldUpdatePotion_whenUpdatePotionEndpointCalled_J7K8L9 () throws Exception {
158+ // Arrange
159+ UUID potionId = UUID .fromString ("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" );
160+ Potion potionUpdateRequest = new Potion ();
161+ potionUpdateRequest .setName ("Updated Elixir" );
162+ potionUpdateRequest .setDescription ("Super strong" );
163+ // Assuming Potion model does not have setIngredients or getIngredients as per error
164+ // potionUpdateRequest.setIngredients("Rare items");
165+
166+ Potion updatedPotionFromService = new Potion ();
167+ updatedPotionFromService .setId (potionId );
168+ updatedPotionFromService .setName (potionUpdateRequest .getName ());
169+ updatedPotionFromService .setDescription (potionUpdateRequest .getDescription ());
170+ // updatedPotionFromService.setIngredients(potionUpdateRequest.getIngredients());
171+
172+
173+ when (potionService .updatePotion (eq (potionId ), any (Potion .class ))).thenReturn (updatedPotionFromService );
174+
175+ // Act & Assert
176+ mockMvc .perform (put ("/potions/" + potionId )
177+ .contentType (MediaType .APPLICATION_JSON )
178+ .content (objectMapper .writeValueAsString (potionUpdateRequest )))
179+ .andExpect (status ().isOk ())
180+ .andExpect (jsonPath ("$.id" ).value (potionId .toString ()))
181+ .andExpect (jsonPath ("$.name" ).value ("Updated Elixir" ))
182+ .andExpect (jsonPath ("$.description" ).value ("Super strong" ));
183+
184+ verify (potionService ).updatePotion (eq (potionId ), any (Potion .class ));
185+ }
186+
187+
188+
189+ }
0 commit comments