| /* |
| * 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.io.json; |
| |
| import org.apache.sling.feature.Feature; |
| import org.junit.Assert; |
| import org.junit.Test; |
| |
| import javax.json.Json; |
| import javax.json.JsonArray; |
| import javax.json.JsonObject; |
| import java.io.*; |
| import java.util.Arrays; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNotNull; |
| |
| public class FeatureJSONWriterTest { |
| |
| @Test public void testWrite() throws Exception { |
| final Feature f = U.readFeature("test"); |
| final Feature rf; |
| try ( final StringWriter writer = new StringWriter() ) { |
| FeatureJSONWriter.write(writer, f); |
| try ( final StringReader reader = new StringReader(writer.toString()) ) { |
| rf = FeatureJSONReader.read(reader, null); |
| } |
| } |
| assertEquals(f.getId(), rf.getId()); |
| assertEquals("org.apache.sling:test-feature:1.1", rf.getId().toMvnId()); |
| assertEquals("The feature description", rf.getDescription()); |
| |
| assertEquals(Arrays.asList("org.osgi.service.http.runtime.HttpServiceRuntime"), |
| U.findCapability(rf.getCapabilities(), "osgi.service").getAttributes().get("objectClass")); |
| } |
| |
| @Test public void testWrite2() throws Exception { |
| final Feature f = U.readFeature("test2"); |
| |
| final Feature rf; |
| try ( final StringWriter writer = new StringWriter() ) { |
| FeatureJSONWriter.write(writer, f); |
| try ( final StringReader reader = new StringReader(writer.toString()) ) { |
| rf = FeatureJSONReader.read(reader, null); |
| } |
| } |
| |
| assertEquals(f.getVariables(), rf.getVariables()); |
| } |
| |
| @Test public void testExtensionsWriteRead() throws Exception { |
| final Feature f = U.readFeature("artifacts-extension"); |
| final Feature rf; |
| try ( final StringWriter writer = new StringWriter() ) { |
| FeatureJSONWriter.write(writer, f); |
| try ( final StringReader reader = new StringReader(writer.toString()) ) { |
| rf = FeatureJSONReader.read(reader, null); |
| } |
| } |
| |
| ArtifactsExtensions.testReadArtifactsExtensions(rf); |
| } |
| |
| @Test public void testIncludeWriteRead() throws Exception { |
| final Feature f = U.readFeature("test"); |
| assertNotNull(f.getInclude()); |
| |
| final Feature rf; |
| try ( final StringWriter writer = new StringWriter() ) { |
| FeatureJSONWriter.write(writer, f); |
| try ( final StringReader reader = new StringReader(writer.toString()) ) { |
| rf = FeatureJSONReader.read(reader, null); |
| } |
| } |
| assertEquals(f.getInclude().getId(), rf.getInclude().getId()); |
| } |
| |
| @Test public void testRepoInitWrite() throws Exception { |
| final Feature f = U.readFeature("repoinit2"); |
| try ( final StringWriter writer = new StringWriter() ) { |
| FeatureJSONWriter.write(writer, f); |
| final JsonObject refJson = Json.createReader( |
| new InputStreamReader(U.class.getResourceAsStream("/features/repoinit2.json")) |
| ).readObject(); |
| final JsonObject resultJson = Json.createReader(new StringReader(writer.toString())).readObject(); |
| |
| JsonArray refJsonArray = refJson.getJsonArray("repoinit:TEXT|false"); |
| JsonArray resultJsonArray = resultJson.getJsonArray("repoinit:TEXT|false"); |
| Assert.assertEquals(refJsonArray, resultJsonArray); |
| } |
| } |
| } |