github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/integrationArtifactTriggerIntegrationTest_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/SAP/jenkins-library/pkg/mock" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type integrationArtifactTriggerIntegrationTestMockUtils struct { 16 *mock.ExecMockRunner 17 *mock.FilesMock 18 } 19 20 func newIntegrationArtifactTriggerIntegrationTestTestsUtils() integrationArtifactTriggerIntegrationTestMockUtils { 21 utils := integrationArtifactTriggerIntegrationTestMockUtils{ 22 ExecMockRunner: &mock.ExecMockRunner{}, 23 FilesMock: &mock.FilesMock{}, 24 } 25 return utils 26 } 27 28 func TestRunIntegrationArtifactTriggerIntegrationTest(t *testing.T) { 29 t.Run("MessageBodyPath good but no ContentType (ERROR) callIFlowURL", func(t *testing.T) { 30 //init 31 iFlowServiceKey := `{ 32 "oauth": { 33 "url": "https://demo", 34 "clientid": "demouser", 35 "clientsecret": "******", 36 "tokenurl": "https://demo/oauth/token" 37 } 38 }` 39 config := integrationArtifactTriggerIntegrationTestOptions{ 40 IntegrationFlowServiceKey: iFlowServiceKey, 41 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 42 MessageBodyPath: "/file.txt", 43 ContentType: "", 44 } 45 46 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 47 utils.AddFile("file.txt", []byte("dummycontent")) 48 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 49 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 50 51 //test 52 err := callIFlowURL(&config, utils, &httpClient, "", &cpe) 53 54 //assert 55 assert.Error(t, err) 56 }) 57 58 t.Run("MessageBodyPath and ContentType good but file missing (ERROR) callIFlowURL", func(t *testing.T) { 59 //init 60 iFlowServiceKey := `{ 61 "oauth": { 62 "url": "https://demo", 63 "clientid": "demouser", 64 "clientsecret": "******", 65 "tokenurl": "https://demo/oauth/token" 66 } 67 }` 68 config := integrationArtifactTriggerIntegrationTestOptions{ 69 IntegrationFlowServiceKey: iFlowServiceKey, 70 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 71 MessageBodyPath: "test.txt", 72 ContentType: "txt", 73 } 74 75 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 76 //no file created here. error expected 77 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 78 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 79 80 //test 81 err := callIFlowURL(&config, utils, &httpClient, "", &cpe) 82 83 //assert 84 assert.Error(t, err) 85 }) 86 87 t.Run("MessageBodyPath, ContentType, and file good (SUCCESS) callIFlowURL", func(t *testing.T) { 88 dir := t.TempDir() 89 //init 90 iFlowServiceKey := `{ 91 "oauth": { 92 "url": "https://demo", 93 "clientid": "demouser", 94 "clientsecret": "******", 95 "tokenurl": "https://demo/oauth/token" 96 } 97 }` 98 config := integrationArtifactTriggerIntegrationTestOptions{ 99 IntegrationFlowServiceKey: iFlowServiceKey, 100 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 101 MessageBodyPath: filepath.Join(dir, "test.txt"), 102 ContentType: "txt", 103 } 104 105 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 106 utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils 107 if err := os.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755); err != nil { 108 t.Fail() 109 } 110 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 111 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 112 113 //test 114 err := callIFlowURL(&config, utils, &httpClient, "https://my-service.com/endpoint", &cpe) 115 116 //assert 117 assert.NoError(t, err) 118 assert.Equal(t, "POST", httpClient.Method) 119 assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL) 120 }) 121 122 t.Run("No MessageBodyPath still works (SUCCESS) callIFlowURL", func(t *testing.T) { 123 //init 124 iFlowServiceKey := `{ 125 "oauth": { 126 "url": "https://demo", 127 "clientid": "demouser", 128 "clientsecret": "******", 129 "tokenurl": "https://demo/oauth/token" 130 } 131 }` 132 config := integrationArtifactTriggerIntegrationTestOptions{ 133 IntegrationFlowServiceKey: iFlowServiceKey, 134 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 135 MessageBodyPath: "", 136 ContentType: "txt", 137 } 138 139 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 140 //utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils 141 //os.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755) 142 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 143 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 144 145 //test 146 err := callIFlowURL(&config, utils, &httpClient, "https://my-service.com/endpoint", &cpe) 147 148 //assert 149 assert.NoError(t, err) 150 assert.Equal(t, "GET", httpClient.Method) 151 assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL) 152 153 }) 154 155 t.Run("nil fileBody (SUCCESS) callIFlowURL", func(t *testing.T) { 156 dir := t.TempDir() 157 //init 158 iFlowServiceKey := `{ 159 "oauth": { 160 "url": "https://demo", 161 "clientid": "demouser", 162 "clientsecret": "******", 163 "tokenurl": "https://demo/oauth/token" 164 } 165 }` 166 config := integrationArtifactTriggerIntegrationTestOptions{ 167 IntegrationFlowServiceKey: iFlowServiceKey, 168 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 169 MessageBodyPath: filepath.Join(dir, "test.txt"), 170 ContentType: "txt", 171 } 172 173 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 174 utils.AddFile(config.MessageBodyPath, []byte(nil)) //have to add a file here to see in utils 175 if err := os.WriteFile(config.MessageBodyPath, []byte(nil), 0755); err != nil { 176 t.Fail() 177 } 178 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 179 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 180 181 //test 182 err := callIFlowURL(&config, utils, &httpClient, "", &cpe) 183 184 //assert 185 assert.NoError(t, err) 186 }) 187 188 t.Run("success - check correct body and headers in cpe", func(t *testing.T) { 189 //init 190 iFlowServiceKey := `{ 191 "oauth": { 192 "url": "https://demo", 193 "clientid": "demouser", 194 "clientsecret": "******", 195 "tokenurl": "https://demo/oauth/token" 196 } 197 }` 198 config := integrationArtifactTriggerIntegrationTestOptions{ 199 IntegrationFlowServiceKey: iFlowServiceKey, 200 IntegrationFlowID: "CPI_IFlow_Call_using_Cert", 201 ContentType: "txt", 202 } 203 204 utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils() 205 httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"} 206 cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{} 207 208 //test 209 err := callIFlowURL(&config, utils, &httpClient, "", &cpe) 210 211 //assert 212 assert.NoError(t, err) 213 bodyRegexIgnoringWhiteSpaces := "{\\s*\"code\": \"Good Request\",\\s*\"message\": {\\s*\"@lang\": \"en\",\\s*\"#text\": \"valid\"\\s*}\\s*}" 214 assert.Regexp(t, bodyRegexIgnoringWhiteSpaces, cpe.custom.integrationFlowTriggerIntegrationTestResponseBody) 215 assert.Equal(t, "{\"test\":[\"this is a test\"]}", cpe.custom.integrationFlowTriggerIntegrationTestResponseHeaders) 216 }) 217 }