package com.continuuity.internal.app.scheduler;

import com.continuuity.app.Id;
import com.continuuity.app.services.AppFabricService;
import com.continuuity.app.services.AuthToken;
import com.continuuity.app.services.EntityType;
import com.continuuity.app.services.ProgramId;
import com.continuuity.app.services.ScheduleId;
import com.continuuity.internal.app.services.AppFabricServer;
import com.continuuity.test.internal.TestHelper;
import com.continuuity.weave.filesystem.LocalLocationFactory;
import junit.framework.Assert;
import org.junit.Test;

import java.util.List;

/**
 *
 */
public class ScheduleDeploymentTests {

  @Test
  public void testDeployment() throws Exception {

    AppFabricServer appFabricServer = TestHelper.getInjector().getInstance(AppFabricServer.class);
    AuthToken token = new AuthToken("token");
    appFabricServer.startAndWait();
    AppFabricService.Iface appFabricService = TestHelper.getInjector().getInstance(AppFabricService.Iface.class);

    appFabricService.reset(TestHelper.DUMMY_AUTH_TOKEN, "developer");

    TestHelper.deployApplication(appFabricService, new LocalLocationFactory(),
                                 Id.Account.from("developer"), token, "SampleApplication",
                                 "SampleApp", SampleApplication.class);

    ProgramId id  = new ProgramId("developer", "SampleApp", "SampleWorkflow");
    id.setType(EntityType.WORKFLOW);

    List<ScheduleId> schedules = appFabricService.getSchedules(TestHelper.DUMMY_AUTH_TOKEN, id);
    Assert.assertEquals(1, schedules.size());

    //deploy application with same name and no schedule.
    TestHelper.deployApplication(appFabricService, new LocalLocationFactory(),
                                 Id.Account.from("developer"), token, "SampleApplication",
                                 "SampleApp", SampleApplicationNoSchedule.class);

    schedules = appFabricService.getSchedules(TestHelper.DUMMY_AUTH_TOKEN, id);
    Assert.assertEquals(0, schedules.size());
  }

}

@@ -820,24 +820,36 @@
 n(%22token%22);%0A
+    try %7B%0A  
     appFabri
@@ -872,20 +872,24 @@
 Wait();%0A
+%0A%0A
     
+  
 AppFabri
@@ -987,24 +987,26 @@
 ce.class);%0A%0A
+  
     appFabri
@@ -1064,24 +1064,26 @@
 per%22);%0A%0A    
+  
 TestHelper.d
@@ -1170,32 +1170,34 @@
                 
+ 
+ 
 Id.Account.from(
@@ -1230,32 +1230,34 @@
 leApplication%22,%0A
+  
                 
@@ -1317,16 +1317,18 @@
 );%0A%0A    
+  
 ProgramI
@@ -1398,16 +1398,18 @@
 %22);%0A    
+ 
+ 
 id.setTy
@@ -1430,24 +1430,26 @@
 WORKFLOW);%0A%0A
+  
     List%3CSch
@@ -1529,32 +1529,34 @@
 TOKEN, id);%0A    
+  
 Assert.assertEqu
@@ -1578,24 +1578,26 @@
 s.size());%0A%0A
+  
     //deploy
@@ -1641,24 +1641,26 @@
 hedule.%0A    
+  
 TestHelper.d
@@ -1714,32 +1714,34 @@
 ationFactory(),%0A
+  
                 
@@ -1840,32 +1840,34 @@
                 
+  
 %22SampleApp%22, Sam
@@ -1904,16 +1904,18 @@
 );%0A%0A    
+ 
+ 
 schedule
@@ -1982,24 +1982,26 @@
 N, id);%0A    
+  
 Assert.asser
@@ -2030,15 +2030,78 @@
 ize());%0A
+    %7D%0A    finally %7B%0A      appFabricServer.stopAndWait();%0A    %7D%0A
   %7D%0A%0A%7D%0A

Stop app fabric after tests