Trinh Tong 6 lat temu
rodzic
commit
5fb64136b7

+ 4
- 4
src/main/java/com/example/bakerylab/models/Baker.java Wyświetl plik

1
 package com.example.bakerylab.models;
1
 package com.example.bakerylab.models;
2
 
2
 
3
-import javax.persistence.Entity;
4
-import javax.persistence.GeneratedValue;
5
-import javax.persistence.GenerationType;
6
-import javax.persistence.Id;
3
+import javax.persistence.*;
7
 
4
 
8
 @Entity
5
 @Entity
9
 public class Baker {
6
 public class Baker {
12
     @GeneratedValue(strategy = GenerationType.AUTO)
9
     @GeneratedValue(strategy = GenerationType.AUTO)
13
     private Long id;
10
     private Long id;
14
 
11
 
12
+    @Column(name = "Name")
15
     private String name;
13
     private String name;
16
 
14
 
15
+    @Column(name = "EmployeeId")
17
     private String employeeId;
16
     private String employeeId;
18
 
17
 
18
+    @Column(name = "Specialty")
19
     private String specialty;
19
     private String specialty;
20
 
20
 
21
     public Baker(String name, String employeeId, String specialty) {
21
     public Baker(String name, String employeeId, String specialty) {

+ 2
- 4
src/main/java/com/example/bakerylab/models/Muffin.java Wyświetl plik

1
 package com.example.bakerylab.models;
1
 package com.example.bakerylab.models;
2
 
2
 
3
-import javax.persistence.Entity;
4
-import javax.persistence.GeneratedValue;
5
-import javax.persistence.GenerationType;
6
-import javax.persistence.Id;
3
+import javax.persistence.*;
7
 
4
 
8
 @Entity
5
 @Entity
9
 public class Muffin {
6
 public class Muffin {
12
     @GeneratedValue(strategy = GenerationType.AUTO)
9
     @GeneratedValue(strategy = GenerationType.AUTO)
13
     private Long id;
10
     private Long id;
14
 
11
 
12
+    @Column(name = "FLAVOR")
15
     private String flavor;
13
     private String flavor;
16
 
14
 
17
     public Muffin(String flavor) {
15
     public Muffin(String flavor) {

+ 15
- 0
src/test/java/com/example/bakerylab/JsonUtil.java Wyświetl plik

1
+package com.example.bakerylab;
2
+
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+
5
+public class JsonUtil {
6
+
7
+    public static String asJsonString(final Object obj) {
8
+        try {
9
+            final ObjectMapper mapper = new ObjectMapper();
10
+            return mapper.writeValueAsString(obj);
11
+        } catch (Exception e) {
12
+            throw new RuntimeException(e);
13
+        }
14
+    }
15
+}

+ 70
- 1
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java Wyświetl plik

1
 package com.example.bakerylab.controllers;
1
 package com.example.bakerylab.controllers;
2
 
2
 
3
+import com.example.bakerylab.models.Baker;
3
 import com.example.bakerylab.repositories.BakerRepository;
4
 import com.example.bakerylab.repositories.BakerRepository;
4
 import org.junit.Before;
5
 import org.junit.Before;
6
+import org.junit.Test;
5
 import org.junit.runner.RunWith;
7
 import org.junit.runner.RunWith;
6
 import org.mockito.InjectMocks;
8
 import org.mockito.InjectMocks;
7
 import org.mockito.MockitoAnnotations;
9
 import org.mockito.MockitoAnnotations;
8
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10
 import org.springframework.boot.test.mock.mockito.MockBean;
12
 import org.springframework.boot.test.mock.mockito.MockBean;
13
+import org.springframework.http.MediaType;
11
 import org.springframework.test.context.junit4.SpringRunner;
14
 import org.springframework.test.context.junit4.SpringRunner;
12
 import org.springframework.test.web.servlet.MockMvc;
15
 import org.springframework.test.web.servlet.MockMvc;
16
+import org.springframework.test.web.servlet.MvcResult;
13
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14
 
18
 
15
-import static org.junit.Assert.*;
19
+import java.util.Optional;
20
+
21
+import static com.example.bakerylab.JsonUtil.asJsonString;
22
+import static org.mockito.BDDMockito.given;
23
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
25
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16
 
26
 
17
 
27
 
18
 @RunWith(SpringRunner.class)
28
 @RunWith(SpringRunner.class)
19
 @WebMvcTest(BakerController.class)
29
 @WebMvcTest(BakerController.class)
20
 public class BakerControllerTest {
30
 public class BakerControllerTest {
31
+    // === Instance Variables === //
32
+    private Baker froilan;
33
+    private final String GOOD_URL = "/bakers";
21
 
34
 
22
     @Autowired
35
     @Autowired
23
     MockMvc mvc;
36
     MockMvc mvc;
35
         mvc = MockMvcBuilders
48
         mvc = MockMvcBuilders
36
                 .standaloneSetup(bakerController)
49
                 .standaloneSetup(bakerController)
37
                 .build();
50
                 .build();
51
+
52
+        froilan = new Baker("Froilan", "Master Baker", "Croissants");
53
+
54
+    }
55
+
56
+    // ================= Create Baker ================= //
57
+
58
+    @Test
59
+    public void testCreateBakerSuccess() throws Exception {
60
+
61
+        given(bakerRepository.save(froilan)).willReturn(froilan);
62
+
63
+        mvc.perform(post("/bakers")
64
+            .content(asJsonString(froilan))
65
+            .contentType(MediaType.APPLICATION_JSON)
66
+                .accept(MediaType.APPLICATION_JSON))
67
+                .andExpect(status().isCreated());
68
+    }
69
+
70
+    @Test
71
+    public void testCreateBakerFail() throws Exception {
72
+        Baker b = new Baker ("Seth", "froilans prepper", "muffins");
73
+        String badUrl = "/bakers500";
74
+        given(bakerRepository.save(b)).willReturn(b);
75
+
76
+        mvc.perform(post(badUrl)
77
+                .content(asJsonString(froilan))
78
+                .contentType(MediaType.APPLICATION_JSON)
79
+                .accept(MediaType.APPLICATION_JSON))
80
+                .andExpect(status().isNotFound());
81
+
38
     }
82
     }
39
 
83
 
84
+    // ================= Get Baker ================= //
85
+
86
+    @Test
87
+    public void getBakerById() throws Exception {
88
+        Long bakerId = 1L;
89
+
90
+        given(bakerRepository.findById(bakerId)).willReturn(Optional.of(froilan));
91
+
92
+        MvcResult response = mvc.perform(get(GOOD_URL)
93
+                .accept(MediaType.APPLICATION_JSON))
94
+                .andExpect(status().isOk())
95
+                .andReturn();
96
+
97
+        System.out.println(response.getResponse().getContentAsString());
98
+//        response.getResponse().getContentAsString();
99
+
100
+    }
101
+
102
+
103
+    // ================= Get Bakers ================= //
104
+
105
+    // ================= Update Baker ================= //
106
+
107
+    // ================= Delete Baker ================= //
108
+
40
 
109
 
41
 }
110
 }

+ 10
- 0
src/test/java/com/example/bakerylab/controllers/MuffinControllerTest.java Wyświetl plik

36
                 .standaloneSetup(muffinController)
36
                 .standaloneSetup(muffinController)
37
                 .build();
37
                 .build();
38
     }
38
     }
39
+
40
+    // ================= Create Muffin ================= //
41
+
42
+    // ================= Get Muffin ================= //
43
+
44
+    // ================= Get Muffins ================= //
45
+
46
+    // ================= Update Muffin ================= //
47
+
48
+    // ================= Delete Muffin ================= //
39
 }
49
 }