package com.jakubu9333.grammarManager;

import com.jakubu9333.grammarManager.model.Grammar;
import com.jakubu9333.grammarManager.model.RightSidePart;
import com.jakubu9333.grammarManager.model.RuleModel;
import com.jakubu9333.grammarManager.service.FileService;
import com.jakubu9333.grammarManager.service.GrammarService;

import com.jakubu9333.grammarManager.service.RuleService;
import org.javatuples.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.server.ResponseStatusException;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @author Jakub Uhlarik
 */
@SpringBootTest
public class FileGrammarServiceTest {

    @Autowired
    public GrammarService grammarService;

    @Autowired
    public FileService fileService;
    @Autowired
    public RuleService ruleService;

    @AfterEach
    public void tearDown() {
        grammarService.deleteAll();
    }

    @Test
    public void jsgfReadTest() {
        boolean xml = false;
        try {
            fileService.addGrammarFromFile("", xml, null, false, false);
            assert false;
        } catch (Exception ignored) {

        }

        String file1 = """
                #JSGF V1.0;
                grammar first;
                <rule1> = smile;
                //root;
                <rule2> = cry|sis;
                                
                               
                          
                """;
        Grammar firstGrammar = fileService.addGrammarFromFile(file1, xml, null, false, false).getValue0();
        assert firstGrammar.getUri().equals("first");
        assert firstGrammar.getRules().size() == 2;
        assert firstGrammar.getRules().equals(new HashSet<>(List.of(new RuleModel("<rule1>", splitRightSide("smile")), new RuleModel("<rule2>", splitRightSide("cry|sis")))));

        String file2 = """
                #JSGF V1.0;
                grammar dms;
                ddsd=sfdsfd;
                grammar xfs;
                                
                """;
        boolean error = false;
        try {
            fileService.addGrammarFromFile(file2, xml, null, false, false);
        } catch (Exception e) {
            error = true;
        }
        assert error;
        String file3 = """
                                
                grammar dms;
                //root;
                ddsd=sfdsfd;
                 //root;
                ddssfsd=sfdsfd;
                                
                """;
        error = false;
        try {
            fileService.addGrammarFromFile(file3, xml, null, false, false);
        } catch (ResponseStatusException e) {
            error = true;
        }
        assert error;
    }

    private Set<RightSidePart> splitRightSide(String part) {
        return Stream.of(part.split("\\|")).map(RightSidePart::new).collect(Collectors.toSet());
    }

    @Test
    public void xmlReadTest() {
        boolean xml = true;
        boolean er = false;
        try {
            fileService.addGrammarFromFile("", xml, null, false, false);
        } catch (ResponseStatusException e) {
            er = true;
        }
        assert er;

        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            xml:lang= "en"
                           xml:base="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            <rule id="rule1">
                            <one-of>
                            <item>123
                            </item>
                             <item>sm <ruleref special= "NULL"  />hah</item>
                            
                            <item>smile <ruleref uri= "#rule2"  />haha</item>
                            </one-of>
                            </rule>
                                <rule id="rule2">
                                    <item>cry</item>
                                </rule>
                            </grammar>
                          
                """;
        Grammar firstGrammar = fileService.addGrammarFromFile(file1, xml, null, false, false).getValue0();
        assert firstGrammar.getUri().equals("first");
        RuleModel rule1 = new RuleModel(null, "<rule1>", new HashSet<>(List.of(new RightSidePart("123"), new RightSidePart("sm <NULL> hah"), new RightSidePart("smile <rule2> haha"))), false);
        assert firstGrammar.getRules().equals(new HashSet<>(List.of(rule1, new RuleModel(null, "<rule2>", new HashSet<>(List.of(new RightSidePart("cry"))), false))));

        String file2 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                           xml:base="first"
                           xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            <rule id="rule1">
                            <one-of>
                            <item>123
                            </item>
                             <item><ruleref special= "NULL"  /></item>
                            <item>x<ruleref special= "VOID"  /></item>
                            <item>smile <ruleref uri= "#rule2"  />haha</item>
                            </one-of>
                            </rule>
                                <rule id="rule2">
                                    <item>cry</item>
                                </rule>
                            </grammar>
                          
                """;
        fileService.addGrammarFromFile(file2, xml, null, false, false);


        String fileRoot = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                           xml:base="first"
                           xml:lang= "en"
                           root="rule1"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            <rule id="rule1">
                            <one-of>
                            <item>123
                            </item>
                            <item>smile <ruleref uri= "#rule2"  />haha</item>
                            </one-of>
                            </rule>
                                <rule id="rule2">
                                    <item>cry</item>
                                </rule>
                            </grammar>
                          
                """;
        Grammar rootGr = fileService.addGrammarFromFile(fileRoot, xml, null, false, false).getValue0();
        assert rootGr.getRootRule() != null;
        String file3 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                           xml:base="second"
                           xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            <rule id="rule1">
                            <one-of>
                            <item>123
                            </item>
                             <item>sm <ruleref special= "NULL"  />hah</item>
                            
                            <item>smile <ruleref uri= "#rule2"  />haha</item>
                            </one-of>
                            </rule>
                                <rule id="rule2">
                                    cry sis  <ruleref uri= "#xd"  />
                                </rule>
                            </grammar>
                          
                """;
        Grammar second = fileService.addGrammarFromFile(file3, xml, null, false, false).getValue0();
        assert second.getUri().equals("second");
        HashSet<RuleModel> rules = new HashSet<>(List.of(rule1, new RuleModel(null, "<rule2>", new HashSet<>(List.of(new RightSidePart("cry sis <xd>"))), false)));
        assert second.getRules().equals(rules);
    }

