Skip to content

Commit

Permalink
chore: increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
toedter committed Mar 12, 2022
1 parent e842cfe commit e18109b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Links;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -162,8 +163,16 @@ protected EntityModel<?> convertToRepresentationModel(List<Object> resources, Js
try {
method.setAccessible(true);
// a setter is expected to return void
if (method.getReturnType() == void.class) {
method.invoke(content);
if (method.getReturnType() == void.class && meta instanceof Map) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
methodName = StringUtils.uncapitalize(methodName.substring(3));
}

Object metaValue = ((Map<?,?>) meta).get(methodName);
if (metaValue != null) {
method.invoke(content, metaValue);
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Cannot set JSON:API meta data for annotated method: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ public String getMetaProperty() {
}

@Test
void should_deserialize_jsonapimeta_method_annotation() throws Exception {
void should_deserialize_jsonapimeta_field_annotation() throws Exception {
JavaType javaType =
mapper.getTypeFactory().constructParametricType(EntityModel.class, MovieWithMetaAnnotation.class);
File file = new ClassPathResource("movieEntityModelWithMeta.json", getClass()).getFile();
Expand All @@ -1044,6 +1044,16 @@ void should_deserialize_jsonapimeta_method_annotation() throws Exception {
assertThat(entityModel.getContent().getMetaProperty()).isEqualTo("metaValue");
}

@Test
void should_deserialize_jsonapimeta_method_annotation() throws Exception {
JavaType javaType =
mapper.getTypeFactory().constructParametricType(EntityModel.class, MovieWithMethodMetaAnnotation.class);
File file = new ClassPathResource("movieEntityModelWithMeta.json", getClass()).getFile();
EntityModel<MovieWithMethodMetaAnnotation> entityModel = mapper.readValue(file, javaType);

assertThat(entityModel.getContent().getMetaProperty()).isEqualTo("metaValue");
}

private void compareWithFile(String json, String fileName) throws Exception {
File file = new ClassPathResource(fileName, getClass()).getFile();
ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed 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
*
* https://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 com.toedter.spring.hateoas.jsonapi;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("JsonApiLinksDeserializer Unit Test")
class JsonApiLinksDeserializerUnitTest extends JsonApiTestBase {

@Test
void should_return_null_content_deserializer() {
assertThat(new JsonApiLinksDeserializer().getContentDeserializer()).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed 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
*
* https://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 com.toedter.spring.hateoas.jsonapi.support;

import com.toedter.spring.hateoas.jsonapi.JsonApiMeta;
import com.toedter.spring.hateoas.jsonapi.JsonApiTypeForClass;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@NoArgsConstructor
@AllArgsConstructor
@With
@JsonApiTypeForClass("movies")
public class MovieWithMethodMetaAnnotation {
private Long id = 1L;
private String title = "Star Wars";
private String metaProperty = "xxx";

@JsonApiMeta
public String getMetaProperty() {
return metaProperty;
}

@JsonApiMeta
public void setMetaProperty(String metaProperty) {
this.metaProperty = metaProperty;
}

}

0 comments on commit e18109b

Please sign in to comment.