|
| 1 | +/* |
| 2 | + * Copyright 2025 Apollo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package com.ctrip.framework.apollo.portal.controller; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.doAnswer; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import com.ctrip.framework.apollo.common.dto.ItemDTO; |
| 28 | +import com.ctrip.framework.apollo.common.exception.ServiceException; |
| 29 | +import com.ctrip.framework.apollo.portal.entity.bo.ItemBO; |
| 30 | +import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO; |
| 31 | +import com.ctrip.framework.apollo.portal.environment.Env; |
| 32 | +import com.ctrip.framework.apollo.portal.service.ConfigsExportService; |
| 33 | +import com.ctrip.framework.apollo.portal.service.NamespaceService; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.OutputStream; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Collections; |
| 38 | +import javax.servlet.ServletOutputStream; |
| 39 | +import javax.servlet.WriteListener; |
| 40 | +import javax.servlet.http.HttpServletRequest; |
| 41 | +import javax.servlet.http.HttpServletResponse; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.mockito.InjectMocks; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.junit.MockitoJUnitRunner; |
| 47 | +import org.springframework.http.HttpHeaders; |
| 48 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 49 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 50 | + |
| 51 | +@RunWith(MockitoJUnitRunner.class) |
| 52 | +public class ConfigsExportControllerTest { |
| 53 | + |
| 54 | + @Mock |
| 55 | + private ConfigsExportService configsExportService; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private NamespaceService namespaceService; |
| 59 | + |
| 60 | + @InjectMocks |
| 61 | + private ConfigsExportController configsExportController; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldExportNamespaceWithPropertiesSuffixWhenMissing() { |
| 65 | + NamespaceBO namespace = new NamespaceBO(); |
| 66 | + namespace.setFormat("properties"); |
| 67 | + namespace.setItems(Collections.singletonList(itemBO("timeout", "100"))); |
| 68 | + |
| 69 | + when(namespaceService.loadNamespaceBO("SampleApp", Env.DEV, "default", "application", true, |
| 70 | + false)).thenReturn(namespace); |
| 71 | + |
| 72 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 73 | + |
| 74 | + configsExportController.exportItems("SampleApp", "DEV", "default", "application", response); |
| 75 | + |
| 76 | + assertEquals("attachment;filename=application.properties", |
| 77 | + response.getHeader(HttpHeaders.CONTENT_DISPOSITION)); |
| 78 | + assertTrue(new String(response.getContentAsByteArray()).contains("\"key\":\"timeout\"")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldExportNamespaceKeepOriginalSuffixWhenFormatIsValid() { |
| 83 | + NamespaceBO namespace = new NamespaceBO(); |
| 84 | + namespace.setFormat("yml"); |
| 85 | + namespace.setItems(Collections.singletonList(itemBO("content", "a: b"))); |
| 86 | + |
| 87 | + when(namespaceService.loadNamespaceBO("SampleApp", Env.DEV, "default", "application.yml", true, |
| 88 | + false)).thenReturn(namespace); |
| 89 | + |
| 90 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 91 | + |
| 92 | + configsExportController.exportItems("SampleApp", "DEV", "default", "application.yml", response); |
| 93 | + |
| 94 | + assertEquals("attachment;filename=application.yml", |
| 95 | + response.getHeader(HttpHeaders.CONTENT_DISPOSITION)); |
| 96 | + assertTrue(new String(response.getContentAsByteArray()).contains("\"key\":\"content\"")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected = ServiceException.class) |
| 100 | + public void shouldWrapExportNamespaceIOExceptionAsServiceException() throws IOException { |
| 101 | + NamespaceBO namespace = new NamespaceBO(); |
| 102 | + namespace.setFormat("properties"); |
| 103 | + namespace.setItems(Collections.singletonList(itemBO("timeout", "100"))); |
| 104 | + |
| 105 | + when(namespaceService.loadNamespaceBO("SampleApp", Env.DEV, "default", "application", true, |
| 106 | + false)).thenReturn(namespace); |
| 107 | + |
| 108 | + HttpServletResponse response = org.mockito.Mockito.mock(HttpServletResponse.class); |
| 109 | + when(response.getOutputStream()).thenReturn(new FailingServletOutputStream()); |
| 110 | + |
| 111 | + configsExportController.exportItems("SampleApp", "DEV", "default", "application", response); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void shouldExportAllConfigsWithParsedEnvs() throws IOException { |
| 116 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 117 | + request.setRemoteAddr("127.0.0.1"); |
| 118 | + request.setRemoteHost("localhost"); |
| 119 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 120 | + |
| 121 | + doAnswer(invocation -> { |
| 122 | + OutputStream outputStream = invocation.getArgument(0); |
| 123 | + outputStream.write("ok".getBytes()); |
| 124 | + return null; |
| 125 | + }).when(configsExportService).exportData(any(OutputStream.class), |
| 126 | + eq(Arrays.asList(Env.DEV, Env.FAT))); |
| 127 | + |
| 128 | + configsExportController.exportAll("DEV,FAT", request, response); |
| 129 | + |
| 130 | + verify(configsExportService).exportData(any(OutputStream.class), |
| 131 | + eq(Arrays.asList(Env.DEV, Env.FAT))); |
| 132 | + assertTrue(response.getHeader(HttpHeaders.CONTENT_DISPOSITION) |
| 133 | + .startsWith("attachment;filename=apollo_config_export_")); |
| 134 | + assertEquals("ok", response.getContentAsString()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void shouldExportAppConfigByEnvAndCluster() throws IOException { |
| 139 | + HttpServletRequest request = new MockHttpServletRequest(); |
| 140 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 141 | + |
| 142 | + doAnswer(invocation -> { |
| 143 | + OutputStream outputStream = invocation.getArgument(3); |
| 144 | + outputStream.write("app".getBytes()); |
| 145 | + return null; |
| 146 | + }).when(configsExportService).exportAppConfigByEnvAndCluster(eq("SampleApp"), eq(Env.DEV), |
| 147 | + eq("default"), any(OutputStream.class)); |
| 148 | + |
| 149 | + configsExportController.exportAppConfig("SampleApp", "DEV", "default", request, response); |
| 150 | + |
| 151 | + verify(configsExportService).exportAppConfigByEnvAndCluster(eq("SampleApp"), eq(Env.DEV), |
| 152 | + eq("default"), any(OutputStream.class)); |
| 153 | + assertTrue(response.getHeader(HttpHeaders.CONTENT_DISPOSITION) |
| 154 | + .startsWith("attachment;filename=SampleApp+DEV+default+")); |
| 155 | + assertEquals("app", response.getContentAsString()); |
| 156 | + } |
| 157 | + |
| 158 | + private ItemBO itemBO(String key, String value) { |
| 159 | + ItemDTO itemDTO = new ItemDTO(); |
| 160 | + itemDTO.setKey(key); |
| 161 | + itemDTO.setValue(value); |
| 162 | + ItemBO itemBO = new ItemBO(); |
| 163 | + itemBO.setItem(itemDTO); |
| 164 | + return itemBO; |
| 165 | + } |
| 166 | + |
| 167 | + private static class FailingServletOutputStream extends ServletOutputStream { |
| 168 | + |
| 169 | + @Override |
| 170 | + public void write(int b) throws IOException { |
| 171 | + throw new IOException("forced write failure"); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public boolean isReady() { |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void setWriteListener(WriteListener writeListener) { |
| 181 | + // no-op |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments