All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.kubernetes.client.mock.BaseMockOperation Maven / Gradle / Ivy

/**
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * 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 io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.Doneable;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.*;
import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;
import org.easymock.IExpectationSetters;
import org.easymock.internal.matchers.And;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.*;

import static io.fabric8.kubernetes.client.mock.util.MockUtils.getArgument;
import static org.easymock.EasyMock.expect;

public class BaseMockOperation, B extends MockDoneable,
  R extends ClientResource,
  E extends Resource, B, IExpectationSetters, IExpectationSetters, Watcher>>
  implements MockOperation,
  MockResource,
  Mockable {

  private final ClientMixedOperation delegate;
  private final Set nested = new LinkedHashSet<>();
  private final Class mockDoneableType;

  public BaseMockOperation() {
    this(EasyMock.createMock(ClientMixedOperation.class));
  }

  public BaseMockOperation(ClientMixedOperation delegate) {
    this.delegate = delegate;
    this.mockDoneableType = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[3];
  }


  private BaseMockOperation loadedMockOp;
  private BaseMockOperation allNamespacesOp;
  private BaseMockOperation fromServerOp;
  private Map nameMap = new HashMap<>();
  private Map namespaceMap = new HashMap<>();
  private Map resourceVersionMap = new HashMap<>();
  private Map cascadingMap = new HashMap<>();
  private Map gracePeriodMap = new HashMap<>();

  private Map labelMap = new HashMap<>();
  private Map labelNotMap = new HashMap<>();
  private Map labelsMap = new HashMap<>();
  private Map labelsNotMap = new HashMap<>();
  private Map labelInsMap = new HashMap<>();
  private Map labelNotInMap = new HashMap<>();
  private Map fieldMap = new HashMap<>();
  private Map filedsMap = new HashMap<>();

  public Void replay() {
    for (Mockable mockable : nested) {
      mockable.replay();
    }
    EasyMock.replay(delegate);
    return null;
  }

  public void verify() {
    for (Mockable mockable : nested) {
      mockable.verify();
    }

    EasyMock.verify(delegate);
  }

  public BaseMockOperation newInstance() {
    try {
      return getClass().newInstance();
    } catch (Throwable t) {
      throw new RuntimeException();
    }
  }

  public ClientMixedOperation getDelegate() {
    return delegate;
  }

  @Override
  public IExpectationSetters create(T... item) {
    return expect(delegate.create(item));
  }

  @Override
  public B createNew() {
    B mock = null;
    try {
      mock = (B) mockDoneableType.newInstance();
      expect(delegate.createNew()).andReturn((D) mock.getDelegate()).once();
    } catch (Throwable t) {
      throw new RuntimeException(t);
    }
    if (mock instanceof Mockable) {
      nested.add((Mockable) mock);
    }
    return mock;
  }

  @Override
  public IExpectationSetters delete() {
    return expect(delegate.delete());
  }

  @Override
  public B edit() {
    B mock = null;
    try {
      mock = (B) mockDoneableType.newInstance();
      expect(delegate.edit()).andReturn((D) mock.getDelegate()).once();
    } catch (Throwable t) {
      throw new RuntimeException(t);
    }
    if (mock instanceof Mockable) {
      nested.add((Mockable) mock);
    }
    return mock;
  }

  @Override
  public E withName(String name) {
    IArgumentMatcher matcher = getArgument(name);
    BaseMockOperation op = nameMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withName(name)).andReturn((R) op.getDelegate()).anyTimes();
      nested.add(op);
      nameMap.put(matcher, op);
    }
    return (E) op;
  }

  @Override
  public MockNonNamespaceOperation inNamespace(String namespace) {
    IArgumentMatcher matcher = getArgument(namespace);
    BaseMockOperation op = namespaceMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.inNamespace(namespace)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      namespaceMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public MockNonNamespaceOperation inAnyNamespace() {
    if (allNamespacesOp == null) {
      allNamespacesOp = newInstance();
      expect(delegate.inAnyNamespace()).andReturn((ClientNonNamespaceOperation) allNamespacesOp.getDelegate()).anyTimes();
      nested.add(allNamespacesOp);
    }
    return allNamespacesOp;
  }

  @Override
  public EditReplaceDeletable, B, IExpectationSetters> cascading(boolean enabled) {
    IArgumentMatcher matcher = getArgument(enabled);
    BaseMockOperation op = cascadingMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.cascading(enabled)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      namespaceMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public IExpectationSetters update(T item) {
    return replace(item);
  }

  @Override
  public IExpectationSetters replace(T item) {
    return null;
  }

  @Override
  public IExpectationSetters get() {
    return expect(delegate.get());
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withLabels(Map l) {
    IArgumentMatcher matcher = getArgument(l);
    BaseMockOperation op = labelsMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withLabels(l)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelsMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withoutLabels(Map l) {
    IArgumentMatcher matcher = getArgument(l);
    BaseMockOperation op = labelsNotMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withoutLabels(l)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelsNotMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withLabelIn(String key, String... values) {
    IArgumentMatcher keyMatcher = getArgument(key);
    IArgumentMatcher valueMatcher = getArgument(values);
    IArgumentMatcher matcher = new And(Arrays.asList(keyMatcher, valueMatcher));

    BaseMockOperation op = labelInsMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withLabelIn(key, values)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelInsMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withLabelNotIn(String key, String... values) {
    IArgumentMatcher keyMatcher = getArgument(key);
    IArgumentMatcher valueMatcher = getArgument(values);
    IArgumentMatcher matcher = new And(Arrays.asList(keyMatcher, valueMatcher));

    BaseMockOperation op = labelNotInMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withLabelNotIn(key, values)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelNotInMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withLabel(String key, String value) {
    IArgumentMatcher keyMatcher = getArgument(key);
    IArgumentMatcher valueMatcher = getArgument(value);
    IArgumentMatcher matcher = new And(Arrays.asList(keyMatcher, valueMatcher));

    BaseMockOperation op = labelMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withLabel(key, value)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withLabel(String key) {
    IArgumentMatcher matcher = getArgument(key);

    BaseMockOperation op = labelMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withLabel(key)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withoutLabel(String key, String value) {
    IArgumentMatcher keyMatcher = getArgument(key);
    IArgumentMatcher valueMatcher = getArgument(value);
    IArgumentMatcher matcher = new And(Arrays.asList(keyMatcher, valueMatcher));

    BaseMockOperation op = labelNotMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withoutLabel(key, value)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelNotMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withoutLabel(String key) {
    IArgumentMatcher matcher = getArgument(key);

    BaseMockOperation op = labelNotMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withoutLabel(key)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      labelNotMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withFields(Map f) {
    IArgumentMatcher matcher = getArgument(f);
    BaseMockOperation op = filedsMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withFields(f)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      filedsMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public FilterWatchListDeletable, IExpectationSetters, IExpectationSetters, IExpectationSetters, Watcher> withField(String key, String value) {
    IArgumentMatcher keyMatcher = getArgument(key);
    IArgumentMatcher valueMatcher = getArgument(value);
    IArgumentMatcher matcher = new And(Arrays.asList(keyMatcher, valueMatcher));

    BaseMockOperation op = fieldMap.get(matcher);
    if (op == null) {
      op = new BaseMockOperation<>();
      expect(delegate.withField(key, value)).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
      fieldMap.put(matcher, op);
    }
    return op;
  }

  @Override
  public IExpectationSetters list() {
    return expect(delegate.list());
  }


  public Set getNested() {
    return nested;
  }

  @Override
  public MockResource load(InputStream input) {
    if (loadedMockOp == null) {
      loadedMockOp = newInstance();
    }
    expect(delegate.load(input)).andReturn((R) loadedMockOp.getDelegate()).anyTimes();
    nested.add(loadedMockOp);
    return loadedMockOp;
  }

  @Override
  public IExpectationSetters delete(T... items) {
    return expect(delegate.delete(items));
  }

  @Override
  public IExpectationSetters delete(List items) {
    return expect(delegate.delete(items));
  }

  @Override
  public Watchable, Watcher> withResourceVersion(String resourceVersion) {
    IArgumentMatcher matcher = getArgument(resourceVersion);
    BaseMockOperation op = resourceVersionMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withResourceVersion(resourceVersion)).andReturn((R) op.getDelegate()).anyTimes();
      nested.add(op);
      resourceVersionMap.put(matcher, op);
    }
    return (E) op;
  }

  @Override
  public IExpectationSetters watch(Watcher watcher) {
    return expect(delegate.watch(watcher));
  }

  @Override
  public IExpectationSetters watch(String resourceVersion, Watcher watcher) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Gettable> fromServer() {
    BaseMockOperation op = fromServerOp;
    if (op == null) {
      op = newInstance();
      expect(delegate.fromServer()).andReturn(op.getDelegate()).anyTimes();
      nested.add(op);
    }
    fromServerOp = op;
    return op;
  }

  @Override public Deletable> withGracePeriod(long gracePeriodSeconds)
  {
    IArgumentMatcher matcher = getArgument(gracePeriodSeconds);
    BaseMockOperation op = resourceVersionMap.get(matcher);
    if (op == null) {
      op = newInstance();
      expect(delegate.withGracePeriod(gracePeriodSeconds)).andReturn((R) op.getDelegate()).anyTimes();
      nested.add(op);
      gracePeriodMap.put(matcher, op);
    }
    return (E) op;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy