| /* |
| * 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.apache.sling.feature.extension.apiregions; |
| |
| import org.apache.sling.feature.ArtifactId; |
| import org.apache.sling.feature.Extension; |
| import org.apache.sling.feature.ExtensionType; |
| import org.apache.sling.feature.Feature; |
| import org.junit.Test; |
| |
| import java.io.StringReader; |
| |
| import javax.json.Json; |
| import javax.json.JsonArray; |
| import javax.json.JsonReader; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assert.assertTrue; |
| |
| public class APIRegionMergeHandlerTest { |
| @Test |
| public void testCanMerge() { |
| APIRegionMergeHandler armh = new APIRegionMergeHandler(); |
| |
| Extension ex = new Extension(ExtensionType.JSON, "api-regions", false); |
| assertTrue(armh.canMerge(ex)); |
| assertFalse(armh.canMerge(new Extension(ExtensionType.JSON, "foo", false))); |
| } |
| |
| @Test |
| public void testAPIRegionMerging() { |
| APIRegionMergeHandler armh = new APIRegionMergeHandler(); |
| |
| Feature tf = new Feature(ArtifactId.fromMvnId("x:t:1")); |
| Feature sf = new Feature(ArtifactId.fromMvnId("y:s:2")); |
| |
| Extension tgEx = new Extension(ExtensionType.JSON, "api-regions", false); |
| tgEx.setJSON("[{\"name\":\"global\"," |
| + "\"exports\": [\"a.b.c\",\"d.e.f\"]}," |
| + "{\"name\":\"internal\"," |
| + "\"exports\":[\"xyz\"]," |
| + "\"some-key\":\"some-val\"}]"); |
| |
| Extension srEx = new Extension(ExtensionType.JSON, "api-regions", false); |
| srEx.setJSON("[{\"name\":\"global\"," |
| + "\"exports\": [\"test\"]}," |
| + "{\"name\":\"something\"," |
| + "\"exports\": [\"a.ha\"]," |
| + "\"my-key\": \"my-val\"}]"); |
| |
| armh.merge(null, tf, sf, tgEx, srEx); |
| |
| String expectedJSON = "[{\"name\":\"global\"," |
| + "\"exports\": [\"a.b.c\",\"d.e.f\", \"test\"]}," |
| + "{\"name\":\"internal\"," |
| + "\"exports\":[\"xyz\"]," |
| + "\"some-key\":\"some-val\"}," |
| + "{\"name\":\"something\"," |
| + "\"exports\": [\"a.ha\"]," |
| + "\"my-key\": \"my-val\"}]"; |
| JsonReader er = Json.createReader(new StringReader(expectedJSON)); |
| JsonReader ar = Json.createReader(new StringReader(tgEx.getJSON())); |
| JsonArray ea = er.readArray(); |
| JsonArray aa = ar.readArray(); |
| |
| assertEquals(ea, aa); |
| } |
| |
| |
| @Test |
| public void testRegionExportsInheritance() throws Exception { |
| APIRegionMergeHandler armh = new APIRegionMergeHandler(); |
| |
| Feature tf = new Feature(ArtifactId.fromMvnId("x:t:1")); |
| Feature sf = new Feature(ArtifactId.fromMvnId("y:s:2")); |
| |
| Extension srEx = new Extension(ExtensionType.JSON, "api-regions", false); |
| srEx.setJSON("[{\"name\":\"global\"," |
| + "\"exports\": [\"a.b.c\",\"d.e.f\"]}," |
| + "{\"name\":\"deprecated\"," |
| + "\"exports\":[\"klm\",\"#ignored\",\"qrs\"]}," |
| + "{\"name\":\"internal\"," |
| + "\"exports\":[\"xyz\"]}," |
| + "{\"name\":\"forbidden\"," |
| + "\"exports\":[\"abc\",\"klm\"]}]"); |
| |
| armh.merge(null, tf, sf, null, srEx); |
| |
| Extension tgEx = tf.getExtensions().iterator().next(); |
| |
| String expectedJSON = "[{\"name\":\"global\",\"exports\":[\"a.b.c\",\"d.e.f\"]}," |
| + "{\"name\":\"deprecated\",\"exports\":[\"klm\",\"qrs\"]}," |
| + "{\"name\":\"internal\",\"exports\":[\"xyz\"]}," |
| + "{\"name\":\"forbidden\",\"exports\":[\"abc\",\"klm\"]}]"; |
| JsonReader er = Json.createReader(new StringReader(expectedJSON)); |
| JsonReader ar = Json.createReader(new StringReader(tgEx.getJSON())); |
| JsonArray ea = er.readArray(); |
| JsonArray aa = ar.readArray(); |
| |
| assertEquals(ea, aa); |
| } |
| } |