|
| 1 | +package com.flagsmith.flagengine.models; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | +import com.flagsmith.MapperFactory; |
| 7 | +import com.flagsmith.models.features.FeatureStateModel; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +public class FeatureSegmentModelTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void deserializesFeatureSegmentAsIntegerForeignKey() throws JsonProcessingException { |
| 14 | + String json = "{" |
| 15 | + + "\"feature\": {\"id\": 1, \"name\": \"my_feature\", \"type\": \"STANDARD\"}," |
| 16 | + + "\"enabled\": true," |
| 17 | + + "\"feature_state_value\": \"foo\"," |
| 18 | + + "\"feature_segment\": 321" |
| 19 | + + "}"; |
| 20 | + |
| 21 | + FeatureStateModel model = |
| 22 | + MapperFactory.getMapper().readValue(json, FeatureStateModel.class); |
| 23 | + |
| 24 | + assertThat(model.getFeatureSegment()).isNotNull(); |
| 25 | + assertThat(model.getFeatureSegment().getPriority()).isEqualTo(321); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void deserializesFeatureSegmentAsEngineObject() throws JsonProcessingException { |
| 30 | + String json = "{" |
| 31 | + + "\"feature\": {\"id\": 1, \"name\": \"my_feature\", \"type\": \"STANDARD\"}," |
| 32 | + + "\"enabled\": true," |
| 33 | + + "\"feature_state_value\": \"foo\"," |
| 34 | + + "\"feature_segment\": {\"priority\": 5}" |
| 35 | + + "}"; |
| 36 | + |
| 37 | + FeatureStateModel model = |
| 38 | + MapperFactory.getMapper().readValue(json, FeatureStateModel.class); |
| 39 | + |
| 40 | + assertThat(model.getFeatureSegment()).isNotNull(); |
| 41 | + assertThat(model.getFeatureSegment().getPriority()).isEqualTo(5); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void deserializesNullFeatureSegment() throws JsonProcessingException { |
| 46 | + String json = "{" |
| 47 | + + "\"feature\": {\"id\": 1, \"name\": \"my_feature\", \"type\": \"STANDARD\"}," |
| 48 | + + "\"enabled\": true," |
| 49 | + + "\"feature_state_value\": \"foo\"," |
| 50 | + + "\"feature_segment\": null" |
| 51 | + + "}"; |
| 52 | + |
| 53 | + FeatureStateModel model = |
| 54 | + MapperFactory.getMapper().readValue(json, FeatureStateModel.class); |
| 55 | + |
| 56 | + assertThat(model.getFeatureSegment()).isNull(); |
| 57 | + } |
| 58 | +} |
0 commit comments