Skip to content

Commit 86d6023

Browse files
xamcrosspivovarit
authored andcommitted
BAEL-2095 CrudRepository save() method (eugenp#5223)
* BAEL-2095 CrudRepository save() method * BAEL-2095 Enhanced the MerchandiseEntity with additional attributes; added unit-tests; removed spring boot runner * BAEL-2095 Test renamed to end with IntegrationTest to make it past Travis's rules
1 parent aef7b7f commit 86d6023

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.MerchandiseEntity;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface InventoryRepository extends CrudRepository<MerchandiseEntity, Long> {
7+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.domain;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import java.math.BigDecimal;
8+
9+
@Entity
10+
public class MerchandiseEntity {
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private Long id;
14+
15+
private String title;
16+
17+
private BigDecimal price;
18+
19+
private String brand;
20+
21+
public MerchandiseEntity() {
22+
}
23+
24+
public MerchandiseEntity(String title, BigDecimal price) {
25+
this.title = title;
26+
this.price = price;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public void setTitle(String title) {
38+
this.title = title;
39+
}
40+
41+
public BigDecimal getPrice() {
42+
return price;
43+
}
44+
45+
public void setPrice(BigDecimal price) {
46+
this.price = price;
47+
}
48+
49+
public String getBrand() {
50+
return brand;
51+
}
52+
53+
public void setBrand(String brand) {
54+
this.brand = brand;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "MerchandiseEntity{" +
60+
"id=" + id +
61+
", title='" + title + '\'' +
62+
", price=" + price +
63+
", brand='" + brand + '\'' +
64+
'}';
65+
}
66+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.config.PersistenceConfiguration;
4+
import com.baeldung.domain.MerchandiseEntity;
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.autoconfigure.orm.jpa.DataJpaTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.math.BigDecimal;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
17+
@RunWith(SpringRunner.class)
18+
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class})
19+
public class InventoryRepositoryIntegrationTest {
20+
21+
private static final String ORIGINAL_TITLE = "Pair of Pants";
22+
private static final String UPDATED_TITLE = "Branded Luxury Pants";
23+
private static final String UPDATED_BRAND = "Armani";
24+
private static final String ORIGINAL_SHORTS_TITLE = "Pair of Shorts";
25+
26+
@Autowired
27+
private InventoryRepository repository;
28+
29+
@Test
30+
public void shouldCreateNewEntryInDB() {
31+
MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE);
32+
pants = repository.save(pants);
33+
34+
MerchandiseEntity shorts = new MerchandiseEntity(ORIGINAL_SHORTS_TITLE, new BigDecimal(3));
35+
shorts = repository.save(shorts);
36+
37+
assertNotNull(pants.getId());
38+
assertNotNull(shorts.getId());
39+
assertNotEquals(pants.getId(), shorts.getId());
40+
}
41+
42+
@Test
43+
public void shouldUpdateExistingEntryInDB() {
44+
MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE);
45+
pants = repository.save(pants);
46+
47+
Long originalId = pants.getId();
48+
49+
pants.setTitle(UPDATED_TITLE);
50+
pants.setPrice(BigDecimal.TEN);
51+
pants.setBrand(UPDATED_BRAND);
52+
53+
MerchandiseEntity result = repository.save(pants);
54+
55+
assertEquals(originalId, result.getId());
56+
assertEquals(UPDATED_TITLE, result.getTitle());
57+
assertEquals(BigDecimal.TEN, result.getPrice());
58+
assertEquals(UPDATED_BRAND, result.getBrand());
59+
}
60+
}

0 commit comments

Comments
 (0)