|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.admin.service.impl; |
| 19 | + |
| 20 | +import org.apache.shenyu.admin.listener.DataChangedEvent; |
| 21 | +import org.apache.shenyu.admin.mapper.AppAuthMapper; |
| 22 | +import org.apache.shenyu.admin.mapper.AppAuthRequestMapper; |
| 23 | +import org.apache.shenyu.admin.mapper.AuthParamMapper; |
| 24 | +import org.apache.shenyu.admin.mapper.AuthPathMapper; |
| 25 | +import org.apache.shenyu.admin.model.dto.AppAuthApplyRequestDTO; |
| 26 | +import org.apache.shenyu.admin.model.dto.AppAuthReplacePathsRequestDTO; |
| 27 | +import org.apache.shenyu.admin.model.dto.AppAuthResultQueryDTO; |
| 28 | +import org.apache.shenyu.admin.model.dto.AppAuthRevokeRequestDTO; |
| 29 | +import org.apache.shenyu.admin.model.vo.AppAuthResultVO; |
| 30 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 31 | +import org.h2.jdbcx.JdbcDataSource; |
| 32 | +import org.h2.tools.RunScript; |
| 33 | +import org.junit.jupiter.api.AfterAll; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.mybatis.spring.SqlSessionFactoryBean; |
| 37 | +import org.mybatis.spring.mapper.MapperFactoryBean; |
| 38 | +import org.springframework.context.ApplicationEventPublisher; |
| 39 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Configuration; |
| 42 | +import org.springframework.core.io.ClassPathResource; |
| 43 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 44 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 45 | + |
| 46 | +import javax.sql.DataSource; |
| 47 | +import java.nio.charset.StandardCharsets; |
| 48 | +import java.nio.file.Paths; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Objects; |
| 51 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 52 | + |
| 53 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 54 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 55 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 56 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 57 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 58 | + |
| 59 | +/** |
| 60 | + * Lightweight H2 coverage for the apply/result/revoke/replacePaths transaction service: |
| 61 | + * no external MySQL and no system-property gate, so it runs in the default surefire pass |
| 62 | + * (the deep MySQL-fixture suites stay in AppAuthApplyTest and friends). |
| 63 | + * Covers first apply, same-parameter replay, different-parameter rejection, replacePaths |
| 64 | + * null/empty/whole-replace semantics, the result tri-state and the revoke tombstone. |
| 65 | + */ |
| 66 | +class AppAuthTransactionServiceH2Test { |
| 67 | + |
| 68 | + private static AnnotationConfigApplicationContext context; |
| 69 | + |
| 70 | + private static AppAuthTransactionService txService; |
| 71 | + |
| 72 | + /** Recorded data-changed events (visible after commit). */ |
| 73 | + private static final List<Object> PUBLISHED_EVENTS = new CopyOnWriteArrayList<>(); |
| 74 | + |
| 75 | + @BeforeAll |
| 76 | + static void setUp() { |
| 77 | + context = new AnnotationConfigApplicationContext(H2TxConfig.class); |
| 78 | + txService = context.getBean(AppAuthTransactionService.class); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterAll |
| 82 | + static void tearDown() { |
| 83 | + if (Objects.nonNull(context)) { |
| 84 | + context.close(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static AppAuthApplyRequestDTO applyRequest(final String requestId) { |
| 89 | + final AppAuthApplyRequestDTO dto = new AppAuthApplyRequestDTO(); |
| 90 | + dto.setNamespaceId("ns-h2"); |
| 91 | + dto.setRequestId(requestId); |
| 92 | + dto.setAppName("h2-app"); |
| 93 | + dto.setUserId("u1"); |
| 94 | + dto.setPathList(List.of("/a", "/b")); |
| 95 | + return dto; |
| 96 | + } |
| 97 | + |
| 98 | + private static AppAuthResultQueryDTO query(final String requestId) { |
| 99 | + final AppAuthResultQueryDTO q = new AppAuthResultQueryDTO(); |
| 100 | + q.setNamespaceId("ns-h2"); |
| 101 | + q.setRequestId(requestId); |
| 102 | + return q; |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void applyReplayReplacePathsRevokeLifecycle() { |
| 107 | + // first apply: CREATED with credentials, enabled=false/open=true fixed |
| 108 | + final AppAuthResultVO first = txService.apply("caller", applyRequest("r1"), "hash-v2"); |
| 109 | + assertEquals("CREATED", first.getState()); |
| 110 | + assertNotNull(first.getAppKey()); |
| 111 | + assertNotNull(first.getAppSecret()); |
| 112 | + assertEquals(Boolean.FALSE, first.getEnabled()); |
| 113 | + assertEquals(Boolean.TRUE, first.getOpen()); |
| 114 | + |
| 115 | + // same-parameter replay returns the same credentials |
| 116 | + final AppAuthResultVO replay = txService.apply("caller", applyRequest("r1"), "hash-v2"); |
| 117 | + assertEquals(first.getId(), replay.getId()); |
| 118 | + assertEquals(first.getAppKey(), replay.getAppKey()); |
| 119 | + |
| 120 | + // same requestId with different parameters is rejected (the facade supplies the digest, so different |
| 121 | + // parameters arrive here as a different digest for the same requestId) |
| 122 | + final AppAuthApplyRequestDTO different = applyRequest("r1"); |
| 123 | + different.setAppName("other-app"); |
| 124 | + assertThrows(Exception.class, () -> txService.apply("caller", different, "hash-different")); |
| 125 | + |
| 126 | + // replacePaths: null is rejected, empty clears, non-empty replaces as a whole (de-duplicated) |
| 127 | + final AppAuthReplacePathsRequestDTO replace = new AppAuthReplacePathsRequestDTO(); |
| 128 | + replace.setNamespaceId("ns-h2"); |
| 129 | + replace.setAuthId(first.getId()); |
| 130 | + replace.setPathList(null); |
| 131 | + assertThrows(Exception.class, () -> txService.replacePaths("caller", replace)); |
| 132 | + replace.setPathList(List.of()); |
| 133 | + assertEquals(0, txService.replacePaths("caller", replace).getPathList().size()); |
| 134 | + replace.setPathList(List.of("/x", "/x", "/y")); |
| 135 | + final List<String> replaced = txService.replacePaths("caller", replace).getPathList(); |
| 136 | + assertEquals(2, replaced.size()); |
| 137 | + assertTrue(replaced.contains("/x") && replaced.contains("/y")); |
| 138 | + |
| 139 | + // result tri-state: NOT_FOUND for unknown keys, CREATED (with credentials) for r1 |
| 140 | + assertEquals("NOT_FOUND", txService.result("caller", query("r-none")).getState()); |
| 141 | + final AppAuthResultVO created = txService.result("caller", query("r1")); |
| 142 | + assertEquals("CREATED", created.getState()); |
| 143 | + assertEquals(first.getAppKey(), created.getAppKey()); |
| 144 | + |
| 145 | + // revoke is idempotent and installs a tombstone that rejects late applies |
| 146 | + final AppAuthRevokeRequestDTO revoke = new AppAuthRevokeRequestDTO(); |
| 147 | + revoke.setNamespaceId("ns-h2"); |
| 148 | + revoke.setRequestId("r1"); |
| 149 | + revoke.setReason("done"); |
| 150 | + assertEquals("REVOKED", txService.revoke("caller", revoke).getState()); |
| 151 | + assertEquals("REVOKED", txService.revoke("caller", revoke).getState()); |
| 152 | + assertEquals("REVOKED", txService.result("caller", query("r1")).getState()); |
| 153 | + assertNull(txService.result("caller", query("r1")).getAppKey()); |
| 154 | + assertThrows(Exception.class, () -> txService.apply("caller", applyRequest("r1"), "hash-v2")); |
| 155 | + |
| 156 | + // commit-time sync events fired for create/update/delete |
| 157 | + assertTrue(PUBLISHED_EVENTS.stream().allMatch(DataChangedEvent.class::isInstance)); |
| 158 | + assertTrue(PUBLISHED_EVENTS.size() >= 3); |
| 159 | + } |
| 160 | + |
| 161 | + /** H2 in-memory MySQL-mode database initialized from the maintained H2 schema (includes app_auth_request). */ |
| 162 | + @Configuration |
| 163 | + @EnableTransactionManagement |
| 164 | + static class H2TxConfig { |
| 165 | + |
| 166 | + @Bean |
| 167 | + public DataSource dataSource() throws Exception { |
| 168 | + final JdbcDataSource ds = new JdbcDataSource(); |
| 169 | + ds.setURL("jdbc:h2:mem:appauthtx;MODE=MySQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1"); |
| 170 | + try (java.sql.Connection connection = ds.getConnection()) { |
| 171 | + RunScript.execute(connection, java.nio.file.Files.newBufferedReader( |
| 172 | + Paths.get("src/main/resources/sql-script/h2/schema.sql"), StandardCharsets.UTF_8)); |
| 173 | + } |
| 174 | + return ds; |
| 175 | + } |
| 176 | + |
| 177 | + @Bean |
| 178 | + public DataSourceTransactionManager transactionManager(final DataSource dataSource) { |
| 179 | + return new DataSourceTransactionManager(dataSource); |
| 180 | + } |
| 181 | + |
| 182 | + @Bean |
| 183 | + public SqlSessionFactory sqlSessionFactory(final DataSource dataSource) throws Exception { |
| 184 | + final SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); |
| 185 | + factoryBean.setDataSource(dataSource); |
| 186 | + factoryBean.setTypeAliasesPackage("org.apache.shenyu.admin.model.entity"); |
| 187 | + factoryBean.setMapperLocations( |
| 188 | + new ClassPathResource("mappers/app-auth-request-sqlmap.xml"), |
| 189 | + new ClassPathResource("mappers/app-auth-sqlmap.xml"), |
| 190 | + new ClassPathResource("mappers/auth-param-sqlmap.xml"), |
| 191 | + new ClassPathResource("mappers/auth-path-sqlmap.xml")); |
| 192 | + return factoryBean.getObject(); |
| 193 | + } |
| 194 | + |
| 195 | + @Bean |
| 196 | + public MapperFactoryBean<AppAuthRequestMapper> appAuthRequestMapper(final SqlSessionFactory factory) { |
| 197 | + final MapperFactoryBean<AppAuthRequestMapper> bean = new MapperFactoryBean<>(AppAuthRequestMapper.class); |
| 198 | + bean.setSqlSessionFactory(factory); |
| 199 | + return bean; |
| 200 | + } |
| 201 | + |
| 202 | + @Bean |
| 203 | + public MapperFactoryBean<AppAuthMapper> appAuthMapper(final SqlSessionFactory factory) { |
| 204 | + final MapperFactoryBean<AppAuthMapper> bean = new MapperFactoryBean<>(AppAuthMapper.class); |
| 205 | + bean.setSqlSessionFactory(factory); |
| 206 | + return bean; |
| 207 | + } |
| 208 | + |
| 209 | + @Bean |
| 210 | + public MapperFactoryBean<AuthParamMapper> authParamMapper(final SqlSessionFactory factory) { |
| 211 | + final MapperFactoryBean<AuthParamMapper> bean = new MapperFactoryBean<>(AuthParamMapper.class); |
| 212 | + bean.setSqlSessionFactory(factory); |
| 213 | + return bean; |
| 214 | + } |
| 215 | + |
| 216 | + @Bean |
| 217 | + public MapperFactoryBean<AuthPathMapper> authPathMapper(final SqlSessionFactory factory) { |
| 218 | + final MapperFactoryBean<AuthPathMapper> bean = new MapperFactoryBean<>(AuthPathMapper.class); |
| 219 | + bean.setSqlSessionFactory(factory); |
| 220 | + return bean; |
| 221 | + } |
| 222 | + |
| 223 | + @Bean |
| 224 | + public AppAuthTransactionService appAuthTransactionService(final AppAuthRequestMapper requestMapper, |
| 225 | + final AppAuthMapper appAuthMapper, |
| 226 | + final AuthParamMapper authParamMapper, |
| 227 | + final AuthPathMapper authPathMapper) { |
| 228 | + // recorder passed directly to the constructor (container injection of ApplicationEventPublisher |
| 229 | + // resolves to the ApplicationContext, whose multicaster has no listeners in this bare context) |
| 230 | + final ApplicationEventPublisher recorder = PUBLISHED_EVENTS::add; |
| 231 | + return new AppAuthTransactionService(requestMapper, appAuthMapper, authParamMapper, |
| 232 | + authPathMapper, recorder); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments