@Override
    public boolean map(URL url) {
        if (CollectionUtils.isEmpty(
                applicationModel.getApplicationConfigManager().getMetadataConfigs())) {
            logger.warn(
                    COMMON_PROPERTY_TYPE_MISMATCH, "", "", "No valid metadata config center found for mapping report.");
            return false;
        }
        String serviceInterface = url.getServiceInterface();
        if (IGNORED_SERVICE_INTERFACES.contains(serviceInterface)) {
            return true;
        }

        boolean result = true;
        for (Map.Entry<String, MetadataReport> entry :
                metadataReportInstance.getMetadataReports(true).entrySet()) {
            MetadataReport metadataReport = entry.getValue();
            String appName = applicationModel.getApplicationName();
            try {
                if (metadataReport.registerServiceAppMapping(serviceInterface, appName, url)) {
                    // MetadataReport support directly register service-app mapping
                    continue;
                }

                boolean succeeded = false;
                int currentRetryTimes = 1;
                String newConfigContent = appName;
                do {
                    ConfigItem configItem = metadataReport.getConfigItem(serviceInterface, DEFAULT_MAPPING_GROUP);
                    String oldConfigContent = configItem.getContent();
                    if (StringUtils.isNotEmpty(oldConfigContent)) {
                        String[] oldAppNames = oldConfigContent.split(",");
                        if (oldAppNames.length > 0) {
                            for (String oldAppName : oldAppNames) {
                                if (StringUtils.trim(oldAppName).equals(appName)) {
                                    succeeded = true;
                                    break;
                                }
                            }
                        }
                        if (succeeded) {
                            break;
                        }
                        newConfigContent = oldConfigContent + COMMA_SEPARATOR + appName;
                    }
                    succeeded = metadataReport.registerServiceAppMapping(
                            serviceInterface, DEFAULT_MAPPING_GROUP, newConfigContent, configItem.getTicket());
                    if (!succeeded) {
                        int waitTime = ThreadLocalRandom.current().nextInt(casRetryWaitTime);
                        logger.info("Failed to publish service name mapping to metadata center by cas operation. "
                                + "Times: "
                                + currentRetryTimes + ". " + "Next retry delay: "
                                + waitTime + ". " + "Service Interface: "
                                + serviceInterface + ". " + "Origin Content: "
                                + oldConfigContent + ". " + "Ticket: "
                                + configItem.getTicket() + ". " + "Excepted context: "
                                + newConfigContent);
                        Thread.sleep(waitTime);
                    }
                } while (!succeeded && currentRetryTimes++ <= casRetryTimes);

                if (!succeeded) {
                    result = false;
                }
            } catch (Exception e) {
                result = false;
                logger.warn(
                        INTERNAL_ERROR,
                        "unknown error in registry module",
                        "",
                        "Failed registering mapping to remote." + metadataReport,
                        e);
            }
        }

        return result;
    }
@Test
    void testMap() {
        ApplicationModel mockedApplicationModel = spy(applicationModel);

        when(configManager.getMetadataConfigs()).thenReturn(Collections.emptyList());
        Mockito.when(mockedApplicationModel.getApplicationConfigManager()).thenReturn(configManager);
        Mockito.when(mockedApplicationModel.getCurrentConfig()).thenReturn(new ApplicationConfig("test"));

        // metadata report config not found
        mapping.setApplicationModel(mockedApplicationModel);
        boolean result = mapping.map(url);
        assertFalse(result);

        when(configManager.getMetadataConfigs()).thenReturn(Arrays.asList(new MetadataReportConfig()));
        MetadataReportInstance reportInstance = mock(MetadataReportInstance.class);
        Mockito.when(reportInstance.getMetadataReports(true)).thenReturn(metadataReportList);
        mapping.metadataReportInstance = reportInstance;

        when(metadataReport.registerServiceAppMapping(any(), any(), any())).thenReturn(true);

        // metadata report directly
        result = mapping.map(url);
        assertTrue(result);

        // metadata report using cas and retry, succeeded after retried 10 times
        when(metadataReport.registerServiceAppMapping(any(), any(), any())).thenReturn(false);
        when(metadataReport.getConfigItem(any(), any())).thenReturn(new ConfigItem());
        when(metadataReport.registerServiceAppMapping(any(), any(), any(), any()))
                .thenAnswer(new Answer<Boolean>() {
                    private int counter = 0;

                    @Override
                    public Boolean answer(InvocationOnMock invocationOnMock) {
                        if (++counter == 10) {
                            return true;
                        }
                        return false;
                    }
                });
        assertTrue(mapping.map(url));

        // metadata report using cas and retry, failed after 11 times retry
        when(metadataReport.registerServiceAppMapping(any(), any(), any(), any()))
                .thenReturn(false);
        Exception exceptionExpected = null;
        assertFalse(mapping.map(url));
    }