Skip to content

Commit

Permalink
feat(core): map Info recursively
Browse files Browse the repository at this point in the history
- extract mapper to AsyncApiInfoMapper
- fix mapping of termsOfService
- update kafka example
  • Loading branch information
timonback committed Nov 22, 2024
1 parent b882612 commit fd97edd
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package io.github.springwolf.core.asyncapi.grouping;

import io.github.springwolf.asyncapi.v3.model.AsyncAPI;
import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.core.configuration.docket.AsyncApiGroup;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,7 +14,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.github.springwolf.core.configuration.docket.DefaultAsyncApiDocketService.mapInfo;
import static io.github.springwolf.core.configuration.docket.AsyncApiInfoMapper.mapInfo;
import static io.github.springwolf.core.configuration.docket.AsyncApiInfoMapper.mergeInfo;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -27,7 +27,7 @@ public Map<String, AsyncAPI> group(AsyncAPI asyncAPI) {
return getAsyncApiGroups()
.map(group -> {
AsyncAPI groupedApi = groupingService.groupAPI(asyncAPI, group);
groupedApi.setInfo(merge(groupedApi.getInfo(), group.getGroupInfo()));
groupedApi.setInfo(mergeInfo(groupedApi.getInfo(), group.getGroupInfo()));
return Map.entry(group.getGroupName(), groupedApi);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Expand All @@ -50,12 +50,12 @@ private static AsyncApiGroup toGroupConfigAndValidate(SpringwolfConfigProperties
}

int allItemCount = group.getActionToMatch().size()
+ group.getChannelNameToMatch().size()
+ group.getMessageNameToMatch().size();
+ group.getChannelNameToMatch().size()
+ group.getMessageNameToMatch().size();
if (allItemCount != 0
&& group.getActionToMatch().size() != allItemCount
&& channelNameToMatch.size() != allItemCount
&& messageNameToMatch.size() != allItemCount) {
&& group.getActionToMatch().size() != allItemCount
&& channelNameToMatch.size() != allItemCount
&& messageNameToMatch.size() != allItemCount) {
throw new IllegalArgumentException(
"AsyncApiGroup %s must specify at most one filter criteria".formatted(groupName));
}
Expand All @@ -70,19 +70,4 @@ private static AsyncApiGroup toGroupConfigAndValidate(SpringwolfConfigProperties
log.debug("Loaded AsyncApiGroup from configuration: {}", asyncApiGroup);
return asyncApiGroup;
}

public static Info merge(Info original, Info updates) {
return Info.builder()
.title(updates.getTitle() != null ? updates.getTitle() : original.getTitle())
.version(updates.getVersion() != null ? updates.getVersion() : original.getVersion())
.description(updates.getDescription() != null ? updates.getDescription() : original.getDescription())
.termsOfService(updates.getTermsOfService() != null ? updates.getTermsOfService() : original.getTermsOfService())
.contact(updates.getContact() != null ? updates.getContact() : original.getContact())
.license(updates.getLicense() != null ? updates.getLicense() : original.getLicense())
.tags(updates.getTags() != null ? updates.getTags() : original.getTags())
.externalDocs(updates.getExternalDocs() != null ? updates.getExternalDocs() : original.getExternalDocs())
// TODO fixme
//.extensionFields(updates.getExtensionFields() != null ? updates.getExtensionFields() : original.getExtensionFields())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.configuration.docket;

import io.github.springwolf.asyncapi.v3.model.info.Contact;
import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.asyncapi.v3.model.info.License;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;

import java.util.HashMap;
import java.util.Map;

public class AsyncApiInfoMapper {
public static Info mapInfo(SpringwolfConfigProperties.ConfigDocket.Info configDocketInfo) {
Info asyncapiInfo = Info.builder()
.version(configDocketInfo.getVersion())
.title(configDocketInfo.getTitle())
.description(configDocketInfo.getDescription())
.termsOfService(configDocketInfo.getTermsOfService())
.contact(configDocketInfo.getContact())
.license(configDocketInfo.getLicense())
.build();

// copy extension fields
if (configDocketInfo.getExtensionFields() != null) {
Map<String, Object> extFieldsMap = Map.copyOf(configDocketInfo.getExtensionFields());
asyncapiInfo.setExtensionFields(extFieldsMap);
}
return asyncapiInfo;
}

public static Info mergeInfo(Info original, Info updates) {
Info info = Info.builder()
.title(updates.getTitle() != null ? updates.getTitle() : original.getTitle())
.version(updates.getVersion() != null ? updates.getVersion() : original.getVersion())
.description(updates.getDescription() != null ? updates.getDescription() : original.getDescription())
.termsOfService(
updates.getTermsOfService() != null
? updates.getTermsOfService()
: original.getTermsOfService())
.contact(mergeContact(original.getContact(), updates.getContact()))
.license(mergeLicense(original.getLicense(), updates.getLicense()))
.tags(updates.getTags() != null ? updates.getTags() : original.getTags())
.build();

// copy extension fields
Map<String, Object> extFieldsMap = new HashMap<>();
if (original.getExtensionFields() != null) {
extFieldsMap.putAll(original.getExtensionFields());
}
if (updates.getExtensionFields() != null) {
extFieldsMap.putAll(updates.getExtensionFields());
}
if (!extFieldsMap.isEmpty()) {
info.setExtensionFields(extFieldsMap);
}

return info;
}

private static Contact mergeContact(Contact original, Contact updates) {
if (updates == null) {
return original;
}

return Contact.builder()
.name(updates.getName() != null ? updates.getName() : original.getName())
.url(updates.getUrl() != null ? updates.getUrl() : original.getUrl())
.email(updates.getEmail() != null ? updates.getEmail() : original.getEmail())
.build();
}

private static License mergeLicense(License original, License updates) {
if (updates == null) {
return original;
}

return License.builder()
.name(updates.getName() != null ? updates.getName() : original.getName())
.url(updates.getUrl() != null ? updates.getUrl() : original.getUrl())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.Map;

import static io.github.springwolf.core.configuration.docket.AsyncApiInfoMapper.mapInfo;
import static io.github.springwolf.core.configuration.properties.SpringwolfConfigConstants.SPRINGWOLF_CONFIG_PREFIX;

@Slf4j
Expand Down Expand Up @@ -63,26 +64,7 @@ private static Info buildInfo(@Nullable SpringwolfConfigProperties.ConfigDocket.
+ " is not set.");
}

Info asyncapiInfo = mapInfo(configDocketInfo);

return asyncapiInfo;
}

public static Info mapInfo(SpringwolfConfigProperties.ConfigDocket.Info configDocketInfo) {
Info asyncapiInfo = Info.builder()
.version(configDocketInfo.getVersion())
.title(configDocketInfo.getTitle())
.description(configDocketInfo.getDescription())
.contact(configDocketInfo.getContact())
.license(configDocketInfo.getLicense())
.build();

// copy extension fields from configDocketInfo to asyncapiInfo.
if (configDocketInfo.getExtensionFields() != null) {
Map<String, Object> extFieldsMap = Map.copyOf(configDocketInfo.getExtensionFields());
asyncapiInfo.setExtensionFields(extFieldsMap);
}
return asyncapiInfo;
return mapInfo(configDocketInfo);
}

private static Map<String, Server> buildServers(Map<String, Server> servers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public static class ConfigDocket {

@Getter
@Setter
@EqualsAndHashCode
@ToString
public static class Info {

/**
Expand Down Expand Up @@ -212,6 +214,13 @@ public static class Group {
*/
private String group = "";

/**
* Allows to override the info object with group specific information .
*
* @see Info
*/
private Info info = new Info();

/**
* The action to match for the group
*/
Expand All @@ -226,13 +235,6 @@ public static class Group {
* The message names to match
*/
private List<String> messageNameToMatch = Collections.emptyList();

/**
* Allows to override the info object with group specific information .
*
* @see Info
*/
private Info info;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AsyncApiGroupServiceTest {
@BeforeEach
void setUp() {
when(springwolfConfigProperties.getDocket()).thenReturn(configDocket);
when(groupedAsyncApi.getInfo()).thenReturn(new Info());
when(groupingService.groupAPI(eq(asyncAPI), any())).thenReturn(groupedAsyncApi);
}

Expand Down Expand Up @@ -119,13 +120,12 @@ void shouldGroupByAction() {
asyncApiGroupService.group(asyncAPI);

// then
verify(groupingService)
.groupAPI(
any(),
eq(AsyncApiGroup.builder()
.groupName("group1")
.operationActionsToKeep(actions)
.build()));
ArgumentCaptor<AsyncApiGroup> captor = ArgumentCaptor.forClass(AsyncApiGroup.class);
verify(groupingService).groupAPI(any(), captor.capture());

AsyncApiGroup capturedGroup = captor.getValue();
List<OperationAction> actualPattern = capturedGroup.getOperationActionsToKeep();
assertThat(actualPattern).isEqualTo(actions);
}

@Test
Expand All @@ -146,8 +146,6 @@ void shouldGroupByChannels() {
verify(groupingService).groupAPI(any(), captor.capture());

AsyncApiGroup capturedGroup = captor.getValue();
assertThat(capturedGroup.getGroupName()).isEqualTo("group1");

Pattern actualPattern = capturedGroup.getChannelNamesToKeep().get(0);
assertThat(actualPattern.pattern()).isEqualTo(channels.get(0));
}
Expand All @@ -170,36 +168,35 @@ void shouldGroupByMessage() {
verify(groupingService).groupAPI(any(), captor.capture());

AsyncApiGroup capturedGroup = captor.getValue();
assertThat(capturedGroup.getGroupName()).isEqualTo("group1");

Pattern actualPattern = capturedGroup.getMessageNamesToKeep().get(0);
assertThat(actualPattern.pattern()).isEqualTo(messages.get(0));
}

@Test
void shouldCustomizeInfoObject() {
// given
SpringwolfConfigProperties.ConfigDocket.Group group = new SpringwolfConfigProperties.ConfigDocket.Group();
group.setGroup("group1");

SpringwolfConfigProperties.ConfigDocket.Info groupInfo = new SpringwolfConfigProperties.ConfigDocket.Info();
groupInfo.setVersion("1.2.3");
groupInfo.setDescription("description-override");

SpringwolfConfigProperties.ConfigDocket.Group group = new SpringwolfConfigProperties.ConfigDocket.Group();
group.setGroup("group1");
group.setInfo(groupInfo);

when(configDocket.getGroupConfigs()).thenReturn(List.of(group));

Info originalInfo = new Info();
originalInfo.setDescription("description-original");
originalInfo.setTitle("title-original");
when(groupedAsyncApi.getInfo()).thenReturn(originalInfo);

// when
Map<String, AsyncAPI> result = asyncApiGroupService.group(asyncAPI);
asyncApiGroupService.group(asyncAPI);

// then
Info expectedInfo = new Info();
expectedInfo.setVersion("1.2.3");
expectedInfo.setDescription("description-override");
verify(groupedAsyncApi).setInfo(expectedInfo);
ArgumentCaptor<Info> expectedInfoCaptor = ArgumentCaptor.forClass(Info.class);
verify(groupedAsyncApi).setInfo(expectedInfoCaptor.capture());
assertThat(expectedInfoCaptor.getValue().getVersion()).isEqualTo("1.2.3");
assertThat(expectedInfoCaptor.getValue().getDescription()).isEqualTo("description-override");
assertThat(expectedInfoCaptor.getValue().getTitle()).isEqualTo("title-original");
}
}
Loading

0 comments on commit fd97edd

Please sign in to comment.