Trinh Tong 6 vuotta sitten
vanhempi
commit
5fb64136b7

+ 4
- 4
src/main/java/com/example/bakerylab/models/Baker.java Näytä tiedosto

@@ -1,9 +1,6 @@
1 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 5
 @Entity
9 6
 public class Baker {
@@ -12,10 +9,13 @@ public class Baker {
12 9
     @GeneratedValue(strategy = GenerationType.AUTO)
13 10
     private Long id;
14 11
 
12
+    @Column(name = "Name")
15 13
     private String name;
16 14
 
15
+    @Column(name = "EmployeeId")
17 16
     private String employeeId;
18 17
 
18
+    @Column(name = "Specialty")
19 19
     private String specialty;
20 20
 
21 21
     public Baker(String name, String employeeId, String specialty) {

+ 2
- 4
src/main/java/com/example/bakerylab/models/Muffin.java Näytä tiedosto

@@ -1,9 +1,6 @@
1 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 5
 @Entity
9 6
 public class Muffin {
@@ -12,6 +9,7 @@ public class Muffin {
12 9
     @GeneratedValue(strategy = GenerationType.AUTO)
13 10
     private Long id;
14 11
 
12
+    @Column(name = "FLAVOR")
15 13
     private String flavor;
16 14
 
17 15
     public Muffin(String flavor) {

+ 15
- 0
src/test/java/com/example/bakerylab/JsonUtil.java Näytä tiedosto

@@ -0,0 +1,15 @@
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 Näytä tiedosto

@@ -1,23 +1,36 @@
1 1
 package com.example.bakerylab.controllers;
2 2
 
3
+import com.example.bakerylab.models.Baker;
3 4
 import com.example.bakerylab.repositories.BakerRepository;
4 5
 import org.junit.Before;
6
+import org.junit.Test;
5 7
 import org.junit.runner.RunWith;
6 8
 import org.mockito.InjectMocks;
7 9
 import org.mockito.MockitoAnnotations;
8 10
 import org.springframework.beans.factory.annotation.Autowired;
9 11
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10 12
 import org.springframework.boot.test.mock.mockito.MockBean;
13
+import org.springframework.http.MediaType;
11 14
 import org.springframework.test.context.junit4.SpringRunner;
12 15
 import org.springframework.test.web.servlet.MockMvc;
16
+import org.springframework.test.web.servlet.MvcResult;
13 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 28
 @RunWith(SpringRunner.class)
19 29
 @WebMvcTest(BakerController.class)
20 30
 public class BakerControllerTest {
31
+    // === Instance Variables === //
32
+    private Baker froilan;
33
+    private final String GOOD_URL = "/bakers";
21 34
 
22 35
     @Autowired
23 36
     MockMvc mvc;
@@ -35,7 +48,63 @@ public class BakerControllerTest {
35 48
         mvc = MockMvcBuilders
36 49
                 .standaloneSetup(bakerController)
37 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 Näytä tiedosto

@@ -36,4 +36,14 @@ public class MuffinControllerTest {
36 36
                 .standaloneSetup(muffinController)
37 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
 }