<repo_name>julianpicon/Podam-Project<file_sep>/README.md
# Podam-Project
Proyecto Java que usa la API para generación de datos Podam
<file_sep>/java-crud/src/test/java/com/tutorial/repository/PersonRepositoryTest.java
/*
 * Copyright 2016, <NAME>                                                 
 * by the @authors tag. See the LICENCE in the distribution for a              
 * full listing of individual contributors.                                   
 *                                                                           
 * Licensed under the Apache License, Version 2.0 (the "License");             
 * you may not use this file except in compliance with the License.            
 * You may obtain a copy of the License at                                     
 * http://www.apache.org/licenses/LICENSE-2.0                                  
 * Unless required by applicable law or agreed to in writing, software         
 * distributed under the License is distributed on an "AS IS" BASIS,           
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
 * See the License for the specific language governing permissions and         
 * limitations under the License. 
 */
package com.tutorial.repository;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Optional;

import org.hibernate.internal.SessionImpl;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tutorial.entity.Person;

import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

public class PersonRepositoryTest {
	private static final Logger log = LoggerFactory.getLogger(PersonRepositoryTest.class);
	private static PersonRepository repository;

	@BeforeClass
	public static void beforeClass() throws SQLException {
		log.info("Initializing entity manager factory...");
		repository = new PersonRepositoryImpl();
		Connection connection = repository.getEntityManager().unwrap(SessionImpl.class).connection();
		Database database = null;
		Liquibase liquibase = null;

		try {
			log.debug("Starting liquibase migration...");
			database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
			liquibase = new Liquibase("dbChangelog.xml", new ClassLoaderResourceAccessor(), database);
			liquibase.update("test");
		} catch (LiquibaseException e) {
			log.error("Error occured in execution: {}", e.getMessage());
			e.printStackTrace();
		}
		log.info("Entity manager factory started");
	}

	@Before
	public void before() {
		Person person = new Person();
		person.setFirstName("Julius");
		person.setLastName("Krah");
		person.setCreatedDate(LocalDateTime.now());
		person.setDateOfBirth(LocalDate.of(1990, Month.APRIL, 4));

		repository.create(person);
	}

	@Test
	public void testCreate() {
		/*Person person = new Person();
		person.setFirstName("Loretta");
		person.setLastName("Krah");
		person.setCreatedDate(LocalDateTime.now());
		person.setDateOfBirth(LocalDate.of(1992, Month.AUGUST, 12));*/
		PodamFactory factory = new PodamFactoryImpl();
		Person person = factory.manufacturePojo(Person.class);
		System.out.println(person);

		Optional<Person> p = repository.create(person);
		p.ifPresent(consumer -> {
			log.info("Created person: {}", consumer);
			assertNotNull(consumer.getId());
		});

	}

	@Test
	public void testUpdate() {
		Optional<Person> person = repository.read(1L);
		person.ifPresent(consumer -> {
			consumer.setModifiedDate(LocalDateTime.now());
			consumer.setFirstName("Abeiku");
			repository.update(consumer);
		});

		person = repository.read(1L);
		assertTrue(person.isPresent());

		assertThat(person.get().getFirstName(), is("Abeiku"));

	}

	@Test
	public void testRead() {
		Optional<Person> person = repository.read(1L);

		assertTrue(person.isPresent());
		assertNotNull(person.get().getFirstName());
	}

	@Test
	public void testDelete() {
		Optional<Person> person = repository.read(2L);

		person.ifPresent(consumer -> {
			assertNotNull(consumer);
			repository.delete(consumer);
		});

		person = repository.read(2L);
		assertFalse(person.isPresent());
	}

