Skip to content

Commit 75d49aa

Browse files
committed
add controller tests
1 parent 2af72c5 commit 75d49aa

File tree

8 files changed

+202
-21
lines changed

8 files changed

+202
-21
lines changed

src/main/java/com/sellist/flashcards/model/Card.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Data;
5+
import lombok.NoArgsConstructor;
56

67
@Data
78
@AllArgsConstructor
9+
@NoArgsConstructor
810
public class Card {
911
public String front;
1012
public String back;

src/main/java/com/sellist/flashcards/service/CardService.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.sellist.flashcards.model.Card;
44
import com.sellist.flashcards.model.Note;
5-
import com.sellist.flashcards.model.Scale;
65
import com.sellist.flashcards.model.request.CardsRequest;
76
import com.sellist.flashcards.model.request.NotesRequest;
87
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,13 +27,13 @@ public CardService(AbcJsService abcJsService, ScaleService scaleService, NoteSer
2827
public List<Card> generateCards(CardsRequest requests) {
2928
List<Card> output = new ArrayList<>();
3029

31-
Scale s = scaleService.generateScale(
32-
scaleService.getScalePattern(requests.getScaleType()),
30+
List<Note> s = scaleService.buildScale(
31+
requests.getScaleType(),
3332
requests.getStartingNote(),
3433
requests.getOctaves()
3534
);
3635

37-
s.getNotes().forEach(n -> output.add(generateCard(n)));
36+
s.forEach(n -> output.add(generateCard(n)));
3837

3938
return output;
4039
}

src/main/java/com/sellist/flashcards/service/ScaleService.java

-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ public List<Note> generateSequentialRangeBetweenNotes(String scaleType, String s
107107
return scale;
108108
}
109109

110-
public String getScalePattern(String scaleName) {
111-
return musiCache.sequentialScaleNameToPattern(scaleName.toLowerCase());
112-
}
113-
114110
@Override
115111
public List<String> listAvailable() {
116112
return musiCache.availableScales().stream().toList();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.sellist.flashcards.controller;
2+
3+
import com.sellist.flashcards.model.response.ResponseMetadata;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class BaseControllerTest {
9+
10+
private final BaseController baseController = new BaseController();
11+
12+
@Test
13+
void testGenerateMetadata() {
14+
// Act
15+
ResponseMetadata metadata = baseController.generateMetadata();
16+
17+
// Assert
18+
assertEquals("OK", metadata.getStatus());
19+
assertEquals(200, metadata.getCode());
20+
assertEquals("Success", metadata.getMessage());
21+
}
22+
23+
@Test
24+
void testHandleException() {
25+
ResponseMetadata metadata = baseController.handleException(new Exception("Test error"));
26+
27+
assertEquals("ERROR", metadata.getStatus());
28+
assertEquals(500, metadata.getCode());
29+
assertEquals("Test error", metadata.getMessage());
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.sellist.flashcards.controller;
2+
3+
import com.sellist.flashcards.model.Card;
4+
import com.sellist.flashcards.model.request.CardsRequest;
5+
import com.sellist.flashcards.model.request.NotesRequest;
6+
import com.sellist.flashcards.model.response.ApiResponse;
7+
import com.sellist.flashcards.service.CardService;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.InjectMocks;
11+
import org.mockito.Mock;
12+
import org.mockito.MockitoAnnotations;
13+
import org.springframework.http.HttpStatus;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.mockito.Mockito.when;
20+
21+
class CardControllerTest {
22+
23+
@Mock
24+
private CardService cardService;
25+
26+
@InjectMocks
27+
private CardController cardController;
28+
29+
@BeforeEach
30+
void setUp() {
31+
MockitoAnnotations.openMocks(this);
32+
}
33+
34+
@Test
35+
void testGetCards() {
36+
List<CardsRequest> request = Collections.singletonList(new CardsRequest());
37+
List<Card> cards = Collections.singletonList(new Card());
38+
when(cardService.generateCards(request)).thenReturn(cards);
39+
40+
ApiResponse<List<Card>> response = cardController.getCards(request);
41+
42+
assertEquals(cards, response.getData());
43+
assertEquals(HttpStatus.OK.value(), response.getMetadata().getCode());
44+
}
45+
46+
@Test
47+
void testGetNotes() {
48+
NotesRequest request = new NotesRequest();
49+
List<Card> cards = Collections.singletonList(new Card());
50+
when(cardService.generateCardsFromNotes(request)).thenReturn(cards);
51+
52+
ApiResponse<List<Card>> response = cardController.getNotes(request);
53+
54+
assertEquals(cards, response.getData());
55+
assertEquals(HttpStatus.OK.value(), response.getMetadata().getCode());
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.sellist.flashcards.controller;
2+
3+
import com.sellist.flashcards.model.FormInformation;
4+
import com.sellist.flashcards.model.response.ApiResponse;
5+
import com.sellist.flashcards.service.InfoService;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.MockitoAnnotations;
11+
12+
import java.util.List;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.mockito.Mockito.when;
16+
17+
class InformationControllerTest {
18+
@Mock
19+
private InfoService infoService;
20+
21+
@InjectMocks
22+
private InformationController informationController;
23+
24+
@BeforeEach
25+
void setUp() {
26+
MockitoAnnotations.openMocks(this);
27+
}
28+
29+
@Test
30+
void testGetFormInfo() {
31+
FormInformation formInformation = new FormInformation();
32+
when(infoService.getInfo()).thenReturn(formInformation);
33+
34+
ApiResponse<FormInformation> response = informationController.getFormInfo();
35+
36+
assertEquals(formInformation, response.getData());
37+
assertEquals("OK", response.getMetadata().getStatus());
38+
}
39+
40+
@Test
41+
void testListScales() {
42+
List<String> scales = List.of("Scale1", "Scale2");
43+
when(infoService.listScales()).thenReturn(scales);
44+
45+
ApiResponse<List<String>> response = informationController.listScales();
46+
47+
assertEquals(scales, response.getData());
48+
assertEquals("OK", response.getMetadata().getStatus());
49+
}
50+
51+
@Test
52+
void testListSteps() {
53+
List<String> steps = List.of("Step1", "Step2");
54+
when(infoService.listSteps()).thenReturn(steps);
55+
56+
ApiResponse<List<String>> response = informationController.listSteps();
57+
58+
assertEquals(steps, response.getData());
59+
assertEquals("OK", response.getMetadata().getStatus());
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.sellist.flashcards.controller;
2+
3+
import com.sellist.flashcards.model.Note;
4+
import com.sellist.flashcards.model.request.NotesRequest;
5+
import com.sellist.flashcards.model.response.ApiResponse;
6+
import com.sellist.flashcards.service.NoteService;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.mockito.InjectMocks;
10+
import org.mockito.Mock;
11+
import org.mockito.MockitoAnnotations;
12+
13+
import java.util.List;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.mockito.Mockito.when;
17+
18+
class NoteControllerTest {
19+
@Mock
20+
private NoteService noteService;
21+
22+
@InjectMocks
23+
private NoteController noteController;
24+
25+
@BeforeEach
26+
void setUp() {
27+
MockitoAnnotations.openMocks(this);
28+
}
29+
30+
@Test
31+
void testGetNotes() {
32+
NotesRequest request = new NotesRequest();
33+
request.setNotes(List.of("C", "D", "E"));
34+
Note noteC = new Note();
35+
Note noteD = new Note();
36+
Note noteE = new Note();
37+
when(noteService.generateNote("C")).thenReturn(noteC);
38+
when(noteService.generateNote("D")).thenReturn(noteD);
39+
when(noteService.generateNote("E")).thenReturn(noteE);
40+
41+
ApiResponse<List<Note>> response = noteController.getNotes(request);
42+
43+
assertEquals(List.of(noteC, noteD, noteE), response.getData());
44+
assertEquals("OK", response.getMetadata().getStatus());
45+
}
46+
}

src/test/java/com/sellist/flashcards/controller/ScaleControllerTest.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@
1111
import static org.junit.jupiter.api.Assertions.assertNotNull;
1212

1313
@SpringBootTest
14-
public class ScaleControllerTest {
15-
14+
class ScaleControllerTest {
1615
@Autowired
1716
private ScaleController scaleController;
1817

1918
@Test
2019
void testGetMajorScaleFromRequest() {
21-
// Create a sample ScaleRequest object
2220
ScaleRequest scaleRequest = new ScaleRequest();
2321
scaleRequest.setScaleTonic("C4");
2422
scaleRequest.setScaleName("Major");
2523
scaleRequest.setOctaves(2);
2624

27-
// Call the getScale method
2825
ApiResponse<Scale> response = scaleController.getScale(scaleRequest);
2926

30-
// Assert the response
3127
assertNotNull(response);
3228
assertNotNull(response.getData());
3329
assertEquals("C", response.getData().get(0).getNoteName());
@@ -37,16 +33,13 @@ void testGetMajorScaleFromRequest() {
3733

3834
@Test
3935
void testGetMinorScaleFromRequest() {
40-
// Create a sample ScaleRequest object
4136
ScaleRequest scaleRequest = new ScaleRequest();
4237
scaleRequest.setScaleTonic("A4");
4338
scaleRequest.setScaleName("Minor");
4439
scaleRequest.setOctaves(2);
4540

46-
// Call the getScale method
4741
ApiResponse<Scale> response = scaleController.getScale(scaleRequest);
4842

49-
// Assert the response
5043
assertNotNull(response);
5144
assertNotNull(response.getData());
5245
assertEquals("A", response.getData().get(0).getNoteName());
@@ -56,22 +49,18 @@ void testGetMinorScaleFromRequest() {
5649

5750
@Test
5851
void testGetSharpChromaticScaleFromRequest() {
59-
// Create a sample ScaleRequest object
6052
ScaleRequest scaleRequest = new ScaleRequest();
6153
scaleRequest.setScaleTonic("C4");
6254
scaleRequest.setScaleName("flat_chromatic");
6355
scaleRequest.setOctaves(1);
6456

65-
// Call the getScale method
6657
ApiResponse<Scale> response = scaleController.getScale(scaleRequest);
6758

68-
// Assert the response
6959
assertNotNull(response);
7060
assertNotNull(response.getData());
7161
assertEquals("C", response.getData().get(0).getNoteName());
7262
assertEquals(60, response.getData().get(0).getMidiValue());
7363
assertEquals(72, response.getData().get(12).getMidiValue());
7464
assertEquals(13, response.getData().size());
7565
}
76-
77-
}
66+
}

0 commit comments

Comments
 (0)