    @Test
    public void testErrorMessages() {


        String file = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="second"
                            xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            <rule id="rule1">
                            <one-of>
                            <item>"123"
                            </item>
                            <item>"smile "<ruleref uri= "#rule2"  />haha</item>
                            </one-of>
                     
                                <rule id="rule2">
                                    <item>"cry"</item>
                                </rule>
                            </grammar>
                          
                """;
        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                            </rule>
                            </grammar>
                """;

        List<String> files = new ArrayList<>();

        files.add(file);
        files.add(file1);

        String file2 = """
                             <?xml version="1.0"?>
                             <rule id="2"> </rule>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              </grammar>
                """;
        files.add(file2);


        String file3 = """
                             <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"><item> "ddd" </item>
                              </rule>
                              <one-of>
                              </one-of>
                              </grammar>
                """;
        files.add(file3);

        String file4 = """
                             <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"> <one-of><item>"bubub"
                              </one-of>
                              </rule>
                             </item>
                              </grammar>
                """;
        files.add(file4);


        String file5 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"> <one-of><item>"cry"
                             </item>
                              </rule>
                              </one-of>
                             
                              </grammar>
                """;
        files.add(file5);

        String file6 = """
                            <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"><item>"smile"</item>
                              </one-of>
                              </rule>
                             
                              </grammar>
                """;
        files.add(file6);

        String file7 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"> <one-of><item>"xd"</item>
                              </one-of>
                              </rule>
                            <item>"outside"</item>
                              </grammar>
                """;

        files.add(file7);
        String file8 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            tag-format="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="2"> <one-of><item>"ff</item>
                              </one-of>
                              </rule>
                              <rule id="5>  <one-of><item>"ls</item>
                              </one-of>
                    
                              </grammar>
                """;

        files.add(file8);


        for (String s : files) {

            testErrors(s);
        }

    }

    private void testErrors(String file) {

        try {
            fileService.addGrammarFromFile(file.trim(), true, null, false, false);
            assert false;
        } catch (ResponseStatusException ignored) {

        }

    }


    @Test
    public void testComment() {
        String file1 = """
                #JSGF  V1.0;
                grammar first;
                /* dsg = xdcko
                drgd
                dgdgdg
                dgdgd
                dgdgd
                rdrgdgdgd
                */
                <rule3>  =/* xd   */ smiling;
                /* xd  fsdfsfsfsfsf
                sfsf
                sfsdfdsf
                sdfdsfsd
                dfsdfsdf
                sfsdfdsfsdf
                dsfsdfdsfs */ <rule4> = xdd;
                /* dfsfsfssfs */  <rule5> = xdgd;
                //<rule1> = smile;
                //root;
                <rule2> = ddggd;
                """;

        Grammar grammar = fileService.addGrammarFromFile(file1, false, null, false, false).getValue0();
        RuleModel rule2 = new RuleModel(null, "<rule2>", new HashSet<>(List.of(new RightSidePart("ddggd"))), false);
        RuleModel rule3 = new RuleModel(null, "<rule3>", new HashSet<>(List.of(new RightSidePart("smiling"))), false);
        RuleModel rule4 = new RuleModel(null, "<rule4>", new HashSet<>(List.of(new RightSidePart("xdd"))), false);
        RuleModel rule5 = new RuleModel(null, "<rule5>", new HashSet<>(List.of(new RightSidePart("xdgd"))), false);
        assert grammar.getRules().size() == 4;


        assert grammar.getRules().equals(new HashSet<>(List.of(rule2, rule4, rule3, rule5)));


        String file2 = """
                 #JSGF v 1.0;
                 grammar first;
                dsg = xdcko */
                 """;
        try {
            fileService.addGrammarFromFile(file2, false, null, false, false);
            assert false;
        } catch (Exception ignored) {

        }
    }


    @Test
    @Transactional
    public void jsgfWrite() {

        String file1 = """
                #JSGF v 1.0;
                grammar first;
                                
                <rule1> = smile;
                //root;
                <rule2> = cry | sis;
                """;
        Grammar grammar = grammarService.processAddGrammar(new Grammar("first", null, null));
        ruleService.processAddRule(new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false), grammar);
        ruleService.processAddRule(new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry"), new RightSidePart("sis"))), true), grammar);
        Pair<String, Integer> result = fileService.newFileFromGrammar(grammar.getId(), false, false);


        System.out.print(result.getValue0());
    }


    @Test
    @Transactional
    public void xmlWrite() {

        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                              xml:base="first"
                              root="rule2"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry <ruleref special="NULL"/>xd</item>
                              <item>sis <ruleref special="VOID"/></item>
                              <item>xd <ruleref urirule3/>si<ruleref uri=#rule1/></item>
                              </one-of>
                              </rule>
                              </grammar>
                """;
        Grammar grammar = grammarService.processAddGrammar(new Grammar("first", null, null));
        ruleService.processAddRule(new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false), grammar);
        ruleService.processAddRule(new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry <NULL>xd"), new RightSidePart("sis <VOID>"), new RightSidePart("xd <rule3>si <rule1>"))), true), grammar);

        Pair<String, Integer> result = fileService.newFileFromGrammar(grammar.getId(), true, false);


        System.out.print(result.getValue0());
    }


