Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.zipcoder.persistenceapp.core.model;

import java.io.Serializable;

public abstract class BaseEntity implements Serializable{



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder.persistenceapp.core.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder.persistenceapp.humans.controller;

import io.zipcoder.persistenceapp.humans.model.Person;
import io.zipcoder.persistenceapp.humans.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/people")
public class PersonController {

private PersonService personService;

@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> createPerson(@RequestBody Person person) {
Person savedPerson = personService.createPerson(person);
return new ResponseEntity<>(savedPerson, HttpStatus.CREATED);
}


}
50 changes: 50 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/humans/model/Home.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.zipcoder.persistenceapp.humans.model;

import io.zipcoder.persistenceapp.core.model.BaseEntity;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;

@Entity
public class Home extends BaseEntity {

private static final Long serialVersionUID = 123456789L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;

@NotEmpty
@Column(name = "ADDRESS")
private String address;

@NotEmpty
@Column(name = "HOMENUMBER")
private String homeNumber;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getHomeNumber() {
return homeNumber;
}

public void setHomeNumber(String homeNumber) {
this.homeNumber = homeNumber;
}

}
84 changes: 84 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/humans/model/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.zipcoder.persistenceapp.humans.model;

import io.zipcoder.persistenceapp.core.model.BaseEntity;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;

@Entity
public class Person extends BaseEntity {

private static final Long serialVersionUID = 123456789L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;

@NotEmpty
@Column(name = "FIRST_NAME")
private String firstName;

@NotEmpty
@Column(name = "LAST_NAME"/*, nullable = false*/)
private String lastName;
// Check to see if nullable=false is better or @NotEmpty is better

@Column(name = "BIRTHDAY")
private String birthday;

@NotEmpty
@Column(name = "MOBILE")
private String mobile;

@Column(name = "HOME_ID")
private Short homeId;

public Long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public Short getHomeId() {
return homeId;
}

public void setHomeId(short homeId) {
this.homeId = homeId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.persistenceapp.humans.repository;

import io.zipcoder.persistenceapp.core.repository.BaseRepository;
import io.zipcoder.persistenceapp.humans.model.Home;
import org.springframework.stereotype.Repository;

@Repository
public interface HomeRepository extends BaseRepository<Home, Integer> {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.persistenceapp.humans.repository;

import io.zipcoder.persistenceapp.core.repository.BaseRepository;
import io.zipcoder.persistenceapp.humans.model.Person;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends BaseRepository<Person, Integer> {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.zipcoder.persistenceapp.humans.service;

import io.zipcoder.persistenceapp.humans.model.Person;
import io.zipcoder.persistenceapp.humans.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonService {

private PersonRepository personRepository;

@Autowired
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}

public Person createPerson(Person person) {
return personRepository.save(person);
}

}
24 changes: 24 additions & 0 deletions src/main/resources/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
INSERT INTO movies VALUES(1, 'Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG');
INSERT INTO movies VALUES(2, 'Lavalantula', 83, 'Horror', 4.7, 'TV-14');
INSERT INTO movies VALUES(3, 'Starship Troopers', 129, 'Sci-Fi', 4.7, 'PG-13');
INSERT INTO movies VALUES(4, 'Waltz With Bashir', 90, 'Documentary', 4.7, 'R');
INSERT INTO movies VALUES(5, 'Spaceballs', 96, 'Comedy', 4.7, 'PG');
INSERT INTO movies VALUES(6, 'Monsters Inc.', 92, 'Animation', 4.7, 'G');

SELECT * FROM movies WHERE genre='Sci-Fi';

SELECT * FROM movies WHERE imdb_score >= 6.5;

SELECT * FROM movies WHERE rating = 'G' OR rating = 'PG' AND runtime < 100;

SELECT AVG(runtime) FROM movies WHERE imdb_score < 7.5 GROUP BY genre;

UPDATE movies SET rating = 'R' WHERE title = 'Starship Troopers';

SELECT id, rating FROM movies WHERE genre = 'Horror' OR genre = 'Documentary';

SELECT AVG(imdb_score), MIN(imdb_score), MAX(imdb_score) FROM movies GROUP BY rating;

SELECT AVG(imdb_score), MIN(imdb_score), MAX(imdb_score) FROM movies GROUP BY rating HAVING COUNT(*) > 1;

DELETE FROM movies WHERE rating = 'R';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.zipcoder;
package io.zipcoder.persistenceapp;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.zipcoder.persistenceapp.humans.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.zipcoder.persistenceapp.humans.model.Person;
import io.zipcoder.persistenceapp.humans.service.PersonService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
public class PersonControllerTest {

@MockBean
private PersonService personService;

@InjectMocks
private PersonController personController;
private MockMvc mvc;

private Long personId;
private Person person;

@Before
public void init() {
MockitoAnnotations.initMocks(this);
personController = new PersonController(personService);
mvc = MockMvcBuilders.standaloneSetup(personController).build();
personId = 13245363L;
person = new Person();
person.setId(personId);
}

@Test
public void testCreatePerson() throws Exception {
String predictedEntity = new ObjectMapper().writeValueAsString(person);

when(personService.createPerson(person))
.thenReturn(person);

mvc.perform(post("/people")
.contentType(MediaType.APPLICATION_JSON)
.content(predictedEntity))
.andExpect(status().isCreated());

/**
* Find out how to verify business logic (either retrieve a representation of the Person object
* from the mockMvc response or verify that the service was called once)
* */
// verify(personService, times(1))
// .createPerson(eq(person));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.zipcoder.persistenceapp.humans.service;

import io.zipcoder.persistenceapp.humans.model.Person;
import io.zipcoder.persistenceapp.humans.repository.PersonRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PersonServiceTest {

@Mock
private PersonRepository personRepository;

private PersonService personService;
private Long personId;
private Person person;

@Before
public void init() {
personService = new PersonService(personRepository);
personId = 12345L;
person = new Person();
person.setId(personId);
}

@Test
public void testCreatePerson() {
when(personRepository.save(person))
.thenReturn(person);

Person returnedPerson = personService.createPerson(person);

Long returnedPersonId = returnedPerson.getId();
Assert.assertEquals("The expected entity is not returned", personId, returnedPersonId);
}

}