package com.eactive.eai.authserver.dao;

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

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.data.CommonJPATestConfiguration;
import com.eactive.eai.data.jpa.BaseRepositoryImpl;

@DataJpaTest
@ContextConfiguration(classes = { CommonJPATestConfiguration.class })
@ComponentScan({ "com.eactive.eai.authserver" })
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class, basePackages = {
		"com.eactive.eai.authserver" })
@EntityScan({ "com.eactive.eai.data.entity.onl.authserver" })
@Sql({ "/com/eactive/eai/authserver/init_tseaiau04.sql" })
class ScopeDAOTest {

	@Autowired
	ScopeDAO scopeDAO;

	@Test
	void testLoadApiScopeRelations() throws DAOException {
		// given

		// when
		scopeDAO.loadApiScopeRelations();

		// then
		assertNotNull(scopeDAO.loadApiScopeRelations());
		assertTrue(scopeDAO.loadApiScopeRelations().size() > 0);
	}

	@Test
	void testGetApiScopeRelationsByApiId() throws DAOException {
		// given
		String apiId = "test.Bzwksvckeyname";
		// when
		scopeDAO.getApiScopeRelationsByApiId(apiId);

		// then
		assertNotNull(scopeDAO.getApiScopeRelationsByApiId(apiId));
		assertTrue(scopeDAO.getApiScopeRelationsByApiId(apiId).size() > 0);
	}

	@Test
	void testGetApiScopeRelationsByScopeId() throws DAOException {
		// given
		String scopeId = "invest.account";
		// when
		scopeDAO.getApiScopeRelationsByScopeId(scopeId);

		// then
		assertNotNull(scopeDAO.getApiScopeRelationsByScopeId(scopeId));
		assertTrue(scopeDAO.getApiScopeRelationsByScopeId(scopeId).size() > 0);
	}

	@Test
	void testHasApiScopeRelation() throws DAOException {
		// given
		String apiId = "test.Bzwksvckeyname";
		String scopeId = "test.Scopeid";
		// when
		scopeDAO.hasApiScopeRelation(apiId, scopeId);

		// then
		assertTrue(scopeDAO.hasApiScopeRelation(apiId, scopeId));
	}
}
