Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkot committed Nov 10, 2014
2 parents 89ac16e + ad43317 commit 13794b7
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 50 deletions.
55 changes: 55 additions & 0 deletions src/main/java/com/thindeck/api/mock/MkScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2014, Thindeck.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the thindeck.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.thindeck.api.mock;

import com.jcabi.aspects.Immutable;
import com.thindeck.api.Scenario;
import com.thindeck.api.Step;
import java.util.Collections;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* Mock of {@link com.thindeck.api.Scenario}.
*
* @author Slava Semushin ([email protected])
* @version $Id$
* @since 0.5
*/
@Immutable
@ToString
@EqualsAndHashCode
public final class MkScenario implements Scenario {

@Override
public Iterable<Step> steps() {
return Collections.<Step>singletonList(new MkStep());
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/thindeck/api/mock/MkStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2014, Thindeck.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the thindeck.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.thindeck.api.mock;

import com.jcabi.aspects.Immutable;
import com.thindeck.api.Context;
import com.thindeck.api.Step;
import java.io.IOException;
import java.util.Random;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* Mock of {@link com.thindeck.api.Step}.
*
* @author Slava Semushin ([email protected])
* @version $Id$
* @since 0.5
*/
@Immutable
@ToString
@EqualsAndHashCode
public final class MkStep implements Step {

/**
* Step's name.
*/
private final transient String step = String.format(
"Step #%d",
new Random().nextInt()
);

@Override
public String name() {
return this.step;
}

@Override
public void exec(final Context ctx) throws IOException {
// do nothing
}

@Override
public void commit(final Context ctx) throws IOException {
// do nothing
}

@Override
public void rollback(final Context ctx) throws IOException {
// do nothing
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/thindeck/api/mock/MkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public String command() {

@Override
public Scenario scenario() {
throw new UnsupportedOperationException("MkTask#scenario");
return new MkScenario();
}
}
2 changes: 1 addition & 1 deletion src/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<project xmlns="http://maven.apache.org/DECORATION/1.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.3.0 http://maven.apache.org/xsd/decoration-1.3.0.xsd"
name="rultor">
name="thindeck">
<skin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-skin</artifactId>
Expand Down
52 changes: 26 additions & 26 deletions src/test/java/com/thindeck/dynamo/DyBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@
*/
package com.thindeck.dynamo;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.jcabi.dynamo.Frame;
import com.jcabi.dynamo.Item;
import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Region;
import com.jcabi.dynamo.Table;
import com.jcabi.dynamo.Valve;
import com.jcabi.dynamo.mock.H2Data;
import com.jcabi.dynamo.mock.MkRegion;
import com.jcabi.urn.URN;
import java.io.IOException;
import java.util.Collections;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Tests for {@link DyBase}.
Expand All @@ -52,31 +48,35 @@
public final class DyBaseTest {

/**
* DyBase can create DyUser.
* DyBase can retrieve DyUser.
* @throws IOException If something goes wrong.
*/
@Test
public void createsDyUser() throws IOException {
final Region region = Mockito.mock(Region.class);
final Table table = Mockito.mock(Table.class);
final Frame frame = Mockito.mock(Frame.class);
public void retrievesDyUser() throws IOException {
final URN urn = URN.create("urn:test:1");
Mockito.when(region.table(Mockito.eq(DyUser.TBL))).thenReturn(table);
Mockito.when(table.frame()).thenReturn(frame);
Mockito.when(frame.through(Mockito.any(Valve.class))).thenReturn(frame);
Mockito.when(
frame.where(
Mockito.eq(DyUser.ATTR_URN), Mockito.eq(urn.toString())
)
).thenReturn(frame);
final Item item = Mockito.mock(Item.class);
Mockito.when(item.get(DyUser.ATTR_URN))
.thenReturn(new AttributeValue(urn.toString()));
Mockito.when(frame.iterator())
.thenReturn(Collections.singleton(item).iterator());
MatcherAssert.assertThat(
new DyBase(region).user(urn).urn(),
new DyBase(DyBaseTest.region(urn)).user(urn).urn(),
Matchers.equalTo(urn)
);
}

/**
* Create region with single DyUser.
* @param urn URN of the user.
* @return Created region.
* @throws IOException In case of error.
*/
private static Region region(final URN urn)
throws IOException {
final Region region = new MkRegion(
new H2Data().with(
DyUser.TBL,
new String[] {DyUser.ATTR_URN},
new String[0]
)
);
region.table(DyUser.TBL)
.put(new Attributes().with(DyUser.ATTR_URN, urn));
return region;
}
}
23 changes: 2 additions & 21 deletions src/test/java/com/thindeck/life/LifecycleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@
package com.thindeck.life;

import com.thindeck.api.Base;
import com.thindeck.api.Repo;
import com.thindeck.api.Repos;
import com.thindeck.api.Task;
import com.thindeck.api.Tasks;
import com.thindeck.api.Txn;
import com.thindeck.api.mock.MkBase;
import java.io.IOException;
import java.util.Collections;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -75,26 +70,12 @@ public void initializedBaseAttribute() {
/**
* RoutineTxn can increment transactions.
* @throws IOException In case of error.
* @todo #351 Implement MkScenario and MkStep.
* Rewrite the test after that using mocks from
* com.thindeck.api.mock package instead of mockito.
*/
@Test
public void incrementsTransactionCount() throws IOException {
final Base base = Mockito.mock(Base.class);
final Repos repos = Mockito.mock(Repos.class);
final Repo repo = Mockito.mock(Repo.class);
final Tasks tasks = Mockito.mock(Tasks.class);
final Task task = Mockito.mock(Task.class);
final Txn txn = Mockito.mock(Txn.class);
Mockito.when(base.repos()).thenReturn(repos);
Mockito.when(repos.iterate()).thenReturn(Collections.singleton(repo));
Mockito.when(repo.tasks()).thenReturn(tasks);
Mockito.when(tasks.open()).thenReturn(Collections.singleton(task));
Mockito.when(base.txn(Mockito.any(Task.class))).thenReturn(txn);
final Base base = new MkBase();
final RoutineTxns routine = new RoutineTxns(base);
routine.close();
routine.run();
Mockito.verify(txn).increment();
}
}
1 change: 0 additions & 1 deletion src/test/java/com/thindeck/steps/NginxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public void createsHostsConfiguration() throws IOException {
public void createsHostSpecificConfigurationFile() throws IOException {
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
final SSHD sshd = new SSHD(temp);
sshd.start();
final File key = File.createTempFile("ssh", "key", temp);
FileUtils.write(key, sshd.key());
this.manifest(temp, sshd.login(), sshd.port(), key);
Expand Down

0 comments on commit 13794b7

Please sign in to comment.