BAEL-2841 updated test cases and test data
This commit is contained in:
parent
bbce155a78
commit
7bb364a2f2
@ -1,33 +1,33 @@
|
|||||||
package com.baeldung.config;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||||
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
|
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
|
||||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||||
|
|
||||||
import com.baeldung.entity.Fruit;
|
import com.baeldung.entity.Fruit;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class JpaPopulators {
|
public class JpaPopulators {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
|
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
|
||||||
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
|
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
|
||||||
factory.setResources(new Resource[] { (Resource) new ClassPathResource("fruit-data.json") });
|
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
|
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||||
marshaller.setClassesToBeBound(Fruit.class);
|
marshaller.setClassesToBeBound(Fruit.class);
|
||||||
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
|
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
|
||||||
factory.setUnmarshaller(marshaller);
|
factory.setUnmarshaller(marshaller);
|
||||||
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") });
|
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") });
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<fruit>
|
<fruit>
|
||||||
<id>3</id>
|
<id>1</id>
|
||||||
<name>mango</name>
|
<name>apple</name>
|
||||||
<color>yellow</color>
|
<color>red</color>
|
||||||
</fruit>
|
</fruit>
|
||||||
|
@ -1,53 +1,38 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.repository;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.entity.Fruit;
|
import com.baeldung.entity.Fruit;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class FruitPopulatorTest {
|
public class FruitPopulatorTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FruitRepository fruitRepository;
|
private FruitRepository fruitRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
|
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
|
||||||
|
|
||||||
List<Fruit> fruits = fruitRepository.findAll();
|
List<Fruit> fruits = fruitRepository.findAll();
|
||||||
assertEquals("record count is not matching", 3, fruits.size());
|
assertEquals("record count is not matching", 2, fruits.size());
|
||||||
|
|
||||||
fruits.forEach(fruit -> {
|
fruits.forEach(fruit -> {
|
||||||
if (1 == fruit.getId()) {
|
if (1 == fruit.getId()) {
|
||||||
assertEquals("This is not an apple", "apple", fruit.getName());
|
assertEquals("apple", fruit.getName());
|
||||||
assertEquals("It is not a red colored fruit", "red", fruit.getColor());
|
assertEquals("red", fruit.getColor());
|
||||||
} else if (2 == fruit.getId()) {
|
} else if (2 == fruit.getId()) {
|
||||||
assertEquals("This is not a guava", "guava", fruit.getName());
|
assertEquals("guava", fruit.getName());
|
||||||
assertEquals("It is not a green colored fruit", "green", fruit.getColor());
|
assertEquals("green", fruit.getColor());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void givenFruitXmlPopulatorThenShouldInsertRecordOnStart() {
|
|
||||||
|
|
||||||
List<Fruit> fruits = fruitRepository.findAll();
|
|
||||||
assertEquals("record count is not matching", 3, fruits.size());
|
|
||||||
|
|
||||||
fruits.forEach(fruit -> {
|
|
||||||
if (3 == fruit.getId()) {
|
|
||||||
assertEquals("This is not a mango", "mango", fruit.getName());
|
|
||||||
assertEquals("It is not a yellow colored fruit", "yellow", fruit.getColor());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user