/*
 * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch)
 *
 * 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
 *
 *     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 guru.nidi.graphviz.engine;

import guru.nidi.graphviz.attribute.Image;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

import static guru.nidi.graphviz.model.Factory.graph;
import static guru.nidi.graphviz.model.Factory.node;

class ImageTest {
    @BeforeAll
    static void init() {
        Graphviz.useEngine(new GraphvizV8Engine());
    }

    @Test
    void relative() throws IOException {
        Graphviz g = Graphviz.fromGraph(graph()
                .with(node(" ").with(Image.of("example/graphviz.png"))));
        g.render(Format.PNG).toFile(new File("target/img-relative.png"));
    }

    @Test
    void relativeWithBase() throws IOException {
        Graphviz g = Graphviz.fromGraph(graph()
                .with(node(" ").with(Image.of("graphviz.png"))));
        g.basedir(new File("example")).render(Format.PNG).toFile(new File("target/img-base-relative.png"));
    }

    @Test
    void absolute() throws IOException {
        Graphviz g = Graphviz.fromGraph(graph()
                .with(node(" ").with(Image.of(new File("example/graphviz.png").getAbsolutePath()))));
        g.render(Format.PNG).toFile(new File("target/img-absolute.png"));
    }

    @Test
    void http() throws IOException {
        Graphviz g = Graphviz.fromGraph(graph()
                .with(node(" ").with(Image.of("https://raw.githubusercontent.com/nidi3/graphviz-java/master/graphviz-java/example/ex7.png"))));
        g.render(Format.PNG).toFile(new File("target/img-http.png"));
    }
}

@@ -818,16 +818,68 @@
 ption;%0A%0A
+import static guru.nidi.graphviz.engine.Format.PNG;%0A
 import s
@@ -976,16 +976,75 @@
 ry.node;
+%0Aimport static org.junit.jupiter.api.Assertions.assertTrue;
 %0A%0Aclass 
@@ -1205,32 +1205,38 @@
 ption %7B%0A        
+final 
 Graphviz g = Gra
@@ -1258,33 +1258,16 @@
 (graph()
-%0A                
 .with(no
@@ -1328,36 +1328,25 @@
     
-g.render(Format.PNG).toFile(
+final File out = 
 new 
@@ -1368,32 +1368,108 @@
 g-relative.png%22)
+;%0A        g.render(PNG).toFile(out);%0A        assertTrue(out.length() %3E 15000
 );%0A    %7D%0A%0A    @T
@@ -1521,32 +1521,38 @@
 ption %7B%0A        
+final 
 Graphviz g = Gra
@@ -1574,33 +1574,16 @@
 (graph()
-%0A                
 .with(no
@@ -1616,32 +1616,99 @@
 phviz.png%22))));%0A
+        final File out = new File(%22target/img-base-relative.png%22);%0A
         g.basedi
@@ -1729,39 +1729,32 @@
 ample%22)).render(
-Format.
 PNG).toFile(new 
@@ -1753,48 +1753,53 @@
 ile(
-new File(%22target/img-base-relative.png%22)
+out);%0A        assertTrue(out.length() %3E 15000
 );%0A 
@@ -1855,32 +1855,38 @@
 ption %7B%0A        
+final 
 Graphviz g = Gra
@@ -1908,33 +1908,16 @@
 (graph()
-%0A                
 .with(no
@@ -1938,16 +1938,33 @@
 mage.of(
+%0A                
 new File
@@ -2023,36 +2023,25 @@
     
-g.render(Format.PNG).toFile(
+final File out = 
 new 
@@ -2067,24 +2067,100 @@
 solute.png%22)
+;%0A        g.render(PNG).toFile(out);%0A        assertTrue(out.length() %3E 15000
 );%0A    %7D%0A%0A  
@@ -2204,32 +2204,38 @@
 ption %7B%0A        
+final 
 Graphviz g = Gra
@@ -2257,33 +2257,16 @@
 (graph()
-%0A                
 .with(no
@@ -2287,16 +2287,33 @@
 mage.of(
+%0A                
 %22https:/
@@ -2414,36 +2414,25 @@
     
-g.render(Format.PNG).toFile(
+final File out = 
 new 
@@ -2458,16 +2458,92 @@
 tp.png%22)
+;%0A        g.render(PNG).toFile(out);%0A        assertTrue(out.length() %3E 15000
 );%0A    %7D

improve ImageTest