    @Test
    public void testGrammarEqualityFiles() {

        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            xml:lang= "en"
                              xml:base="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry</item>
                              <item>sis</item>
                              </one-of>
                              </rule>
                              </grammar>
                """;

        String file2 = """
                #JSGF V1.0;
                                
                grammar first;
                <rule1> = smile;
                <rule2> = cry|sis;
                """;

        Grammar grammarFromXml = fileService.addGrammarFromFile(file1, true, null, false, false).getValue0();
        assert grammarFromXml.getFiles().size() == 0;
        Set<RuleModel> ruleSet = new HashSet<>();
        RuleModel rule1 = new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false);
        RuleModel rule2 = new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry|sis"))), false);
        ruleSet.add(rule1);
        ruleSet.add(rule2);
        Grammar grammarFromObj = new Grammar("first", ruleSet, null);

        grammarFromObj = grammarService.preProcessGrammar(grammarFromObj);
        grammarService.deleteAll();
        Grammar grammarFromJsgf = fileService.addGrammarFromFile(file2, false, null, false, false).getValue0();
        assert grammarFromJsgf.getFiles().size() == 0;

        assert grammarFromXml.equals(grammarFromObj);
        assert grammarFromJsgf.equals(grammarFromObj);
        assert grammarFromJsgf.equals(grammarFromXml);
    }

    @Test
    public void testFileGeneration() {
        Set<RuleModel> ruleSet = new HashSet<>();
        RuleModel rule1 = new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false);
        RuleModel rule2 = new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry|sis"))), false);
        ruleSet.add(rule1);
        ruleSet.add(rule2);
        Grammar grammarFromObj = new Grammar("first", ruleSet, null);
        grammarFromObj = grammarService.processAddGrammar(grammarFromObj);
        Long id = grammarFromObj.getId();
        Pair<String, Integer> result = fileService.newFileFromGrammar(id, false, true);
        assert result.getValue1()==1;

        assert fileService.newFileFromGrammar(id, false, true).getValue1()==0;
        Pair<String, Integer> result1 = fileService.newFileFromGrammar(id, true, true);
        assert result1.getValue1()==1;
        grammarService.changeUri(id, new Grammar("xd", null, null));
        assert fileService.newFileFromGrammar(id, false, true).getValue1()==2;
        grammarService.deleteById(id);
        grammarFromObj = grammarService.processAddGrammar(grammarFromObj);
        fileService.newFileFromGrammar(grammarFromObj.getId(), false, true);
    }


    @Test
    public void testFileSaving() {
        String fileXmlObj = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                              xml:base="firstObj"
                              xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry</item>
                              <item>sis</item>
                              </one-of>
                              </rule>
                              </grammar>
                """;

        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                              xml:base="first"
                              xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry</item>
                              <item>sis</item>
                              </one-of>
                              </rule>
                              </grammar>
                """;
        String fileXmlSameJsgf = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                              xml:base="first1"
                              xml:lang= "en"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry</item>
                              <item>sis</item>
                              </one-of>
                              </rule>
                              </grammar>
                """;
        String file2 = """
                #JSGF V1.0;
                                
                grammar first1;
                <rule1> = smile;
                <rule2> = cry|sis;
                """;
        Grammar grammarFromJsgf = fileService.addGrammarFromFile(file2, false, null, false, true).getValue0();
        Pair<Grammar, Boolean> resultFromXml = fileService.addGrammarFromFile(file1, true, null, false, true);
        Grammar grammarFromXml = resultFromXml.getValue0();
        assert resultFromXml.getValue1();
        assert grammarFromJsgf.getFiles().stream().toList().get(0).getGrammar().equals(grammarFromJsgf);
        assert grammarFromXml.getFiles().stream().toList().get(0).getGrammar().equals(grammarFromXml);
        String file2diff = """
                #JSGF V1.0;
                                
                grammar first12;
                <rule1> = smile;
                <rule2> = crfy|sis;
                """;
        Set<RuleModel> ruleSet = new HashSet<>();
        RuleModel rule1 = new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false);
        RuleModel rule2 = new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry|sis"))), false);
        ruleSet.add(rule1);
        ruleSet.add(rule2);
        Grammar grammarFromObj = new Grammar("firstObj", ruleSet, null);
        grammarFromObj = grammarService.processAddGrammar(grammarFromObj);
        Long id = grammarFromObj.getId();
        assert fileService.addGrammarFromFile(file2diff, false, grammarFromJsgf.getId(), false, true) == null;
        Pair<Grammar, Boolean> resXml2 = fileService.addGrammarFromFile(fileXmlSameJsgf, true, grammarFromJsgf.getId(), false, true);
        Grammar grammarFromXml2 = resXml2.getValue0();
        assert !resXml2.getValue1();
        assert grammarFromXml2.getFiles().stream().toList().size() == 2;
        Pair<Grammar, Boolean> resFromObj2 = fileService.addGrammarFromFile(fileXmlObj, true, id, false, true);
        Grammar grammarFromObj2 = resFromObj2.getValue0();
        assert !resFromObj2.getValue1();
        assert grammarFromObj2.getFiles().stream().toList().size() == 1;
        assert grammarFromObj2.getFiles().stream().toList().get(0).getGrammar().equals(grammarFromObj2);
        Pair<Grammar, Boolean> resFromJsgf2 = fileService.addGrammarFromFile(file2diff, false, grammarFromJsgf.getId(), true, true);
        Grammar grammarFromJsgf2 = resFromJsgf2.getValue0();
        assert resFromJsgf2.getValue1();
        assert grammarFromJsgf2.getFiles().stream().toList().size() == 1;
        assert grammarFromJsgf2.getFiles().stream().toList().get(0).getGrammar().equals(grammarFromJsgf2);
    }

    @Test
    public void testUnknownSource() {
        String file = """
                <?xml version="1.0"?>
                <grammar version="1.0"
                xml:base="xd"
                xml:lang= "en"
                \t\t\t\t root="sde"
                 xmlns="http://www.w3.org/2001/06/grammar">
                <rule id="p">
                <one-of>
                <item>d
                </item>
                <item>pred<ruleref uri=              "#xd"                  />zad</item>
                </one-of>
                </rule>
                \t<rule id="sde">
                \t\t<item> mam rad zvuk <ruleref uri = "#mirozbirka" />  zvuk jabloní</item>
                \t</rule>
                </grammar>""";

        fileService.addGrammarFromFile(file, null, null, false, false);
        String file2diff = """
                                
                                
                                
                                
                                
                     #JSGF V1.0;
                                
                grammar first12;
                <rule1> = smile;
                <rule2> = crfy|sis;
                """;
        fileService.addGrammarFromFile(file2diff, null, null, false, false);


        String badDiff = """
                                
                                
                                
                                
                                
                     #JGF V1.0;
                                
                grammar first12;
                <rule1> = smile;
                <rule2> = crfy|sis;
                """;
        try {
            fileService.addGrammarFromFile(badDiff, null, null, false, false);
            assert false;
        } catch (Exception ignored) {

        }

    }

    @Test
    @Transactional
    public void testRegenWithFiles() {
        Set<RuleModel> ruleSet = new HashSet<>();
        RuleModel rule1 = new RuleModel(null, "rule1", new HashSet<>(List.of(new RightSidePart("smile"))), false);
        RuleModel rule2 = new RuleModel(null, "rule2", new HashSet<>(List.of(new RightSidePart("cry|sis"))), false);
        RuleModel rule3 = new RuleModel(null, "rule3", new HashSet<>(List.of(new RightSidePart("smi558e"))), false);
        ruleSet.add(rule1);
        ruleSet.add(rule2);
        Grammar grammarFromObj = new Grammar("firstObj", ruleSet, null);
        grammarFromObj = grammarService.processAddGrammar(grammarFromObj);
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==1;
        grammarService.changeUri(grammarFromObj.getId(), new Grammar("xfdsfs", null, null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;
        rule3 = ruleService.processAddRule(rule3, grammarFromObj).getValue0();
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;
        rule3 = ruleService.updateRule(rule3.getId(), new RuleModel(null, "xd", null, null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;
        rule3 = ruleService.updateRule(rule3.getId(), new RuleModel(null, null, new HashSet<>(List.of(new RightSidePart("smi5dgdgd58e"))), null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;
        rule3 = ruleService.updateRule(rule3.getId(), new RuleModel(null, "ssfs", new HashSet<>(List.of(new RightSidePart("smi5dddgdgdgd58e"))), null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;
        ruleService.updateRule(rule3.getId(), new RuleModel(null, "ssfs", new HashSet<>(List.of(new RightSidePart("smi5dddgdgdgd58e"))), null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, true).getValue1()==2;

        ruleService.updateRule(rule3.getId(), new RuleModel(null, "ssfs", new HashSet<>(List.of(new RightSidePart("smi5ddgdgd58e"))), null));
        assert fileService.newFileFromGrammar(grammarFromObj.getId(), true, false).getValue1() ==2;
    }


    @Test
    public void testFileDelete() {
        String file1 = """
                <?xml version="1.0"?>
                            <grammar version="1.0"
                            xml:lang= "en"
                              xml:base="first"
                             xmlns="http://www.w3.org/2001/06/grammar">
                              <rule id="rule1"> <item>smile</item>
                                </rule>
                                <rule id="rule2">
                              <one-of>
                              <item>cry</item>
                              <item>sis</item>
                              </one-of>
                              </rule>
                              </grammar>
                """;
        Grammar grammarFromJsgf = fileService.addGrammarFromFile(file1, true, null, false, true).getValue0();
        fileService.deleteFilesFromGrammar(grammarFromJsgf.getId());
        assert fileService.getAllFiles().size() == 0;
        grammarFromJsgf = fileService.addGrammarFromFile(file1, true, null, false, true).getValue0();
        Long id = grammarFromJsgf.getFiles().stream().toList().get(0).getId();
        fileService.deleteFileById(id);
        assert fileService.getAllFiles().size() == 0;
    }
}