	@AfterClass
	public static void afterClass() {
		log.info("Closing entity manager factory...");
		repository.close();
		log.info("Entity manager factory closed");
	}
}
<file_sep>/java-crud/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.juliuskrah.tutorial</groupId>
	<artifactId>gs-hibernate-jpa</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<description>Simple CRUD project using Hibernate and JPA</description>

	<properties>
		<h2.version>1.4.193</h2.version>
		<hibernate.version>5.2.6.Final</hibernate.version>
		<junit.version>4.12</junit.version>
		<liquibase.version>3.5.3</liquibase.version>
		<log4j2.version>2.7</log4j2.version>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-hikaricp</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>${h2.version}</version>
		</dependency>

		<!-- Migration -->
		<dependency>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-core</artifactId>
			<version>${liquibase.version}</version>
		</dependency>

		<!-- Logging -->
		<dependency><!-- Send all Slf4J logs to Log4J -->
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j2.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j2.version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency><!-- Send liquibase logs to Log4J -->
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-liquibase</artifactId>
			<version>${log4j2.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/uk.co.jemos.podam/podam -->
		<dependency>
		    <groupId>uk.co.jemos.podam</groupId>
		    <artifactId>podam</artifactId>
		    <version>7.0.5.RELEASE</version>
		    <scope>test</scope>
		</dependency>

	</dependencies>
</project><file_sep>/java-crud/src/test/java/com/tutorial/repository/CompanyRepositoryTest.java
package com.tutorial.repository;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;

import org.hibernate.internal.SessionImpl;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tutorial.entity.Company;

import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

public class CompanyRepositoryTest {
	private static final Logger log = LoggerFactory.getLogger(CompanyRepositoryTest.class);
	private static CompanyRepository repository;

	@BeforeClass
	public static void beforeClass() throws SQLException {
		log.info("Initializing entity manager factory...");
		repository = new CompanyRepositoryImpl();
		Connection connection = repository.getEntityManager().unwrap(SessionImpl.class).connection();
		Database database = null;
		Liquibase liquibase = null;

		try {
			log.debug("Starting liquibase migration...");
			database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
			liquibase = new Liquibase("dbChangelog.xml", new ClassLoaderResourceAccessor(), database);
			liquibase.update("test");
		} catch (LiquibaseException e) {
			log.error("Error occured in execution: {}", e.getMessage());
			e.printStackTrace();
		}
		log.info("Entity manager factory started");
	}

	@Before
	public void before() {

	}

	@Test
	public void testCreate() {
		
		PodamFactory factory = new PodamFactoryImpl();
		Company company = factory.manufacturePojo(Company.class);
		System.out.println(company);

		Optional<Company> p = repository.create(company);
		p.ifPresent(consumer -> {
			log.info("Created company: {}", consumer);
			assertNotNull(company.getIdCompany());
		});

	}

	@Test
	public void testUpdate() {
		Optional<Company> company = repository.read(1L);
		company.ifPresent(consumer -> {
			consumer.setPhone("123456");
			repository.update(consumer);
		});

		company = repository.read(1L);
		assertTrue(company.isPresent());

		assertNotNull(company.get().getName());

	}

	@Test
	public void testRead() {
		Optional<Company> company = repository.read(1L);

		assertTrue(company.isPresent());
		assertNotNull(company.get().getName());
	}

	@Test
	public void testDelete() {
		Optional<Company> company = repository.read(2L);

		company.ifPresent(consumer -> {
			assertNotNull(consumer);
			repository.delete(consumer);
		});

		company = repository.read(2L);
		assertFalse(company.isPresent());
	}

	@AfterClass
	public static void afterClass() {
		log.info("Closing entity manager factory...");
		repository.close();
		log.info("Entity manager factory closed");
	}
}
<file_sep>/java-crud/README.md
[ ![Codeship Status for juliuskrah/java-crud](https://app.codeship.com/projects/e1e1f9d0-d361-0134-0c11-16d407f7e953/status?branch=master)](https://app.codeship.com/projects/201898)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
# CRUD Operations with Hibernate and JPA using Liquibase Migrations
In this repository you will find code that performs basic CRUD operations on H2 database using Hibernate and JPA.  
This repository also showcases how to perform database migrations using Liquibase.

The accopanying tutorial is located at [http://juliuskrah.com/tutorial/2017/02/15/crud-operations-with-hibernate-and-jpa/](http://juliuskrah.com/tutorial/2017/02/15/crud-operations-with-hibernate-and-jpa/)

## Prerequisites
- Maven
- JDK

# Setup
Just import into your IDE and run main method
