|
16 | 16 |
|
17 | 17 | package com.cedarpolicy; |
18 | 18 |
|
19 | | -import com.cedarpolicy.model.schema.Schema; |
20 | | -import com.cedarpolicy.model.schema.Schema.JsonOrCedar; |
21 | | - |
22 | | -import org.junit.jupiter.api.Test; |
| 19 | +import java.util.Optional; |
23 | 20 |
|
24 | 21 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
25 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 25 | +import org.junit.jupiter.api.DisplayName; |
| 26 | +import org.junit.jupiter.api.Nested; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import com.cedarpolicy.model.exception.InternalException; |
| 30 | +import com.cedarpolicy.model.schema.Schema; |
| 31 | +import com.cedarpolicy.model.schema.Schema.JsonOrCedar; |
| 32 | +import com.fasterxml.jackson.databind.JsonNode; |
| 33 | +import com.fasterxml.jackson.databind.ObjectMapper; |
26 | 34 |
|
27 | 35 | public class SchemaTests { |
28 | 36 | @Test |
@@ -107,4 +115,124 @@ public void parseCedarSchema() { |
107 | 115 | Schema.parse(JsonOrCedar.Cedar, "namspace Foo::Bar;"); |
108 | 116 | }); |
109 | 117 | } |
| 118 | + |
| 119 | + @Nested |
| 120 | + @DisplayName("toCedarFormat Tests") |
| 121 | + class ToCedarFormatTests { |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("Should return the same Cedar schema text") |
| 125 | + void testFromCedar() throws InternalException { |
| 126 | + String cedarSchema = "entity User;"; |
| 127 | + Schema cedarSchemaObj = new Schema(cedarSchema); |
| 128 | + String result = cedarSchemaObj.toCedarFormat(); |
| 129 | + assertNotNull(result, "Result should not be null"); |
| 130 | + assertEquals(cedarSchema, result, "Should return the original Cedar schema"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("Should convert JSON schema to Cedar format") |
| 135 | + void testFromJson() throws InternalException { |
| 136 | + String jsonSchema = """ |
| 137 | + { |
| 138 | + "": { |
| 139 | + "entityTypes": { |
| 140 | + "User": {} |
| 141 | + }, |
| 142 | + "actions": {} |
| 143 | + } |
| 144 | + } |
| 145 | + """; |
| 146 | + Schema jsonSchemaObj = Schema.parse(JsonOrCedar.Json, jsonSchema); |
| 147 | + String result = jsonSchemaObj.toCedarFormat(); |
| 148 | + |
| 149 | + assertNotNull(result, "Result should not be null"); |
| 150 | + String expectedCedar = "entity User;"; |
| 151 | + assertEquals(expectedCedar, result.trim(), "Converted Cedar should match expected format"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @DisplayName("Should throw IllegalStateException for empty schema") |
| 156 | + void testEmptySchema() { |
| 157 | + Schema emptySchema = new Schema(JsonOrCedar.Cedar, Optional.empty(), Optional.empty()); |
| 158 | + Exception exception = assertThrows(IllegalStateException.class, emptySchema::toCedarFormat); |
| 159 | + assertEquals("No schema found", exception.getMessage()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + @DisplayName("Should throw exception for malformed JSON schema") |
| 164 | + void testMalformedSchema() { |
| 165 | + String malformedJson = """ |
| 166 | + { |
| 167 | + "": { |
| 168 | + "entityMalformedTypes": { |
| 169 | + "User": {} |
| 170 | + }, |
| 171 | + "actions": {} |
| 172 | + } |
| 173 | + } |
| 174 | + """; |
| 175 | + Schema malformedSchema = new Schema(JsonOrCedar.Json, Optional.of(malformedJson), Optional.empty()); |
| 176 | + assertNotNull(malformedSchema.schemaJson); |
| 177 | + assertThrows(InternalException.class, malformedSchema::toCedarFormat); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Nested |
| 182 | + @DisplayName("toJsonFormat Tests") |
| 183 | + class ToJsonFormatTests { |
| 184 | + |
| 185 | + @Test |
| 186 | + @DisplayName("Should convert Cedar schema to JSON format") |
| 187 | + void testFromCedar() throws Exception { |
| 188 | + String cedarSchema = "entity User;"; |
| 189 | + Schema cedarSchemaObj = new Schema(cedarSchema); |
| 190 | + JsonNode result = cedarSchemaObj.toJsonFormat(); |
| 191 | + |
| 192 | + String expectedJson = "{\"\":{\"entityTypes\":{\"User\":{}},\"actions\":{}}}"; |
| 193 | + JsonNode expectedNode = new ObjectMapper().readTree(expectedJson); |
| 194 | + |
| 195 | + assertNotNull(result, "Result should not be null"); |
| 196 | + assertEquals(expectedNode, result, "JSON should match expected structure"); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + @DisplayName("Should return the same JSON schema object") |
| 201 | + void testFromJson() throws Exception { |
| 202 | + String jsonSchema = """ |
| 203 | + { |
| 204 | + "": { |
| 205 | + "entityTypes": { |
| 206 | + "User": {} |
| 207 | + }, |
| 208 | + "actions": {} |
| 209 | + } |
| 210 | + } |
| 211 | + """; |
| 212 | + Schema jsonSchemaObj = Schema.parse(JsonOrCedar.Json, jsonSchema); |
| 213 | + JsonNode result = jsonSchemaObj.toJsonFormat(); |
| 214 | + |
| 215 | + ObjectMapper mapper = new ObjectMapper(); |
| 216 | + JsonNode expectedNode = mapper.readTree(jsonSchema); |
| 217 | + |
| 218 | + assertNotNull(result, "Result should not be null"); |
| 219 | + assertEquals(expectedNode, result, "JSON should match the original schema"); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + @DisplayName("Should throw IllegalStateException for empty schema") |
| 224 | + void testEmptySchema() { |
| 225 | + Schema emptySchema = new Schema(JsonOrCedar.Cedar, Optional.empty(), Optional.empty()); |
| 226 | + Exception exception = assertThrows(IllegalStateException.class, emptySchema::toJsonFormat); |
| 227 | + assertEquals("No schema found", exception.getMessage()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + @DisplayName("Should throw exception for malformed Cedar schema") |
| 232 | + void testMalformedSchema() { |
| 233 | + String malformedCedar = "entty User"; |
| 234 | + Schema malformedSchema = new Schema(JsonOrCedar.Cedar, Optional.empty(), Optional.of(malformedCedar)); |
| 235 | + assertThrows(InternalException.class, malformedSchema::toJsonFormat); |
| 236 | + } |
| 237 | + } |
110 | 238 | } |
0 commit comments