/*
 * AMW - Automated Middleware allows you to manage the configurations of
 * your Java EE applications on an unlimited number of different environments
 * with various versions, including the automated deployment of those apps.
 * Copyright (C) 2013-2016 by Puzzle ITC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ch.puzzle.itc.mobiliar.business.generator.control.extracted.templates;

import ch.puzzle.itc.mobiliar.business.environment.entity.ContextEntity;
import ch.puzzle.itc.mobiliar.business.generator.control.AMWTemplateExceptionHandler;
import ch.puzzle.itc.mobiliar.business.generator.control.extracted.ResourceDependencyResolverService;
import ch.puzzle.itc.mobiliar.business.property.entity.AmwAppServerNodeModel;
import ch.puzzle.itc.mobiliar.business.releasing.entity.ReleaseEntity;
import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceEntity;
import ch.puzzle.itc.mobiliar.business.resourcerelation.entity.ProvidedResourceRelationEntity;
import ch.puzzle.itc.mobiliar.common.exception.TemplatePropertyException;
import ch.puzzle.itc.mobiliar.test.CustomLogging;
import ch.puzzle.itc.mobiliar.test.testrunner.PersistenceTestRunner;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateModelException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.IOException;
import java.util.Set;
import java.util.logging.Level;

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

@RunWith(PersistenceTestRunner.class)
public class ApplicationResolverTest {

	@Spy
	@PersistenceContext
	EntityManager entityManager;

	ApplicationResolverEntityBuilder builder;

	@InjectMocks
	ContextEntity context;

	ApplicationResolver resolver;

     @Mock
	ResourceDependencyResolverService resourceDependencyResolverService;

	@Before
	public void before() throws Exception {
		CustomLogging.setup(Level.OFF);
		builder = new ApplicationResolverEntityBuilder(entityManager).buildScenario();
		context = builder.context;
		MockitoAnnotations.initMocks(this);
		Mockito.when(resourceDependencyResolverService.getProvidedMasterRelationsForRelease(ArgumentMatchers.<ResourceEntity>any(), ArgumentMatchers.<ReleaseEntity>any()))
				.thenAnswer(new Answer<Set<ProvidedResourceRelationEntity>>() {

					@Override
					public Set<ProvidedResourceRelationEntity> answer(InvocationOnMock invocation) throws Throwable {
						return ((ResourceEntity) invocation.getArguments()[0]).getProvidedMasterRelations();
					}
				});
		Mockito.when(resourceDependencyResolverService.getProvidedSlaveRelationsForRelease(ArgumentMatchers.<ResourceEntity>any(), ArgumentMatchers.<ReleaseEntity>any()))
				.thenAnswer(new Answer<Set<ProvidedResourceRelationEntity>>() {

					@Override
					public Set<ProvidedResourceRelationEntity> answer(InvocationOnMock invocation) throws Throwable {
						return ((ResourceEntity) invocation.getArguments()[0]).getProvidedSlaveRelations();
					}
				});
		resolver = new ApplicationResolver(builder.options, builder.ws, resourceDependencyResolverService);
		assertEquals(true, resolver.resolve());
	}

	/**
	 * FIXME ama -> what about node, doesnt seem right but that's how i understand old generation code
	 */
	public void testResolvedEntities() throws IOException, TemplatePropertyException {
		assertEquals(builder.app2, resolver.getApplication());
		assertEquals(builder.as2, resolver.getApplicationServer());
	}

	/**
	 * FIXME ama -> what is the actual structure of the map, does it need property types?
	 */
	@Test
	public void testTransformedProperties() throws TemplateModelException {
		AMWTemplateExceptionHandler templateExceptionHandler = new AMWTemplateExceptionHandler();
		AmwAppServerNodeModel model = new AmwAppServerNodeModel();
		resolver.transform(templateExceptionHandler, model);
		assertEquals("app2", ((SimpleHash)model.get("app")).get("label").toString());
		assertEquals("as2", ((SimpleHash)model.get("appServer")).get("label").toString());
		assertTrue(templateExceptionHandler.isSuccess());
	}
}

@@ -1509,103 +1509,8 @@
 ty;%0A
-import ch.puzzle.itc.mobiliar.business.resourcerelation.entity.ProvidedResourceRelationEntity;%0A
 impo
@@ -1894,92 +1894,8 @@
 o.*;
-%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;
 %0A%0Aim
@@ -2009,30 +2009,8 @@
 on;%0A
-import java.util.Set;%0A
 impo
@@ -2679,12 +2679,12 @@
 ons.
-init
+open
 Mock
@@ -2878,191 +2878,21 @@
 wer(
-new Answer%3CSet%3CProvidedResourceRelationEntity%3E%3E() %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public Set%3CProvidedResourceRelationEntity%3E answer(InvocationOnMock invocation) throws Throwable %7B%0A%09%09%09%09%09%09return
+invocation -%3E
  ((R
@@ -2960,38 +2960,24 @@
 rRelations()
-;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 );%0A%09%09Mockito
@@ -3152,191 +3152,21 @@
 wer(
-new Answer%3CSet%3CProvidedResourceRelationEntity%3E%3E() %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public Set%3CProvidedResourceRelationEntity%3E answer(InvocationOnMock invocation) throws Throwable %7B%0A%09%09%09%09%09%09return
+invocation -%3E
  ((R
@@ -3241,22 +3241,8 @@
 ns()
-;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 );%0A%09
@@ -3498,16 +3498,23 @@
 de%0A%09 */%0A
+%09@Test%0A
 %09public 

use lambda for mock