package org.javacs;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.junit.Test;

public class CodeLensTest {

    private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();

    private List<? extends CodeLens> lenses(String file) {
        var uri = FindResource.uri(file);
        var params = new CodeLensParams(new TextDocumentIdentifier(uri.toString()));
        try {
            return server.getTextDocumentService().codeLens(params).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private List<String> commands(List<? extends CodeLens> lenses) {
        var commands = new ArrayList<String>();
        for (var lens : lenses) {
            var command = new StringJoiner(", ");
            for (var arg : lens.getCommand().getArguments()) {
                if (arg instanceof Optional) arg = ((Optional) arg).orElse(null);
                command.add(Objects.toString(arg));
            }
            commands.add(command.toString());
        }
        return commands;
    }

    @Test
    public void codeLens() {
        var file = "/org/javacs/example/HasTest.java";
        var lenses = lenses(file);
        assertThat(lenses, not(empty()));

        var commands = commands(lenses);
        assertThat(commands, hasItem(containsString("HasTest, null")));
        assertThat(commands, hasItem(containsString("HasTest, testMethod")));
        assertThat(commands, hasItem(containsString("HasTest, otherTestMethod")));
    }
}

@@ -88,16 +88,45 @@
 ert.*;%0A%0A
+import com.google.gson.Gson;%0A
 import j
@@ -141,24 +141,24 @@
 .ArrayList;%0A
-
 import java.
@@ -198,35 +198,8 @@
 ts;%0A
-import java.util.Optional;%0A
 impo
@@ -764,22 +764,28 @@
         
-return
+var lenses =
  server.
@@ -829,24 +829,542 @@
 ams).get();%0A
+            var resolved = new ArrayList%3CCodeLens%3E();%0A            for (var lens : lenses) %7B%0A                if (lens.getCommand() == null) %7B%0A                    var gson = new Gson();%0A                    var data = lens.getData();%0A                    var dataJson = gson.toJsonTree(data);%0A                    lens.setData(dataJson);%0A                    lens = server.getTextDocumentService().resolveCodeLens(lens).get();%0A                %7D%0A                resolved.add(lens);%0A            %7D%0A            return resolved;%0A
         %7D ca
@@ -1676,16 +1676,16 @@
 (%22, %22);%0A
+
         
@@ -1743,90 +1743,8 @@
 ) %7B%0A
-                if (arg instanceof Optional) arg = ((Optional) arg).orElse(null);%0A
     

Resolve codelenses in tests