Procházet zdrojové kódy

done, except for tests

ThuyKhong před 5 roky
rodič
revize
b78da42e9a

+ 43
- 0
pom.xml Zobrazit soubor

@@ -50,6 +50,49 @@
50 50
 			<artifactId>spring-boot-starter-data-jpa</artifactId>
51 51
 			<!--<version>1.3.5.RELEASE</version>-->
52 52
 		</dependency>
53
+
54
+		<dependency>
55
+			<groupId>junit</groupId>
56
+			<artifactId>junit</artifactId>
57
+			<version>4.12</version>
58
+			<scope>compile</scope>
59
+		</dependency>
60
+		<dependency>
61
+			<groupId>org.assertj</groupId>
62
+			<artifactId>assertj-core</artifactId>
63
+			<version>3.8.0</version>
64
+			<scope>compile</scope>
65
+		</dependency>
66
+		<dependency>
67
+			<groupId>org.mockito</groupId>
68
+			<artifactId>mockito-core</artifactId>
69
+			<version>2.11.0</version>
70
+			<scope>compile</scope>
71
+		</dependency>
72
+		<dependency>
73
+			<groupId>org.hamcrest</groupId>
74
+			<artifactId>hamcrest-core</artifactId>
75
+			<version>1.3</version>
76
+			<scope>compile</scope>
77
+		</dependency>
78
+		<dependency>
79
+			<groupId>org.hamcrest</groupId>
80
+			<artifactId>hamcrest-library</artifactId>
81
+			<version>1.3</version>
82
+			<scope>compile</scope>
83
+		</dependency>
84
+		<dependency>
85
+			<groupId>org.skyscreamer</groupId>
86
+			<artifactId>jsonassert</artifactId>
87
+			<version>1.5.0</version>
88
+			<scope>compile</scope>
89
+		</dependency>
90
+		<dependency>
91
+			<groupId>org.springframework</groupId>
92
+			<artifactId>spring-test</artifactId>
93
+			<version>5.0.1.RELEASE</version>
94
+			<scope>compile</scope>
95
+		</dependency>
53 96
     </dependencies>
54 97
 
55 98
 	<build>

+ 53
- 0
src/main/java/io/zipcoder/crudapp/Person.java Zobrazit soubor

@@ -0,0 +1,53 @@
1
+package io.zipcoder.crudapp;
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 Person {
10
+    private String firstName;
11
+    private String lastName;
12
+    @Id
13
+    @GeneratedValue(strategy = GenerationType.AUTO)
14
+    private int id;
15
+
16
+    public Person(String firstName, String lastName) {
17
+        this.firstName = firstName;
18
+        this.lastName = lastName;
19
+    }
20
+
21
+    public Person(int id, String firstName, String lastName){
22
+        this.id = id;
23
+        this.firstName = firstName;
24
+        this.lastName = lastName;
25
+    }
26
+
27
+    public Person() {
28
+    }
29
+
30
+    public String getFirstName() {
31
+        return firstName;
32
+    }
33
+
34
+    public void setFirstName(String firstName) {
35
+        this.firstName = firstName;
36
+    }
37
+
38
+    public String getLastName() {
39
+        return lastName;
40
+    }
41
+
42
+    public void setLastName(String lastName) {
43
+        this.lastName = lastName;
44
+    }
45
+
46
+    public int getId() {
47
+        return id;
48
+    }
49
+
50
+    public void setId(int id) {
51
+        this.id = id;
52
+    }
53
+}

+ 61
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Zobrazit soubor

@@ -0,0 +1,61 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpHeaders;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.*;
8
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
9
+
10
+import java.net.URI;
11
+
12
+@RestController
13
+@RequestMapping(value = "/people")
14
+public class PersonController {
15
+
16
+    PersonRepository personRepository;
17
+
18
+    @Autowired
19
+    public PersonController(PersonRepository personRepository){
20
+        this.personRepository = personRepository;
21
+    }
22
+
23
+    @PostMapping
24
+    public ResponseEntity<?> createPerson(@RequestBody Person p){
25
+        personRepository.save(p);
26
+
27
+        URI newUri = ServletUriComponentsBuilder
28
+                .fromCurrentRequest()
29
+                .path("/{id}")
30
+                .buildAndExpand(p.getId())
31
+                .toUri();
32
+        HttpHeaders header = new HttpHeaders();
33
+        header.setLocation(newUri);
34
+        return new ResponseEntity<>(header, HttpStatus.CREATED);
35
+    }
36
+
37
+    @GetMapping(value = "/{id}")
38
+    public ResponseEntity<?> getPerson(@PathVariable int id){
39
+        Person person =  personRepository.findOne(id);
40
+        return new ResponseEntity<>(person, HttpStatus.OK);
41
+    }
42
+
43
+    @GetMapping
44
+    public ResponseEntity<Iterable<Person>> getPersonList(){
45
+        Iterable<Person> personList = personRepository.findAll();
46
+
47
+        return new ResponseEntity<>(personList, HttpStatus.OK);
48
+    }
49
+
50
+    @PutMapping(value = "/{id}")
51
+    public ResponseEntity<?> updatePerson(@RequestBody Person p, @PathVariable int id){
52
+        p = personRepository.save(p);
53
+        return new ResponseEntity<>(p,HttpStatus.OK);
54
+    }
55
+
56
+    @DeleteMapping(value = "/{id}")
57
+    public ResponseEntity<?> DeletePerson(@PathVariable int id){
58
+        personRepository.delete(id);
59
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
60
+    }
61
+}

+ 9
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Zobrazit soubor

@@ -0,0 +1,9 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import org.springframework.stereotype.Repository;
5
+
6
+@Repository
7
+public interface PersonRepository extends CrudRepository<Person, Integer> {
8
+
9
+}

+ 1
- 1
src/main/resources/application.properties Zobrazit soubor

@@ -1,3 +1,3 @@
1 1
 spring.profiles.active=h2
2 2
 
3
-spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

+ 42
- 0
src/test/java/io/zipcoder/crudapp/TestPersonController.java Zobrazit soubor

@@ -0,0 +1,42 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.mockito.InjectMocks;
8
+import org.mockito.Mock;
9
+import org.mockito.runners.MockitoJUnitRunner;
10
+
11
+import static org.mockito.Mockito.mock;
12
+import static org.mockito.Mockito.when;
13
+
14
+
15
+public class TestPersonController {
16
+
17
+
18
+    @Mock
19
+    PersonRepository repositoryMock;
20
+
21
+    @InjectMocks
22
+    PersonController controllerMock;
23
+
24
+    @Before
25
+    public void setupMock(){
26
+        repositoryMock = mock(PersonRepository.class);
27
+        controllerMock = mock(PersonController.class);
28
+    }
29
+
30
+    @Test
31
+    public void testMockCreation(){
32
+        Assert.assertNotNull(repositoryMock);
33
+        Assert.assertNotNull(controllerMock);
34
+    }
35
+
36
+    @Test
37
+    public void testCreate(){
38
+        Person person = new Person (1,"First","Last");
39
+        when(repositoryMock.save(person)).thenReturn(person);
40
+        Assert.assertEquals(person,controllerMock.getPerson(1));
41
+    }
42
+}