Skip to content

Commit 02a1d4b

Browse files
committed
added tutorial files
1 parent 4ed4d5d commit 02a1d4b

File tree

9 files changed

+230
-2
lines changed

9 files changed

+230
-2
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<dependency>
5757
<groupId>com.h2database</groupId>
5858
<artifactId>h2</artifactId>
59-
<scope>runtime</scope>
6059
</dependency>
6160
<dependency>
6261
<groupId>org.springframework.boot</groupId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package guru.springframework.bootstrap;
2+
3+
import guru.springframework.domain.Product;
4+
import guru.springframework.repositories.ProductRepository;
5+
import org.apache.log4j.Logger;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.ApplicationListener;
8+
import org.springframework.context.event.ContextRefreshedEvent;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.math.BigDecimal;
12+
13+
@Component
14+
public class ProductLoader implements ApplicationListener<ContextRefreshedEvent> {
15+
16+
private ProductRepository productRepository;
17+
18+
private Logger log = Logger.getLogger(ProductLoader.class);
19+
20+
@Autowired
21+
public void setProductRepository(ProductRepository productRepository) {
22+
this.productRepository = productRepository;
23+
}
24+
25+
@Override
26+
public void onApplicationEvent(ContextRefreshedEvent event) {
27+
28+
Product shirt = new Product();
29+
shirt.setDescription("Spring Framework Guru Shirt");
30+
shirt.setPrice(new BigDecimal("18.95"));
31+
shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
32+
shirt.setProductId("235268845711068308");
33+
productRepository.save(shirt);
34+
35+
log.info("Saved Shirt - id: " + shirt.getId());
36+
37+
Product mug = new Product();
38+
mug.setDescription("Spring Framework Guru Mug");
39+
mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
40+
mug.setProductId("168639393495335947");
41+
productRepository.save(mug);
42+
43+
log.info("Saved Mug - id:" + mug.getId());
44+
}
45+
}

src/main/java/guru/springframework/configuration/SecurityConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
99

1010
@Override
1111
protected void configure(HttpSecurity httpSecurity) throws Exception {
12-
httpSecurity.authorizeRequests().antMatchers("/").permitAll();
12+
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
13+
.authorizeRequests().antMatchers("/console/**").permitAll();
14+
15+
httpSecurity.csrf().disable();
16+
httpSecurity.headers().frameOptions().disable();
1317
}
1418

1519
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package guru.springframework.configuration;
2+
3+
import org.h2.server.web.WebServlet;
4+
import org.springframework.boot.context.embedded.ServletRegistrationBean;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class WebConfiguration {
10+
@Bean
11+
ServletRegistrationBean h2servletRegistration(){
12+
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
13+
registrationBean.addUrlMappings("/console/*");
14+
return registrationBean;
15+
}
16+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package guru.springframework.domain;
2+
3+
import javax.persistence.*;
4+
import java.math.BigDecimal;
5+
6+
@Entity
7+
public class Product {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.AUTO)
10+
private Integer id;
11+
12+
@Version
13+
private Integer version;
14+
15+
private String productId;
16+
private String description;
17+
private String imageUrl;
18+
private BigDecimal price;
19+
20+
public String getDescription() {
21+
return description;
22+
}
23+
24+
public void setDescription(String description) {
25+
this.description = description;
26+
}
27+
28+
public Integer getVersion() {
29+
return version;
30+
}
31+
32+
public void setVersion(Integer version) {
33+
this.version = version;
34+
}
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public String getProductId() {
45+
return productId;
46+
}
47+
48+
public void setProductId(String productId) {
49+
this.productId = productId;
50+
}
51+
52+
public String getImageUrl() {
53+
return imageUrl;
54+
}
55+
56+
public void setImageUrl(String imageUrl) {
57+
this.imageUrl = imageUrl;
58+
}
59+
60+
public BigDecimal getPrice() {
61+
return price;
62+
}
63+
64+
public void setPrice(BigDecimal price) {
65+
this.price = price;
66+
}
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package guru.springframework.repositories;
2+
3+
import guru.springframework.domain.Product;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface ProductRepository extends CrudRepository<Product, Integer>{
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#logging.level.org.h2.server: DEBUG
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package guru.springframework.configuration;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.boot.orm.jpa.EntityScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
8+
9+
@Configuration
10+
@EnableAutoConfiguration
11+
@EntityScan(basePackages = {"guru.springframework.domain"})
12+
@EnableJpaRepositories(basePackages = {"guru.springframework.repositories"})
13+
@EnableTransactionManagement
14+
public class RepositoryConfiguration {
15+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package guru.springframework.repositories;
2+
3+
import guru.springframework.configuration.RepositoryConfiguration;
4+
import guru.springframework.domain.Product;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
import java.math.BigDecimal;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertNull;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@SpringApplicationConfiguration(classes = {RepositoryConfiguration.class})
19+
public class ProductRepositoryTest {
20+
21+
private ProductRepository productRepository;
22+
23+
@Autowired
24+
public void setProductRepository(ProductRepository productRepository) {
25+
this.productRepository = productRepository;
26+
}
27+
28+
@Test
29+
public void testSaveProduct(){
30+
//setup product
31+
Product product = new Product();
32+
product.setDescription("Spring Framework Guru Shirt");
33+
product.setPrice(new BigDecimal("18.95"));
34+
product.setProductId("1234");
35+
36+
//save product, verify has ID value after save
37+
assertNull(product.getId()); //null before save
38+
productRepository.save(product);
39+
assertNotNull(product.getId()); //not null after save
40+
41+
//fetch from DB
42+
Product fetchedProduct = productRepository.findOne(product.getId());
43+
44+
//should not be null
45+
assertNotNull(fetchedProduct);
46+
47+
//should equal
48+
assertEquals(product.getId(), fetchedProduct.getId());
49+
assertEquals(product.getDescription(), fetchedProduct.getDescription());
50+
51+
//update description and save
52+
fetchedProduct.setDescription("New Description");
53+
productRepository.save(fetchedProduct);
54+
55+
//get from DB, should be updated
56+
Product fetchedUpdatedProduct = productRepository.findOne(fetchedProduct.getId());
57+
assertEquals(fetchedProduct.getDescription(), fetchedUpdatedProduct.getDescription());
58+
59+
//verify count of products in DB
60+
long productCount = productRepository.count();
61+
assertEquals(productCount, 1);
62+
63+
//get all products, list should only have one
64+
Iterable<Product> products = productRepository.findAll();
65+
66+
int count = 0;
67+
68+
for(Product p : products){
69+
count++;
70+
}
71+
72+
assertEquals(count, 1);
73+
}
74+
}

0 commit comments

Comments
 (0)