diff --git a/utils/src/test/java/com/cloud/utils/SwiftUtilTest.java b/utils/src/test/java/com/cloud/utils/SwiftUtilTest.java index 6dc2cc78cac5..c3d01aebe743 100644 --- a/utils/src/test/java/com/cloud/utils/SwiftUtilTest.java +++ b/utils/src/test/java/com/cloud/utils/SwiftUtilTest.java @@ -99,12 +99,7 @@ public void testGetContainerName(){ @Test public void testGetSwiftCmd() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String cmd = SwiftUtil.getSwiftCmd(cfg, "swift", "stat"); @@ -114,12 +109,7 @@ public void testGetSwiftCmd() { @Test public void testGetSwiftObjectCmd() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String objectCmd = SwiftUtil.getSwiftObjectCmd(cfg, "swift", "delete", "T-123", "template.vhd"); @@ -129,12 +119,7 @@ public void testGetSwiftObjectCmd() { @Test public void testGetSwiftContainerCmd() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String containerCmd = SwiftUtil.getSwiftContainerCmd(cfg, "swift", "list", "T-123"); @@ -144,27 +129,16 @@ public void testGetSwiftContainerCmd() { @Test public void testGetUploadCmd() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024); - String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd"; assertThat(uploadCmd, is(equalTo(expected))); } @Test public void testGetUploadCmdWithSegmentsBecauseOfSize() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 5368709121L); @@ -174,12 +148,7 @@ public void testGetUploadCmdWithSegmentsBecauseOfSize() { @Test public void testGetUploadCmdWithStoragePolicy() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn("policy1"); + SwiftClientCfg cfg = CreateMockSwiftClientCfg("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\""; @@ -188,12 +157,7 @@ public void testGetUploadCmdWithStoragePolicy() { @Test public void testGetUploadCmdWithSegmentsAndStoragePolicy() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn("policy1"); + SwiftClientCfg cfg = CreateMockSwiftClientCfg("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 5368709121L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\" -S 5368709120"; assertThat(uploadCmd, is(equalTo(expected))); @@ -201,12 +165,7 @@ public void testGetUploadCmdWithSegmentsAndStoragePolicy() { @Test public void testListContainerCmdWithStoragePolicyButNotSupportedByOperation() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn("policy1"); + SwiftClientCfg cfg = CreateMockSwiftClientCfg("policy1"); String uploadCmd = SwiftUtil.getSwiftContainerCmd(cfg, "swift", "list", "T-1"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword list T-1"; @@ -215,15 +174,20 @@ public void testListContainerCmdWithStoragePolicyButNotSupportedByOperation() { @Test public void testListContainerCmdWithoutStoragePolicy() { - SwiftClientCfg cfg = mock(SwiftClientCfg.class); - given(cfg.getEndPoint()).willReturn("swift.endpoint"); - given(cfg.getAccount()).willReturn("cs"); - given(cfg.getUserName()).willReturn("sec-storage"); - given(cfg.getKey()).willReturn("mypassword"); - given(cfg.getStoragePolicy()).willReturn(null); + SwiftClientCfg cfg = CreateMockSwiftClientCfg(null); String uploadCmd = SwiftUtil.getSwiftContainerCmd(cfg, "swift", "list", "T-1"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword list T-1"; assertThat(uploadCmd, is(equalTo(expected))); } + + private SwiftClientCfg CreateMockSwiftClientCfg(String policy){ + SwiftClientCfg cfg = mock(SwiftClientCfg.class);// + given(cfg.getEndPoint()).willReturn("swift.endpoint"); + given(cfg.getAccount()).willReturn("cs"); + given(cfg.getUserName()).willReturn("sec-storage"); + given(cfg.getKey()).willReturn("mypassword"); + given(cfg.getStoragePolicy()).willReturn(policy); + return cfg; + } }