|
87 | 87 | import org.apache.cloudstack.framework.extensions.dao.ExtensionResourceMapDetailsDao; |
88 | 88 | import org.apache.cloudstack.framework.extensions.vo.ExtensionCustomActionDetailsVO; |
89 | 89 | import org.apache.cloudstack.framework.extensions.vo.ExtensionCustomActionVO; |
| 90 | +import org.apache.cloudstack.framework.extensions.vo.ExtensionResourceMapDetailsVO; |
90 | 91 | import org.apache.cloudstack.framework.extensions.vo.ExtensionResourceMapVO; |
91 | 92 | import org.apache.cloudstack.framework.extensions.vo.ExtensionVO; |
92 | 93 | import org.apache.cloudstack.utils.identity.ManagementServerNode; |
93 | 94 | import org.junit.Before; |
94 | 95 | import org.junit.Test; |
95 | 96 | import org.junit.runner.RunWith; |
| 97 | +import org.mockito.ArgumentCaptor; |
96 | 98 | import org.mockito.InjectMocks; |
97 | 99 | import org.mockito.Mock; |
98 | 100 | import org.mockito.MockedStatic; |
@@ -1124,6 +1126,102 @@ public void updateRegisteredExtensionWithResourceUpdatesDetailsForExistingMappin |
1124 | 1126 | verify(extensionResourceMapDetailsDao).saveDetails(any()); |
1125 | 1127 | } |
1126 | 1128 |
|
| 1129 | + @Test |
| 1130 | + public void updateRegisteredExtensionWithResourceCleanupDetailsFirstThenSaveRequested() { |
| 1131 | + UpdateRegisteredExtensionCmd cmd = mock(UpdateRegisteredExtensionCmd.class); |
| 1132 | + when(cmd.getResourceType()).thenReturn(ExtensionResourceMap.ResourceType.PhysicalNetwork.name()); |
| 1133 | + when(cmd.getResourceId()).thenReturn("physnet-uuid"); |
| 1134 | + when(cmd.getExtensionId()).thenReturn(1L); |
| 1135 | + when(cmd.getDetails()).thenReturn(Map.of("username", "root", "password", "secret")); |
| 1136 | + when(cmd.isCleanupDetails()).thenReturn(true); |
| 1137 | + |
| 1138 | + ExtensionVO extension = mock(ExtensionVO.class); |
| 1139 | + when(extensionDao.findById(1L)).thenReturn(extension); |
| 1140 | + |
| 1141 | + PhysicalNetworkVO physicalNetwork = mock(PhysicalNetworkVO.class); |
| 1142 | + when(physicalNetwork.getId()).thenReturn(42L); |
| 1143 | + when(physicalNetworkDao.findByUuid("physnet-uuid")).thenReturn(physicalNetwork); |
| 1144 | + |
| 1145 | + ExtensionResourceMapVO existing = mock(ExtensionResourceMapVO.class); |
| 1146 | + when(existing.getExtensionId()).thenReturn(1L); |
| 1147 | + when(existing.getId()).thenReturn(100L); |
| 1148 | + when(extensionResourceMapDao.listByResourceIdAndType(42L, ExtensionResourceMap.ResourceType.PhysicalNetwork)) |
| 1149 | + .thenReturn(List.of(existing)); |
| 1150 | + |
| 1151 | + extensionsManager.updateRegisteredExtensionWithResource(cmd); |
| 1152 | + |
| 1153 | + verify(extensionResourceMapDetailsDao).removeDetails(100L); |
| 1154 | + verify(extensionResourceMapDetailsDao, never()).saveDetails(any()); |
| 1155 | + } |
| 1156 | + |
| 1157 | + @Test |
| 1158 | + @SuppressWarnings("unchecked") |
| 1159 | + public void updateRegisteredExtensionWithResourceStoresSensitiveDetailsWithDisplayFalse() { |
| 1160 | + UpdateRegisteredExtensionCmd cmd = mock(UpdateRegisteredExtensionCmd.class); |
| 1161 | + when(cmd.getResourceType()).thenReturn(ExtensionResourceMap.ResourceType.PhysicalNetwork.name()); |
| 1162 | + when(cmd.getResourceId()).thenReturn("physnet-uuid"); |
| 1163 | + when(cmd.getExtensionId()).thenReturn(1L); |
| 1164 | + when(cmd.getDetails()).thenReturn(Map.of("username", "root", "password", "newSecret")); |
| 1165 | + when(cmd.isCleanupDetails()).thenReturn(false); |
| 1166 | + |
| 1167 | + ExtensionVO extension = mock(ExtensionVO.class); |
| 1168 | + when(extensionDao.findById(1L)).thenReturn(extension); |
| 1169 | + |
| 1170 | + PhysicalNetworkVO physicalNetwork = mock(PhysicalNetworkVO.class); |
| 1171 | + when(physicalNetwork.getId()).thenReturn(42L); |
| 1172 | + when(physicalNetworkDao.findByUuid("physnet-uuid")).thenReturn(physicalNetwork); |
| 1173 | + |
| 1174 | + ExtensionResourceMapVO existing = mock(ExtensionResourceMapVO.class); |
| 1175 | + when(existing.getExtensionId()).thenReturn(1L); |
| 1176 | + when(existing.getId()).thenReturn(100L); |
| 1177 | + when(extensionResourceMapDao.listByResourceIdAndType(42L, ExtensionResourceMap.ResourceType.PhysicalNetwork)) |
| 1178 | + .thenReturn(List.of(existing)); |
| 1179 | + extensionsManager.updateRegisteredExtensionWithResource(cmd); |
| 1180 | + |
| 1181 | + ArgumentCaptor<List<ExtensionResourceMapDetailsVO>> captor = ArgumentCaptor.forClass(List.class); |
| 1182 | + verify(extensionResourceMapDetailsDao).saveDetails(captor.capture()); |
| 1183 | + verify(extensionResourceMapDetailsDao, never()).removeDetails(anyLong()); |
| 1184 | + List<ExtensionResourceMapDetailsVO> savedDetails = captor.getValue(); |
| 1185 | + |
| 1186 | + ExtensionResourceMapDetailsVO passwordDetail = savedDetails.stream() |
| 1187 | + .filter(detail -> "password".equals(detail.getName())) |
| 1188 | + .findFirst() |
| 1189 | + .orElse(null); |
| 1190 | + assertNotNull(passwordDetail); |
| 1191 | + assertFalse(passwordDetail.isDisplay()); |
| 1192 | + assertEquals("newSecret", passwordDetail.getValue()); |
| 1193 | + } |
| 1194 | + |
| 1195 | + @Test |
| 1196 | + @SuppressWarnings("unchecked") |
| 1197 | + public void registerExtensionWithClusterStoresSensitiveDetailsWithDisplayFalse() { |
| 1198 | + Cluster cluster = mock(Cluster.class); |
| 1199 | + when(cluster.getId()).thenReturn(12L); |
| 1200 | + when(cluster.getName()).thenReturn("cluster-12"); |
| 1201 | + when(cluster.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.External); |
| 1202 | + |
| 1203 | + Extension extension = mock(Extension.class); |
| 1204 | + when(extension.getId()).thenReturn(5L); |
| 1205 | + |
| 1206 | + ExtensionResourceMapVO persistedMap = mock(ExtensionResourceMapVO.class); |
| 1207 | + when(persistedMap.getId()).thenReturn(120L); |
| 1208 | + when(extensionResourceMapDao.persist(any())).thenReturn(persistedMap); |
| 1209 | + |
| 1210 | + extensionsManager.registerExtensionWithCluster(cluster, extension, |
| 1211 | + Map.of("username", "admin", "password", "s3cr3t")); |
| 1212 | + |
| 1213 | + ArgumentCaptor<List<ExtensionResourceMapDetailsVO>> captor = ArgumentCaptor.forClass(List.class); |
| 1214 | + verify(extensionResourceMapDetailsDao).saveDetails(captor.capture()); |
| 1215 | + List<ExtensionResourceMapDetailsVO> savedDetails = captor.getValue(); |
| 1216 | + |
| 1217 | + ExtensionResourceMapDetailsVO passwordDetail = savedDetails.stream() |
| 1218 | + .filter(detail -> "password".equals(detail.getName())) |
| 1219 | + .findFirst() |
| 1220 | + .orElse(null); |
| 1221 | + assertNotNull(passwordDetail); |
| 1222 | + assertFalse(passwordDetail.isDisplay()); |
| 1223 | + } |
| 1224 | + |
1127 | 1225 | @Test |
1128 | 1226 | public void testCreateExtensionResponse_BasicFields() { |
1129 | 1227 | Extension extension = mock(Extension.class); |
|
0 commit comments