Sfoglia il codice sorgente

annotations finished, testing

Trinh Tong 6 anni fa
parent
commit
863975d8bb

+ 12
- 0
src/main/java/com/example/bakerylab/BakerylabApplication.java Vedi File

@@ -0,0 +1,12 @@
1
+package com.example.bakerylab;
2
+
3
+import org.springframework.boot.SpringApplication;
4
+import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+@SpringBootApplication
7
+public class BakerylabApplication {
8
+
9
+    public static void main(String[] args) {
10
+        SpringApplication.run(BakerylabApplication.class, args);
11
+    }
12
+}

+ 49
- 0
src/main/java/com/example/bakerylab/controllers/BakerController.java Vedi File

@@ -0,0 +1,49 @@
1
+package com.example.bakerylab.controllers;
2
+
3
+import com.example.bakerylab.models.Baker;
4
+import com.example.bakerylab.repositories.BakerRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.PathVariable;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+@RestController
14
+public class BakerController {
15
+
16
+    @Autowired
17
+    private BakerRepository bakerRepository;
18
+
19
+    @RequestMapping(value = "/bakers", method = RequestMethod.GET)
20
+    public ResponseEntity<Iterable<Baker>> index() {
21
+        return new ResponseEntity<>(this.bakerRepository.findAll(), HttpStatus.OK);
22
+    }
23
+
24
+    @RequestMapping(value = "/bakers/{id}", method = RequestMethod.GET)
25
+    public ResponseEntity<Baker> show(@PathVariable Long id) {
26
+        return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
27
+    }
28
+
29
+    @RequestMapping(value = "/bakers", method = RequestMethod.POST)
30
+    public ResponseEntity<Baker> create(Baker baker) {
31
+        return new ResponseEntity<>(this.bakerRepository.save(baker), HttpStatus.CREATED);
32
+    }
33
+
34
+    @RequestMapping(value = "/bakers/{id}", method = RequestMethod.PUT)
35
+    public ResponseEntity<Baker> update(@PathVariable Long id, Baker baker) {
36
+        Baker foundBaker = bakerRepository.findById(id).get();
37
+
38
+        foundBaker.setName(baker.getName());
39
+        foundBaker.setSpecialty(baker.getSpecialty());
40
+
41
+        return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
42
+    }
43
+
44
+    @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
45
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
46
+        this.bakerRepository.delete(bakerRepository.findById(id).get());
47
+        return new ResponseEntity<>(true, HttpStatus.OK);
48
+    }
49
+}

+ 48
- 0
src/main/java/com/example/bakerylab/controllers/MuffinController.java Vedi File

@@ -0,0 +1,48 @@
1
+package com.example.bakerylab.controllers;
2
+
3
+import com.example.bakerylab.models.Muffin;
4
+import com.example.bakerylab.repositories.MuffinRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.PathVariable;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+@RestController
14
+public class MuffinController {
15
+
16
+    @Autowired
17
+    private MuffinRepository muffinRepository;
18
+
19
+    @RequestMapping(value = "/muffins", method = RequestMethod.GET)
20
+    public ResponseEntity<Iterable<Muffin>> index() {
21
+        return new ResponseEntity<>(this.muffinRepository.findAll(), HttpStatus.OK);
22
+    }
23
+
24
+    @RequestMapping(value = "/muffins/{id}", method = RequestMethod.GET)
25
+    public ResponseEntity<Muffin> show(@PathVariable Long id) {
26
+        return new ResponseEntity<>(this.muffinRepository.findById(id).get(), HttpStatus.OK);
27
+    }
28
+
29
+    @RequestMapping(value = "/muffins", method = RequestMethod.POST)
30
+    public ResponseEntity<Muffin> create(Muffin muffin) {
31
+        return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
32
+    }
33
+
34
+    @RequestMapping(value = "/muffins/{id}", method = RequestMethod.PUT)
35
+    public ResponseEntity<Muffin> update(@PathVariable Long id, Muffin muffin) {
36
+        Muffin foundMuffin = muffinRepository.findById(id).get();
37
+        foundMuffin.setFlavor(muffin.getFlavor());
38
+
39
+        return new ResponseEntity<>(this.muffinRepository.save(foundMuffin), HttpStatus.OK);
40
+    }
41
+
42
+    @RequestMapping(value = "muffins/{id}", method = RequestMethod.DELETE)
43
+    public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
44
+        this.muffinRepository.delete(muffinRepository.findById(id).get());
45
+        return new ResponseEntity<>(true, HttpStatus.OK);
46
+    }
47
+
48
+}

+ 58
- 0
src/main/java/com/example/bakerylab/models/Baker.java Vedi File

@@ -0,0 +1,58 @@
1
+package com.example.bakerylab.models;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Baker {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    private Long id;
14
+
15
+    private String name;
16
+
17
+    private String employeeId;
18
+
19
+    private String specialty;
20
+
21
+    public Baker(String name, String employeeId, String specialty) {
22
+        this.name = name;
23
+        this.employeeId = employeeId;
24
+        this.specialty = specialty;
25
+    }
26
+
27
+    public Long getId() {
28
+        return id;
29
+    }
30
+
31
+    public void setId(Long id) {
32
+        this.id = id;
33
+    }
34
+
35
+    public String getName() {
36
+        return name;
37
+    }
38
+
39
+    public void setName(String name) {
40
+        this.name = name;
41
+    }
42
+
43
+    public String getEmployeeId() {
44
+        return employeeId;
45
+    }
46
+
47
+    public void setEmployeeId(String employeeId) {
48
+        this.employeeId = employeeId;
49
+    }
50
+
51
+    public String getSpecialty() {
52
+        return specialty;
53
+    }
54
+
55
+    public void setSpecialty(String specialty) {
56
+        this.specialty = specialty;
57
+    }
58
+}

