skip to Main Content

we have YAML file where we need to find out whether specific host entry is available or not .

following is code

public class ListTest {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub

        InputStream inputStream = new FileInputStream(new File("C:\Automation\values.yaml"));

        Yaml yaml = new Yaml(new Constructor(Values1.class));

        Values1 data = yaml.load(inputStream);

        data.getRoute().getHosts().forEach(x -> System.out.println(x.getHost()));

        for (Host element : data.getRoute().getHosts()) {
            if (element.contains("goa.dev.abc.com")) {
                System.out.println(element);
                System.out.println("test");
            }
        }

    }

}

Values1.java

public class Values1 {
    
    private Route route;
    private Ingress ingress;
    
    public Ingress getIngress() {
        return ingress;
    }
    public void setIngress(Ingress ingress) {
        this.ingress = ingress;
    }
    public Route getRoute() {
        return route;
    }
    public void setRoute(Route route) {
        this.route = route;
    }

}

Host.java

public class Host {

  private String host;
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public boolean contains(Object obj) {
        // TODO Auto-generated method stub
        
          if(!(obj instanceof Host)) {
                 return false;
              }
          
          Host host = (Host)obj;
          return this.host == host.getHost();
       }
    }

Values.yaml

ingress: {backendServiceName: serv-deployment, cluster_issuer: lt-eks,
  ingressClassName: nginx-lr1, tls_acme: 'true'}
route:
  hosts:
  - {host: goa.dev.abc.com}

Now, we just want to check if , entry, "goa.dev.abc.com" exists or not in the list

Please suggest what is missing

2

Answers


  1. The issue in your code is in the contains method of the Host class. You are using == for string comparison, which checks for reference equality, not content equality. You should use the equals method for string comparison.

    Here’s the corrected contains method:

    public boolean contains(Object obj) {
        if (!(obj instanceof Host)) {
            return false;
        }
    
        Host otherHost = (Host) obj;
        return this.host.equals(otherHost.getHost()); // here was the problem
    }
    

    But actually this contains method is weird and unnecessary here, we can simple use getter for each host:

    for (Host element : data.getRoute().getHosts()) {
                if (element.getHost().equals("goa.dev.abc.com")) {
                    System.out.println(element);
                    System.out.println("test");
                }
            }
    
    Login or Signup to reply.
  2. Your Host.contains method returns true only when it is passed a Host, but you are actually passing it a String. You need something like this:

    public boolean contains(String host)
        return this.host.equals(host);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search