forked from apache/shenyu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
480 append test to module soul-sync-data-http (apache#495)
* add test case for SelectorDataRefresh * refactor SelectorDataRefreshTest * update SelectorDataRefersh comment * update SelectorDataRefreshTest comment * add test for RuleDateRefresh * add test for PluginDateRefresh * add test for MetaDataRefresh * add test for AppAuthDataRefresh * append global lombok configuration file to avoid jacoco compute lombok generate method code coverage * add mock http config data sync endpoint using Jetty * add test for HttpSyncDataService * add license info to new file * add lombok config file to apache RAT plugin exlucde rule * update apache rat plugin exclude rule * update test in module soul-sync-data-http according to checkstyle * update test in module soul-sync-data-http * update test in module soul-sync-data-http * update test in module soul-sync-data-http * refactor test HttpSyncDataServiceTest on using WireMock to replace Jetty Mock
- Loading branch information
Showing
11 changed files
with
819 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
config.stopBubbling=true | ||
lombok.addLombokGeneratedAnnotation=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...sync-data-http/src/test/java/org/dromara/soul/sync/data/http/HttpSyncDataServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dromara.soul.sync.data.http; | ||
|
||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import org.dromara.soul.common.dto.AppAuthData; | ||
import org.dromara.soul.common.dto.MetaData; | ||
import org.dromara.soul.common.dto.PluginData; | ||
import org.dromara.soul.sync.data.api.AuthDataSubscriber; | ||
import org.dromara.soul.sync.data.api.MetaDataSubscriber; | ||
import org.dromara.soul.sync.data.api.PluginDataSubscriber; | ||
import org.dromara.soul.sync.data.http.config.HttpConfig; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import wiremock.org.apache.http.HttpHeaders; | ||
import wiremock.org.apache.http.entity.ContentType; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; | ||
|
||
/** | ||
* Test cases for {@link HttpSyncDataService}. | ||
* | ||
* @author davidliu | ||
*/ | ||
public class HttpSyncDataServiceTest { | ||
|
||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort(), false); | ||
|
||
@Before | ||
public final void setUpWiremock() { | ||
wireMockRule.stubFor(get(urlPathEqualTo("/configs/fetch")) | ||
.willReturn(aResponse() | ||
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) | ||
.withBody(this.mockConfigsFetchResponseJson()) | ||
.withStatus(200)) | ||
); | ||
wireMockRule.stubFor(post(urlPathEqualTo("/configs/listener")) | ||
.willReturn(aResponse() | ||
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) | ||
.withBody(this.mockConfigsListenResponseJson()) | ||
.withStatus(200)) | ||
); | ||
} | ||
|
||
/** | ||
* this method covers {@link HttpSyncDataService} constructor and {@link HttpSyncDataService#close()} method. | ||
* | ||
* @throws Exception any exception | ||
*/ | ||
@Test | ||
public void test() throws Exception { | ||
try (HttpSyncDataService ignored = this.buildHttpSyncDataService()) { | ||
// sleep 5 seconds to ensure Http Long polling task run | ||
TimeUnit.SECONDS.sleep(5); | ||
} | ||
|
||
} | ||
|
||
private HttpSyncDataService buildHttpSyncDataService() { | ||
HttpConfig httpConfig = new HttpConfig(); | ||
httpConfig.setUrl(this.getMockServerUrl()); | ||
// set http connection timeout | ||
httpConfig.setConnectionTimeout(3); | ||
// set delay time | ||
httpConfig.setDelayTime(3); | ||
PluginDataSubscriber pluginDataSubscriber = new PluginDataSubscriber() { | ||
@Override | ||
public void onSubscribe(final PluginData pluginData) { | ||
|
||
} | ||
}; | ||
List<MetaDataSubscriber> metaDataSubscribers = Collections.singletonList(new MetaDataSubscriber() { | ||
@Override | ||
public void onSubscribe(final MetaData metaData) { | ||
|
||
} | ||
|
||
@Override | ||
public void unSubscribe(final MetaData metaData) { | ||
|
||
} | ||
}); | ||
List<AuthDataSubscriber> authDataSubscribers = Collections.singletonList(new AuthDataSubscriber() { | ||
@Override | ||
public void onSubscribe(final AppAuthData appAuthData) { | ||
|
||
} | ||
|
||
@Override | ||
public void unSubscribe(final AppAuthData appAuthData) { | ||
|
||
} | ||
}); | ||
return new HttpSyncDataService(httpConfig, pluginDataSubscriber, metaDataSubscribers, authDataSubscribers); | ||
} | ||
|
||
private String getMockServerUrl() { | ||
return "http://localhost:" + wireMockRule.port(); | ||
} | ||
|
||
// mock configs listen api response | ||
private String mockConfigsListenResponseJson() { | ||
return "{\"code\":200,\"message\":\"success\",\"data\":[\"PLUGIN\"]}"; | ||
} | ||
|
||
// mock configs fetch api response | ||
private String mockConfigsFetchResponseJson() { | ||
try (FileInputStream fis = new FileInputStream(this.getClass().getClassLoader().getResource("mock_configs_fetch_response.json").getPath()); | ||
InputStreamReader reader = new InputStreamReader(fis); | ||
BufferedReader bufferedReader = new BufferedReader(reader); | ||
) { | ||
StringBuilder builder = new StringBuilder(); | ||
bufferedReader.lines().forEach(builder::append); | ||
return builder.toString(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...ta-http/src/test/java/org/dromara/soul/sync/data/http/refresh/AppAuthDataRefreshTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dromara.soul.sync.data.http.refresh; | ||
|
||
import com.google.gson.JsonObject; | ||
import org.dromara.soul.common.dto.AppAuthData; | ||
import org.dromara.soul.common.dto.ConfigData; | ||
import org.dromara.soul.common.enums.ConfigGroupEnum; | ||
import org.dromara.soul.common.utils.GsonUtils; | ||
import org.dromara.soul.sync.data.api.AuthDataSubscriber; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Test cases for {@link AppAuthDataRefresh}. | ||
* | ||
* @author davidliu | ||
*/ | ||
public class AppAuthDataRefreshTest { | ||
|
||
private final AppAuthDataRefresh mockAppAuthDataRefresh = this.buildMockAppAuthDataRefresh(); | ||
|
||
/** | ||
* test case for {@link AppAuthDataRefresh#convert(JsonObject)}. | ||
*/ | ||
@Test | ||
public void testConvert() { | ||
JsonObject jsonObject = new JsonObject(); | ||
JsonObject expectJsonObject = new JsonObject(); | ||
jsonObject.add(ConfigGroupEnum.APP_AUTH.name(), expectJsonObject); | ||
Assert.assertEquals(expectJsonObject, mockAppAuthDataRefresh.convert(jsonObject)); | ||
} | ||
|
||
/** | ||
* test case for {@link AppAuthDataRefresh#fromJson(JsonObject)}. | ||
*/ | ||
@Test | ||
public void testFromJson() { | ||
ConfigData<AppAuthData> appAuthDataConfigData = new ConfigData<>(); | ||
AppAuthData appAuthData = new AppAuthData(); | ||
appAuthDataConfigData.setData(Collections.singletonList(appAuthData)); | ||
JsonObject jsonObject = GsonUtils.getGson().fromJson(GsonUtils.getGson().toJson(appAuthDataConfigData), JsonObject.class); | ||
Assert.assertEquals(appAuthDataConfigData, mockAppAuthDataRefresh.fromJson(jsonObject)); | ||
} | ||
|
||
/** | ||
* This case coverages the following method: | ||
* {@link AppAuthDataRefresh#cacheConfigData()} | ||
* {@link AppAuthDataRefresh#updateCacheIfNeed(ConfigData)}. | ||
* For {@link SelectorDataRefresh} inherits from {@link AbstractDataRefresh}, the {@link AbstractDataRefresh#GROUP_CACHE} was initialized when the class of | ||
* {@link AbstractDataRefresh} load, in two different test methods in this class, the the {@link AbstractDataRefresh#GROUP_CACHE} class only load once, so | ||
* the method which manipulate the {@link AbstractDataRefresh#GROUP_CACHE} invocation has aftereffects to the other methods. | ||
*/ | ||
@Test | ||
public void testUpdateCacheIfNeed() { | ||
final AppAuthDataRefresh appAuthDataRefresh = mockAppAuthDataRefresh; | ||
// update cache, then assert equals | ||
ConfigData<AppAuthData> expect = new ConfigData<>(); | ||
expect.setLastModifyTime(System.currentTimeMillis()); | ||
appAuthDataRefresh.updateCacheIfNeed(expect); | ||
Assert.assertEquals(expect, appAuthDataRefresh.cacheConfigData()); | ||
} | ||
|
||
/** | ||
* This case is only for {@link AppAuthDataRefresh} code coverage. | ||
*/ | ||
@Test | ||
public void testRefreshCoverage() { | ||
final AppAuthDataRefresh appAuthDataRefresh = mockAppAuthDataRefresh; | ||
AppAuthData appAuthData = new AppAuthData(); | ||
List<AppAuthData> appAuthDataList = new ArrayList<>(); | ||
appAuthDataRefresh.refresh(appAuthDataList); | ||
appAuthDataList.add(appAuthData); | ||
appAuthDataRefresh.refresh(appAuthDataList); | ||
|
||
} | ||
|
||
private AppAuthDataRefresh buildMockAppAuthDataRefresh() { | ||
List<AuthDataSubscriber> authDataSubscribers = new ArrayList<>(); | ||
authDataSubscribers.add(new AuthDataSubscriber() { | ||
@Override | ||
public void onSubscribe(final AppAuthData appAuthData) { | ||
|
||
} | ||
|
||
@Override | ||
public void unSubscribe(final AppAuthData appAuthData) { | ||
|
||
} | ||
}); | ||
return new AppAuthDataRefresh(authDataSubscribers); | ||
} | ||
|
||
} |
Oops, something went wrong.