+ 36
- 0
src/main/java/com/example/bakerylab/models/Muffin.java Vedi File

@@ -0,0 +1,36 @@
1
+package com.example.bakerylab.models;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Muffin {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    private Long id;
14
+
15
+    private String flavor;
16
+
17
+    public Muffin(String flavor) {
18
+        this.flavor = flavor;
19
+    }
20
+
21
+    public Long getId() {
22
+        return id;
23
+    }
24
+
25
+    public void setId(Long id) {
26
+        this.id = id;
27
+    }
28
+
29
+    public String getFlavor() {
30
+        return flavor;
31
+    }
32
+
33
+    public void setFlavor(String flavor) {
34
+        this.flavor = flavor;
35
+    }
36
+}

+ 7
- 0
src/main/java/com/example/bakerylab/repositories/BakerRepository.java Vedi File

@@ -0,0 +1,7 @@
1
+package com.example.bakerylab.repositories;
2
+
3
+import com.example.bakerylab.models.Baker;
4
+import org.springframework.data.repository.CrudRepository;
5
+
6
+public interface BakerRepository extends CrudRepository<Baker, Long> {
7
+}

+ 7
- 0
src/main/java/com/example/bakerylab/repositories/MuffinRepository.java Vedi File

@@ -0,0 +1,7 @@
1
+package com.example.bakerylab.repositories;
2
+
3
+import com.example.bakerylab.models.Muffin;
4
+import org.springframework.data.repository.CrudRepository;
5
+
6
+public interface MuffinRepository extends CrudRepository<Muffin, Long> {
7
+}

+ 24
- 0
src/test/java/com/example/bakerylab/BakerylabApplicationTests.java Vedi File

@@ -0,0 +1,24 @@
1
+package com.example.bakerylab;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+import org.junit.runner.RunWith;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.boot.test.context.SpringBootTest;
8
+import org.springframework.context.ApplicationContext;
9
+import org.springframework.test.context.junit4.SpringRunner;
10
+
11
+@RunWith(SpringRunner.class)
12
+@SpringBootTest
13
+public class BakerylabApplicationTests {
14
+
15
+    @Autowired
16
+    private ApplicationContext applicationContext;
17
+
18
+    @Test
19
+    public void contextLoads() {
20
+        Assert.assertNotNull(this.applicationContext);
21
+
22
+    }
23
+
24
+}

+ 41
- 0
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java Vedi File

@@ -0,0 +1,41 @@
1
+package com.example.bakerylab.controllers;
2
+
3
+import com.example.bakerylab.repositories.BakerRepository;
4
+import org.junit.Before;
5
+import org.junit.runner.RunWith;
6
+import org.mockito.InjectMocks;
7
+import org.mockito.MockitoAnnotations;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10
+import org.springframework.boot.test.mock.mockito.MockBean;
11
+import org.springframework.test.context.junit4.SpringRunner;
12
+import org.springframework.test.web.servlet.MockMvc;
13
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14
+
15
+import static org.junit.Assert.*;
16
+
17
+
18
+@RunWith(SpringRunner.class)
19
+@WebMvcTest(BakerController.class)
20
+public class BakerControllerTest {
21
+
22
+    @Autowired
23
+    MockMvc mvc;
24
+
25
+    @MockBean
26
+    BakerRepository bakerRepository;
27
+
28
+    @Autowired
29
+    @InjectMocks
30
+    BakerController bakerController;
31
+
32
+    @Before
33
+    public void init(){
34
+        MockitoAnnotations.initMocks(this);
35
+        mvc = MockMvcBuilders
36
+                .standaloneSetup(bakerController)
37
+                .build();
38
+    }
39
+
40
+
41
+}

+ 39
- 0
src/test/java/com/example/bakerylab/controllers/MuffinControllerTest.java Vedi File

@@ -0,0 +1,39 @@
1
+package com.example.bakerylab.controllers;
2
+
3
+import com.example.bakerylab.models.Muffin;
4
+import com.example.bakerylab.repositories.MuffinRepository;
5
+import org.junit.Before;
6
+import org.junit.runner.RunWith;
7
+import org.mockito.InjectMocks;
8
+import org.mockito.MockitoAnnotations;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11
+import org.springframework.boot.test.mock.mockito.MockBean;
12
+import org.springframework.test.context.junit4.SpringRunner;
13
+import org.springframework.test.web.servlet.MockMvc;
14
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15
+
16
+import static org.junit.Assert.*;
17
+
18
+@RunWith(SpringRunner.class)
19
+@WebMvcTest(MuffinController.class)
20
+public class MuffinControllerTest {
21
+
22
+    @Autowired
23
+    private MockMvc mvc;
24
+
25
+    @MockBean
26
+    MuffinRepository muffinRepository;
27
+
28
+    @InjectMocks
29
+    @Autowired
30
+    MuffinController muffinController;
31
+
32
+    @Before
33
+    public void init(){
34
+        MockitoAnnotations.initMocks(this);
35
+        mvc = MockMvcBuilders
36
+                .standaloneSetup(muffinController)
37
+                .build();
38
+    }
39
+}