package com.godmonth.status.test.sample.machine.cfg3;

import com.godmonth.status.advancer.intf.SyncResult;
import com.godmonth.status.executor.intf.OrderExecutor;
import com.godmonth.status.test.sample.db2.RepoConfig2;
import com.godmonth.status.test.sample.db3.RepoConfig3;
import com.godmonth.status.test.sample.domain.SampleModel;
import com.godmonth.status.test.sample.domain.SampleStatus;
import com.godmonth.status.test.sample.repo.SampleModelRepository;
import com.godmonth.status.test.sample.repo2.SampleModelReadOnlyRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

/**
 * <p></p >
 *
 * @author shenyue
 */
@ComponentScan
@SpringBootTest(classes = {RepoConfig2.class, RepoConfig3.class, SampleOrderExecutorConfig3.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Sample3Test {

    @Autowired
    private OrderExecutor<SampleModel, String> sampleModelOrderExecutor;

    @Autowired
    private SampleModelRepository sampleModelRepository;

    @Autowired
    private SampleModelReadOnlyRepository sampleModelReadOnlyRepository;

    @Qualifier("d2")
    @Autowired
    private DataSource dataSource2;

    @Qualifier("d3")
    @Autowired
    private DataSource dataSource3;

    @Test
    void name() throws SQLException {
        SampleModel sampleModel = new SampleModel();
        sampleModel.setStatus(SampleStatus.CREATED);
        sampleModel.setType("test");
        SampleModel sampleModel1 = sampleModelRepository.save(sampleModel);
        SyncResult<SampleModel, ?> execute = sampleModelOrderExecutor.execute(sampleModel1, "eee", "fff");
        Assertions.assertEquals(SampleStatus.PAID, execute.getModel().getStatus());


        System.out.println(execute);
        {
            Connection connection = dataSource2.getConnection();
            ResultSet resultSet = connection.createStatement().executeQuery("select count(*) from sample_model");
            resultSet.next();
            Assertions.assertEquals(resultSet.getInt(1), 1);
            String string = resultSet.getString(1);
            System.out.println(string);
        }
        {
            //查看数据源3
            Connection connection3 = dataSource3.getConnection();
            ResultSet resultSet3 = connection3.createStatement().executeQuery("select count(*) from sample_model");
            resultSet3.next();
            Assertions.assertEquals(resultSet3.getInt(1), 0);
        }
        //查看数据源2
        Optional<SampleModel> byId = sampleModelRepository.findById(1L);
        Assertions.assertTrue(byId.isPresent());
        Optional<SampleModel> byId2 = sampleModelReadOnlyRepository.findById(1L);
        Assertions.assertFalse(byId2.isPresent());

    }
}

@@ -79,31 +79,37 @@
 tus.
-advancer.intf.SyncResul
+executor.intf.ExecutionReques
 t;%0Ai
@@ -161,16 +161,17 @@
 Executor
+2
 ;%0Aimport
@@ -395,16 +395,87 @@
 Status;%0A
+import com.godmonth.status.test.sample.machine.inst.SampleInstruction;%0A
 import c
@@ -1524,16 +1524,17 @@
 Executor
+2
 %3CSampleM
@@ -1539,22 +1539,22 @@
 eModel, 
-String
+Object
 %3E sample
@@ -2073,33 +2073,33 @@
 test%22);%0A        
-S
+s
 ampleModel sampl
@@ -2084,32 +2084,34 @@
      sampleModel
+ =
  sampleModel1 = 
@@ -2110,23 +2110,8 @@
 odel
-1 = sampleModel
 Repo
@@ -2144,16 +2144,234 @@
         
+%7B%0A            ExecutionRequest%3CSampleModel, Object%3E req = ExecutionRequest.%3CSampleModel, Object%3Ebuilder().model(sampleModel).instruction(%22pay%22).message(%22balance%22).build();%0A            com.godmonth.status.executor.intf.
 SyncResu
@@ -2436,37 +2436,59 @@
 ute(
-sampleModel1, %22eee%22, %22fff%22);%0A
+req);%0A            System.out.println(execute);%0A    
     
@@ -2571,18 +2571,16 @@
 ));%0A
-%0A%0A
         
 Syst
@@ -2579,36 +2579,55 @@
     
-System.out.println(execute);
+    sampleModel = execute.getModel();%0A        %7D
 %0A   
@@ -2649,263 +2649,483 @@
     
-Conn
+Ex
 ec
+u
 tion
- conn
+Request%3CSampleModel, Object%3E req = Ex
 ec
+u
 tion
- = dataSource2.getConnection();%0A            ResultSet resultSet = connection.createStatement().executeQuery(%22select count(*) from sample_model%22);%0A            resultSet.next();%0A            Assertions.assertEquals(resultSet.getInt(1), 1
+Request.%3CSampleModel, Object%3Ebuilder().model(sampleModel).instruction(SampleInstruction.DELIVER).message(%22yunda%22).build();%0A            com.godmonth.status.executor.intf.SyncResult%3CSampleModel, ?%3E execute = sampleModelOrderExecutor.execute(req);%0A            System.out.println(execute);%0A            Assertions.assertEquals(SampleStatus.DELIVERED, execute.getModel().getStatus());%0A            sampleModel = execute.getModel(
 );%0A 
@@ -3135,89 +3135,458 @@
     
+%7D%0A
     
-String string = resultSet.getString(1);%0A            System.out.println(string
+    %7B%0A            ExecutionRequest%3CSampleModel, Object%3E req = ExecutionRequest.%3CSampleModel, Object%3Ebuilder().model(sampleModel).instruction(SampleInstruction.EVALUATE).message(%225%22).build();%0A            com.godmonth.status.executor.intf.SyncResult%3CSampleModel, ?%3E execute = sampleModelOrderExecutor.execute(req);%0A            System.out.println(execute);%0A            Assertions.assertEquals(SampleStatus.EVALUATED, execute.getModel().getStatus()
 );%0A 
@@ -4186,13 +4186,14 @@
 ));%0A
-%0A
     %7D%0A
+%0A%0A
 %7D%0A

test code to OrderExecutor2