/*
 * Copyright 2018 EPAM Systems
 *
 * 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.epam.ta.reportportal.config;

import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author <a href="mailto:ihar_kahadouski@epam.com">Ihar Kahadouski</a>
 */
@Configuration
public class DataSourceConfig {

	@Value("${embedded.datasource.dir}")
	private String dataDir = "/embedded-postgres";

	@Value("${embedded.datasource.clean}")
	private Boolean clean = true;

	// A port number of 0 means that the port number is automatically allocated.
	@Value("${embedded.datasource.port}")
	private Integer port;

	@Primary
	@Bean
	public DataSourceProperties dataSourceProperties() {
		return new DataSourceProperties();
	}

	@Primary
	@Profile("!unittest")
	@Bean
	public DataSource dataSource() {
		return dataSourceProperties().initializeDataSourceBuilder().build();
	}

	@Bean
	@Profile("unittest")
	public DataSource testDataSource() throws IOException {
		final EmbeddedPostgres.Builder builder = EmbeddedPostgres.builder()
				.setPort(port)
				.setDataDirectory(System.getProperty("user.dir") + dataDir)
				.setCleanDataDirectory(clean);
		return builder.start().getPostgresDatabase();
	}
}

@@ -1237,322 +1237,35 @@
 %0A%0A%09@
-Value(%22$%7Bembedded.datasource.dir%7D%22)%0A%09private String dataDir = %22/embedded-postgres%22;%0A%0A%09@Value(%22$%7Bembedded.datasource.clean%7D%22)%0A%09private Boolean clean = true;%0A%0A%09// A port number of 0 means that the port number is automatically allocated.%0A%09@Value(%22$%7Bembedded.datasource.port%7D%22)%0A%09private Integer port;%0A%0A%09@Primary%0A%09@Bean
+Bean%0A%09@Profile(%22!unittest%22)
 %0A%09pu
@@ -1366,16 +1366,23 @@
 Primary%0A
+%09@Bean%0A
 %09@Profil
@@ -1396,23 +1396,16 @@
 ttest%22)%0A
-%09@Bean%0A
 %09public 
@@ -1505,16 +1505,26 @@
 ();%0A%09%7D%0A%0A
+%09@Primary%0A
 %09@Bean%0A%09
@@ -1578,16 +1578,176 @@
 aSource(
+@Value(%22$%7Bembedded.datasource.dir%7D%22) String dataDir,%0A%09%09%09@Value(%22$%7Bembedded.datasource.clean%7D%22) Boolean clean, @Value(%22$%7Bembedded.datasource.port%7D%22) Integer port
 ) throws

Update datasource config