Skip to content

Commit

Permalink
480 append test to module soul-sync-data-http (apache#495)
Browse files Browse the repository at this point in the history
* 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
0x12FD16B authored Dec 4, 2020
1 parent 0f10a5b commit e7bf8a8
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.stopBubbling=true
lombok.addLombokGeneratedAnnotation=true
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@
<exclude>**/spring.factories</exclude>
<exclude>**/spring.provides</exclude>
<exclude>/script/**</exclude>
<exclude>**/lombok.config</exclude>
<exclude>**/*.json</exclude>
<!-- IDE files -->
<exclude>**/*.iml</exclude>
<exclude>**/.idea/**</exclude>
Expand Down
11 changes: 11 additions & 0 deletions soul-sync-data-center/soul-sync-data-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,16 @@
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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;
}
}
}
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);
}

}
Loading

0 comments on commit e7bf8a8

Please sign in to comment.