|
| 1 | +/* |
| 2 | + * Copyright 2023 Conductor Authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * <p> |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.netflix.conductor.postgres.dao; |
| 14 | + |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.SQLException; |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.flywaydb.core.Flyway; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 27 | +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.test.context.ContextConfiguration; |
| 30 | +import org.springframework.test.context.TestPropertySource; |
| 31 | +import org.springframework.test.context.junit4.SpringRunner; |
| 32 | + |
| 33 | +import com.netflix.conductor.common.config.TestObjectMapperConfiguration; |
| 34 | +import com.netflix.conductor.common.metadata.tasks.Task; |
| 35 | +import com.netflix.conductor.common.run.TaskSummary; |
| 36 | +import com.netflix.conductor.common.run.Workflow; |
| 37 | +import com.netflix.conductor.common.run.WorkflowSummary; |
| 38 | +import com.netflix.conductor.postgres.config.PostgresConfiguration; |
| 39 | +import com.netflix.conductor.postgres.util.Query; |
| 40 | + |
| 41 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +@ContextConfiguration( |
| 46 | + classes = { |
| 47 | + TestObjectMapperConfiguration.class, |
| 48 | + PostgresConfiguration.class, |
| 49 | + FlywayAutoConfiguration.class |
| 50 | + }) |
| 51 | +@RunWith(SpringRunner.class) |
| 52 | +@TestPropertySource( |
| 53 | + properties = { |
| 54 | + "conductor.app.asyncIndexingEnabled=false", |
| 55 | + "conductor.elasticsearch.version=0", |
| 56 | + "conductor.indexing.type=postgres", |
| 57 | + "conductor.postgres.onlyIndexOnStatusChange=true", |
| 58 | + "spring.flyway.clean-disabled=false" |
| 59 | + }) |
| 60 | +@SpringBootTest |
| 61 | +public class PostgresIndexDAOStatusChangeOnlyTest { |
| 62 | + |
| 63 | + @Autowired private PostgresIndexDAO indexDAO; |
| 64 | + |
| 65 | + @Autowired private ObjectMapper objectMapper; |
| 66 | + |
| 67 | + @Qualifier("dataSource") |
| 68 | + @Autowired |
| 69 | + private DataSource dataSource; |
| 70 | + |
| 71 | + @Autowired Flyway flyway; |
| 72 | + |
| 73 | + // clean the database between tests. |
| 74 | + @Before |
| 75 | + public void before() { |
| 76 | + flyway.migrate(); |
| 77 | + } |
| 78 | + |
| 79 | + private WorkflowSummary getMockWorkflowSummary(String id) { |
| 80 | + WorkflowSummary wfs = new WorkflowSummary(); |
| 81 | + wfs.setWorkflowId(id); |
| 82 | + wfs.setCorrelationId("correlation-id"); |
| 83 | + wfs.setWorkflowType("workflow-type"); |
| 84 | + wfs.setStartTime("2023-02-07T08:42:45Z"); |
| 85 | + wfs.setStatus(Workflow.WorkflowStatus.RUNNING); |
| 86 | + return wfs; |
| 87 | + } |
| 88 | + |
| 89 | + private TaskSummary getMockTaskSummary(String taskId) { |
| 90 | + TaskSummary ts = new TaskSummary(); |
| 91 | + ts.setTaskId(taskId); |
| 92 | + ts.setTaskType("task-type"); |
| 93 | + ts.setTaskDefName("task-def-name"); |
| 94 | + ts.setStatus(Task.Status.SCHEDULED); |
| 95 | + ts.setStartTime("2023-02-07T09:41:45Z"); |
| 96 | + ts.setUpdateTime("2023-02-07T09:42:45Z"); |
| 97 | + ts.setWorkflowType("workflow-type"); |
| 98 | + return ts; |
| 99 | + } |
| 100 | + |
| 101 | + private List<Map<String, Object>> queryDb(String query) throws SQLException { |
| 102 | + try (Connection c = dataSource.getConnection()) { |
| 103 | + try (Query q = new Query(objectMapper, c, query)) { |
| 104 | + return q.executeAndFetchMap(); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public void checkWorkflow(String workflowId, String status, String correlationId) |
| 110 | + throws SQLException { |
| 111 | + List<Map<String, Object>> result = |
| 112 | + queryDb( |
| 113 | + String.format( |
| 114 | + "SELECT * FROM workflow_index WHERE workflow_id = '%s'", |
| 115 | + workflowId)); |
| 116 | + assertEquals("Wrong number of rows returned", 1, result.size()); |
| 117 | + assertEquals("Wrong status returned", status, result.get(0).get("status")); |
| 118 | + assertEquals( |
| 119 | + "Correlation id does not match", |
| 120 | + correlationId, |
| 121 | + result.get(0).get("correlation_id")); |
| 122 | + } |
| 123 | + |
| 124 | + public void checkTask(String taskId, String status, String updateTime) throws SQLException { |
| 125 | + List<Map<String, Object>> result = |
| 126 | + queryDb(String.format("SELECT * FROM task_index WHERE task_id = '%s'", taskId)); |
| 127 | + assertEquals("Wrong number of rows returned", 1, result.size()); |
| 128 | + assertEquals("Wrong status returned", status, result.get(0).get("status")); |
| 129 | + assertEquals( |
| 130 | + "Update time does not match", |
| 131 | + updateTime, |
| 132 | + result.get(0).get("update_time").toString()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testIndexWorkflowOnlyStatusChange() throws SQLException { |
| 137 | + WorkflowSummary wfs = getMockWorkflowSummary("workflow-id"); |
| 138 | + indexDAO.indexWorkflow(wfs); |
| 139 | + |
| 140 | + // retrieve the record, make sure it exists |
| 141 | + checkWorkflow("workflow-id", "RUNNING", "correlation-id"); |
| 142 | + |
| 143 | + // Change the record, but not the status, and re-index |
| 144 | + wfs.setCorrelationId("new-correlation-id"); |
| 145 | + indexDAO.indexWorkflow(wfs); |
| 146 | + |
| 147 | + // retrieve the record, make sure it hasn't changed |
| 148 | + checkWorkflow("workflow-id", "RUNNING", "correlation-id"); |
| 149 | + |
| 150 | + // Change the status and re-index |
| 151 | + wfs.setStatus(Workflow.WorkflowStatus.FAILED); |
| 152 | + indexDAO.indexWorkflow(wfs); |
| 153 | + |
| 154 | + // retrieve the record, make sure it has changed |
| 155 | + checkWorkflow("workflow-id", "FAILED", "new-correlation-id"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testIndexTaskOnlyStatusChange() throws SQLException { |
| 160 | + TaskSummary ts = getMockTaskSummary("task-id"); |
| 161 | + |
| 162 | + indexDAO.indexTask(ts); |
| 163 | + |
| 164 | + // retrieve the record, make sure it exists |
| 165 | + checkTask("task-id", "SCHEDULED", "2023-02-07 09:42:45.0"); |
| 166 | + |
| 167 | + // Change the record, but not the status |
| 168 | + ts.setUpdateTime("2023-02-07T10:42:45Z"); |
| 169 | + indexDAO.indexTask(ts); |
| 170 | + |
| 171 | + // retrieve the record, make sure it hasn't changed |
| 172 | + checkTask("task-id", "SCHEDULED", "2023-02-07 09:42:45.0"); |
| 173 | + |
| 174 | + // Change the status and re-index |
| 175 | + ts.setStatus(Task.Status.FAILED); |
| 176 | + indexDAO.indexTask(ts); |
| 177 | + |
| 178 | + // retrieve the record, make sure it has changed |
| 179 | + checkTask("task-id", "FAILED", "2023-02-07 10:42:45.0"); |
| 180 | + } |
| 181 | +} |
0 commit comments