package org.ihtsdo.buildcloud.service;

import org.ihtsdo.buildcloud.entity.Build;
import org.ihtsdo.buildcloud.entity.Package;
import org.ihtsdo.buildcloud.entity.User;
import org.ihtsdo.buildcloud.entity.helper.EntityHelper;
import org.ihtsdo.buildcloud.entity.helper.TestEntityGenerator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/test/testDataServiceContext.xml"})
@Transactional
public class PackageServiceImplTest extends TestEntityGenerator {
	
	@Autowired
	private BuildService bs;
	
	@Autowired
	private PackageService ps;
	private User authenticatedUser;

	@Before
	public void setup() {
		authenticatedUser = TestEntityGenerator.TEST_USER;
	}

	@Test
	public void testCreate() throws Exception{

		Assert.assertNotNull(ps);
		String rc  = EntityHelper.formatAsBusinessKey(releaseCenterShortNames[0]);
		String ext = EntityHelper.formatAsBusinessKey(extensionNames[0]);
		String p   = EntityHelper.formatAsBusinessKey(productNames[0][0]);
		
		//Packages get looked up using a build composite key (ie include the unique ID) 
		//so first lets find the first build for a known product, and use that
		List<Build> builds = bs.findForProduct(rc, ext, p, authenticatedUser);
		String buildCompKey = builds.get(0).getCompositeKey();
		
		List<Package> packages = ps.findAll(buildCompKey, authenticatedUser);
		int before = packages.size();
		//LOGGER.warn("Found " + before + " packages");
		
		Assert.assertTrue(before > 0);  //Check our test data is in there.
		ps.create(buildCompKey, "my test packages name", authenticatedUser);
		int after = ps.findAll(buildCompKey, authenticatedUser).size();
		//LOGGER.warn("After create, found " + after + " packages");
		Assert.assertEquals(before + 1, after);
		
		//TODO Could add further tests to ensure that the new item was created at the correct point in the hierarchy
	}

}

@@ -905,17 +905,27 @@
 ervice b
-s
+uildService
 ;%0A%09%0A%09@Au
@@ -961,10 +961,23 @@
 ce p
-s;
+ackageService;%0A
 %0A%09pr
@@ -1005,16 +1005,68 @@
 tedUser;
+%0A%09private String buildCompKey;%0A%09private Build build;
 %0A%0A%09@Befo
@@ -1149,102 +1149,73 @@
 R;%0A%09
-%7D%0A%0A%09@Test%0A%09public void testCreate() throws Exception%7B%0A%0A%09%09Assert.assertNotNull(ps);%0A%09%09String rc
+%09Assert.assertNotNull(packageService);%0A%09%09String releaseCenterName
   = 
@@ -1288,16 +1288,26 @@
 ring ext
+ensionName
  = Entit
@@ -1364,16 +1364,26 @@
 String p
+ackageName
    = Ent
@@ -1433,18 +1433,16 @@
 0%5D%5B0%5D);%0A
-%09%09
 %0A%09%09//Pac
@@ -1515,17 +1515,16 @@
 ique ID)
- 
 %0A%09%09//so 
@@ -1613,17 +1613,27 @@
 ilds = b
-s
+uildService
 .findFor
@@ -1645,48 +1645,101 @@
 ct(r
-c, ext, p, authenticatedUser);%0A%09%09String 
+eleaseCenterName, extensionName, packageName, authenticatedUser);%0A%09%09build = builds.get(0);%0A%09%09
 buil
@@ -1754,24 +1754,16 @@
  = build
-s.get(0)
 .getComp
@@ -1779,39 +1779,155 @@
 );%0A%09
-%09%0A%09%09List%3CPackage%3E packages = ps
+%7D%0A%0A%09@Test%0A%09public void testCreate() throws Exception %7B%0A%09%09// Assert pre conditions%0A%09%09Assert.assertEquals(%22Test data in database.%22, 3, packageService
 .fin
@@ -1967,168 +1967,75 @@
 ser)
-;%0A%09%09int before = packages
 .size()
-;
+);%0A
 %0A%09%09//
-LOGGER.warn(%22Found %22 + before + %22 packages%22);%0A%09%09%0A%09%09Assert.assertTrue(before %3E 0);  //Check our test data is in there.%0A%09%09ps
+ Run test method%0A%09%09Package aPackage = packageService
 .cre
@@ -2101,24 +2101,100 @@
 r);%0A
+%0A
 %09%09
-int after = ps
+// Assert post conditions%0A%09%09Assert.assertEquals(%22New package created.%22, 4, packageService
 .fin
@@ -2241,231 +2241,176 @@
 ze()
+)
 ;%0A%09%09
-//LOGGER.warn(%22After create, found %22 + after + %22 packages%22);%0A%09%09Assert.assertEquals(before + 1, after);%0A%09%09%0A%09%09//TODO Could add further tests to ensure that the new item was created at the correct point in the hierarchy
+Assert.assertNotNull(aPackage.getId());%0A%09%09Assert.assertEquals(%22my test packages name%22, aPackage.getName());%0A%09%09Assert.assertEquals(build, aPackage.getBuild());
 %0A%09%7D%0A%0A
+%0A%0A
 %7D%0A

Refactor PackageServiceImplTest as clean example.