|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package e2e |
| 19 | + |
| 20 | +import ( |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/apache/incubator-devlake/core/models/common" |
| 26 | + "github.com/apache/incubator-devlake/core/models/domainlayer" |
| 27 | + "github.com/apache/incubator-devlake/core/models/domainlayer/codequality" |
| 28 | + coremigrations "github.com/apache/incubator-devlake/core/models/migrationscripts" |
| 29 | + "github.com/apache/incubator-devlake/core/plugin" |
| 30 | + "github.com/apache/incubator-devlake/helpers/e2ehelper" |
| 31 | + implcontext "github.com/apache/incubator-devlake/impls/context" |
| 32 | + "github.com/apache/incubator-devlake/plugins/sonarqube/impl" |
| 33 | + "github.com/apache/incubator-devlake/plugins/sonarqube/models" |
| 34 | + sonarqubemigrations "github.com/apache/incubator-devlake/plugins/sonarqube/models/migrationscripts" |
| 35 | + "github.com/apache/incubator-devlake/plugins/sonarqube/tasks" |
| 36 | + "github.com/stretchr/testify/require" |
| 37 | +) |
| 38 | + |
| 39 | +type sonarqubeIssueCodeBlockBeforeText struct { |
| 40 | + ConnectionId uint64 `gorm:"primaryKey"` |
| 41 | + Id string `gorm:"primaryKey"` |
| 42 | + IssueKey string `gorm:"index"` |
| 43 | + Component string `gorm:"index;type:varchar(500)"` |
| 44 | + StartLine int |
| 45 | + EndLine int |
| 46 | + StartOffset int |
| 47 | + EndOffset int |
| 48 | + Msg string |
| 49 | + common.NoPKModel |
| 50 | +} |
| 51 | + |
| 52 | +func (sonarqubeIssueCodeBlockBeforeText) TableName() string { |
| 53 | + return "_tool_sonarqube_issue_code_blocks" |
| 54 | +} |
| 55 | + |
| 56 | +type cqIssueCodeBlockBeforeText struct { |
| 57 | + domainlayer.DomainEntity |
| 58 | + IssueKey string `json:"key" gorm:"index"` |
| 59 | + Component string `gorm:"index"` |
| 60 | + StartLine int |
| 61 | + EndLine int |
| 62 | + StartOffset int |
| 63 | + EndOffset int |
| 64 | + Msg string |
| 65 | +} |
| 66 | + |
| 67 | +func (cqIssueCodeBlockBeforeText) TableName() string { |
| 68 | + return "cq_issue_code_blocks" |
| 69 | +} |
| 70 | + |
| 71 | +func TestSonarqubeIssueCodeBlockLongComponent(t *testing.T) { |
| 72 | + var sonarqube impl.Sonarqube |
| 73 | + dataflowTester := e2ehelper.NewDataFlowTester(t, "sonarqube", sonarqube) |
| 74 | + dataflowTester.FlushTabler(&models.SonarqubeIssue{}) |
| 75 | + require.NoError(t, dataflowTester.Db.Migrator().DropTable( |
| 76 | + &sonarqubeIssueCodeBlockBeforeText{}, |
| 77 | + &cqIssueCodeBlockBeforeText{}, |
| 78 | + )) |
| 79 | + require.NoError(t, dataflowTester.Db.AutoMigrate( |
| 80 | + &sonarqubeIssueCodeBlockBeforeText{}, |
| 81 | + &cqIssueCodeBlockBeforeText{}, |
| 82 | + )) |
| 83 | + |
| 84 | + existingComponent := "existing:component" |
| 85 | + require.NoError(t, dataflowTester.Db.Create(&sonarqubeIssueCodeBlockBeforeText{ |
| 86 | + ConnectionId: 1, |
| 87 | + Id: "existing-tool-block", |
| 88 | + IssueKey: "existing-issue", |
| 89 | + Component: existingComponent, |
| 90 | + }).Error) |
| 91 | + require.NoError(t, dataflowTester.Db.Create(&cqIssueCodeBlockBeforeText{ |
| 92 | + DomainEntity: domainlayer.DomainEntity{Id: "existing-domain-block"}, |
| 93 | + IssueKey: "existing-domain-issue", |
| 94 | + Component: existingComponent, |
| 95 | + }).Error) |
| 96 | + |
| 97 | + basicRes := implcontext.NewDefaultBasicRes(dataflowTester.Cfg, dataflowTester.Log, dataflowTester.Dal) |
| 98 | + runMigration(t, coremigrations.All(), "change cq_issue_code_blocks.component type to text", basicRes) |
| 99 | + runMigration(t, sonarqubemigrations.All(), "change _tool_sonarqube_issue_code_blocks.component type to text", basicRes) |
| 100 | + assertTextColumnWithoutIndex(t, dataflowTester, "cq_issue_code_blocks") |
| 101 | + assertTextColumnWithoutIndex(t, dataflowTester, "_tool_sonarqube_issue_code_blocks") |
| 102 | + |
| 103 | + var migratedToolBlock sonarqubeIssueCodeBlockBeforeText |
| 104 | + require.NoError(t, dataflowTester.Db.First(&migratedToolBlock, "id = ?", "existing-tool-block").Error) |
| 105 | + require.Equal(t, existingComponent, migratedToolBlock.Component) |
| 106 | + var migratedDomainBlock cqIssueCodeBlockBeforeText |
| 107 | + require.NoError(t, dataflowTester.Db.First(&migratedDomainBlock, "id = ?", "existing-domain-block").Error) |
| 108 | + require.Equal(t, existingComponent, migratedDomainBlock.Component) |
| 109 | + require.NoError(t, dataflowTester.Db.Delete(&migratedToolBlock).Error) |
| 110 | + require.NoError(t, dataflowTester.Db.Delete(&migratedDomainBlock).Error) |
| 111 | + |
| 112 | + longComponent256 := "project:" + strings.Repeat("a", 256) |
| 113 | + longComponent500 := "project:" + strings.Repeat("b", 500) |
| 114 | + require.Greater(t, len(longComponent256), 256) |
| 115 | + require.Greater(t, len(longComponent500), 500) |
| 116 | + |
| 117 | + issueKey := "TEST-LONG-COMPONENT-ISSUE" |
| 118 | + projectKey := "test-long-component-project" |
| 119 | + result := dataflowTester.Db.Create(&models.SonarqubeIssue{ |
| 120 | + ConnectionId: 1, |
| 121 | + IssueKey: issueKey, |
| 122 | + ProjectKey: projectKey, |
| 123 | + Component: longComponent500, |
| 124 | + Rule: "java:S3776", |
| 125 | + Severity: "CRITICAL", |
| 126 | + }) |
| 127 | + require.NoError(t, result.Error) |
| 128 | + |
| 129 | + codeBlocks := []*models.SonarqubeIssueCodeBlock{ |
| 130 | + { |
| 131 | + ConnectionId: 1, |
| 132 | + Id: "test-long-component-block-256", |
| 133 | + IssueKey: issueKey, |
| 134 | + Component: longComponent256, |
| 135 | + Msg: "component longer than 256 characters", |
| 136 | + }, |
| 137 | + { |
| 138 | + ConnectionId: 1, |
| 139 | + Id: "test-long-component-block-500", |
| 140 | + IssueKey: issueKey, |
| 141 | + Component: longComponent500, |
| 142 | + Msg: "component longer than 500 characters", |
| 143 | + }, |
| 144 | + } |
| 145 | + for _, block := range codeBlocks { |
| 146 | + require.NoError(t, dataflowTester.Db.Create(block).Error) |
| 147 | + } |
| 148 | + |
| 149 | + dataflowTester.Subtask(tasks.ConvertIssueCodeBlocksMeta, &tasks.SonarqubeTaskData{ |
| 150 | + Options: &tasks.SonarqubeOptions{ |
| 151 | + ConnectionId: 1, |
| 152 | + ProjectKey: projectKey, |
| 153 | + }, |
| 154 | + TaskStartTime: time.Now(), |
| 155 | + }) |
| 156 | + |
| 157 | + var domainBlocks []codequality.CqIssueCodeBlock |
| 158 | + require.NoError(t, dataflowTester.Db.Find(&domainBlocks).Error) |
| 159 | + require.Len(t, domainBlocks, 2) |
| 160 | + require.ElementsMatch(t, |
| 161 | + []string{longComponent256, longComponent500}, |
| 162 | + []string{domainBlocks[0].Component, domainBlocks[1].Component}, |
| 163 | + ) |
| 164 | + |
| 165 | + var toolBlocks []models.SonarqubeIssueCodeBlock |
| 166 | + require.NoError(t, dataflowTester.Db.Where( |
| 167 | + "connection_id = ? AND issue_key = ?", 1, issueKey, |
| 168 | + ).Find(&toolBlocks).Error) |
| 169 | + require.Len(t, toolBlocks, 2) |
| 170 | + require.ElementsMatch(t, |
| 171 | + []string{longComponent256, longComponent500}, |
| 172 | + []string{toolBlocks[0].Component, toolBlocks[1].Component}, |
| 173 | + ) |
| 174 | +} |
| 175 | + |
| 176 | +func runMigration(t *testing.T, scripts []plugin.MigrationScript, name string, basicRes *implcontext.DefaultBasicRes) { |
| 177 | + t.Helper() |
| 178 | + for _, script := range scripts { |
| 179 | + if script.Name() == name { |
| 180 | + require.NoError(t, script.Up(basicRes)) |
| 181 | + return |
| 182 | + } |
| 183 | + } |
| 184 | + require.Fail(t, "migration is not registered", name) |
| 185 | +} |
| 186 | + |
| 187 | +func assertTextColumnWithoutIndex(t *testing.T, dataflowTester *e2ehelper.DataFlowTester, table string) { |
| 188 | + t.Helper() |
| 189 | + columnTypes, err := dataflowTester.Db.Migrator().ColumnTypes(table) |
| 190 | + require.NoError(t, err) |
| 191 | + for _, columnType := range columnTypes { |
| 192 | + if columnType.Name() == "component" { |
| 193 | + require.Contains(t, strings.ToLower(columnType.DatabaseTypeName()), "text") |
| 194 | + require.False(t, dataflowTester.Db.Migrator().HasIndex(table, "idx_"+table+"_component")) |
| 195 | + return |
| 196 | + } |
| 197 | + } |
| 198 | + require.Fail(t, "component column not found", table) |
| 199 | +} |
0 commit comments