How to use method of org.jmock.AbstractExpectations class

Best Jmock-library code snippet using org.jmock.AbstractExpectations.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.wixpress.guineapig.services;
2
3import com.google.common.collect.ImmutableList;
4import com.google.common.collect.ImmutableSet;
5import com.wixpress.guineapig.dao.MetaDataDao;
6import com.wixpress.guineapig.dto.SpecExposureIdViewDto;
7import com.wixpress.guineapig.dto.UserAgentRegex;
8import com.wixpress.guineapig.entities.ui.FilterOption;
9import com.wixpress.guineapig.entities.ui.UiSpecForScope;
10import com.wixpress.guineapig.spi.*;
11import com.wixpress.guineapig.util.MockHardCodedScopesProvider;
12import com.wixpress.guineapig.util.MockHardCodedScopesProvider$;
13import com.wixpress.petri.experiments.domain.ExperimentSpec;
14import com.wixpress.petri.experiments.domain.ScopeDefinition;
15import com.wixpress.petri.petri.FullPetriClient;
16import org.jmock.AbstractExpectations;
17import org.jmock.Expectations;
18import org.jmock.integration.junit4.JUnitRuleMockery;
19import org.joda.time.DateTime;
20import org.junit.Rule;
21import org.junit.Test;
22import scala.Some;
23import scala.collection.JavaConversions;
24
25import java.util.Collections;
26import java.util.List;
27import java.util.Locale;
28import java.util.Set;
29
30import static com.wixpress.petri.petri.SpecDefinition.ExperimentSpecBuilder.aNewlyGeneratedExperimentSpec;
31import static java.util.Arrays.asList;
32import static org.hamcrest.CoreMatchers.is;
33import static org.junit.Assert.*;
34
35public class MetaDataServiceTest {
36
37    @Rule
38    public JUnitRuleMockery context = new JUnitRuleMockery();
39
40    MetaDataDao metaDataDao = context.mock(MetaDataDao.class);
41    SpecExposureIdRetriever specExposureIdDao = context.mock(SpecExposureIdRetriever.class);
42    FullPetriClient fullPetriClient = context.mock(FullPetriClient.class);
43    Set<String> languagesSet = ImmutableSet.of("en", "fr", "de", "es", "pt");
44    SupportedLanguagesProvider supportedSupportedLanguagesProvider = new MockSupportedSupportedLanguagesProvider(languagesSet);
45    GlobalGroupsManagementService globalGroupsManagementService = context.mock(GlobalGroupsManagementService.class);
46    HardCodedScopesProvider hardCodedScopesProvider = new MockHardCodedScopesProvider();
47
48    MetaDataService metaDataService = new MetaDataService(
49            metaDataDao, fullPetriClient, hardCodedScopesProvider, specExposureIdDao, supportedSupportedLanguagesProvider, globalGroupsManagementService
50    );
51    private final String EDITOR_SCOPE = "editor";
52    ScopeDefinition editorScope = new ScopeDefinition(EDITOR_SCOPE, true);
53    private final String VIEWER_SCOPE = "viewer";
54    ScopeDefinition viewerScope = new ScopeDefinition(VIEWER_SCOPE, false);
55    private final ExperimentSpec viewerEditorSpec = aNewlyGeneratedExperimentSpec("f.q.n.Class1").
56            withTestGroups(asList("1", "2")).
57            withScopes(editorScope, viewerScope).build();
58
59    @Test
60    public void geoListContainsAllCountries() {
61        //ui supply code list examplw : ["IL","FR"]
62        //ui expects list of geo objects [{id:"IL",text:"Israel"},{id:"FR",text:"France"}]
63        List<FilterOption> fullList = metaDataService.getGeoList();
64        assertEquals(fullList.size(), Locale.getISOCountries().length + MetaDataService.COUNTRIES_GROUPS().size());
65        assertThat(fullList.get(0), is(MetaDataService.COUNTRIES_GROUPS().get(0)));
66    }
67
68    @Test
69    public void userAgentRegexListReadsValuesFromDao() {
70        final UserAgentRegex userAgentRegex = new UserAgentRegex("*test", "This is a test");
71
72        context.checking(new Expectations() {{
73            allowing(metaDataDao).get(UserAgentRegex.class);
74            will(AbstractExpectations.returnValue(ImmutableList.of(userAgentRegex)));
75        }});
76
77        List<FilterOption> fullList = metaDataService.getUserAgentRegexList();
78        assertEquals(fullList.get(0).id(), userAgentRegex.regex());
79        assertEquals(fullList.get(0).text(), userAgentRegex.description());
80        assertThat(fullList.size(), is(1));
81    }
82
83
84    @Test
85    public void userGroupsListReadFromGlobalGroupsManagementService() {
86        ImmutableList<String> userGroupsList = ImmutableList.of("group1");
87
88        context.checking(new Expectations() {{
89            allowing(globalGroupsManagementService).allGlobalGroups();
90            will(AbstractExpectations.returnValue(JavaConversions.asScalaBuffer(userGroupsList)));
91        }});
92
93        assertEquals(metaDataService.getUserGroupsList().get(0).id(), userGroupsList.get(0));
94    }
95
96    @Test
97    public void userGroupsListReturnsEmptyListWhenReadFromGlobalGroupsManagementServiceFails() {
98        context.checking(new Expectations() {{
99            allowing(globalGroupsManagementService).allGlobalGroups();
100            will(AbstractExpectations.throwException(new NullPointerException()));
101        }});
102
103        assertThat(metaDataService.getUserGroupsList().size(), is(0));
104    }
105
106    @Test
107    public void languagesListContainsAllWixLanguages() {
108
109        List<FilterOption> fullList = metaDataService.getLangList();
110        assertEquals(fullList.size(), languagesSet.size());
111    }
112
113    @Test
114    public void specWithMultipleScopesIsAddedCorrectly() {
115        context.checking(new Expectations() {{
116
117            allowing(specExposureIdDao).getAll();
118            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));
119
120        }});
121        ScopeDefinition newRegisteredScope = new ScopeDefinition("new-scope", true);
122        ExperimentSpec multipleScopeSpec = aNewlyGeneratedExperimentSpec("f.q.n.Class1").
123                withTestGroups(asList("1", "2")).
124                withScopes(editorScope, newRegisteredScope).build();
125        scala.collection.immutable.Map<String, scala.collection.immutable.List<UiSpecForScope>> map =
126                metaDataService.createScopeToSpecMap(asList(multipleScopeSpec));
127
128        assertThat(map.contains(EDITOR_SCOPE + ",new-scope"), is(true));
129
130    }
131
132    @Test
133    public void editorScopeIsAddedWhenKeyCaseChanged() {
134        context.checking(new Expectations() {{
135
136            allowing(specExposureIdDao).getAll();
137            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));
138
139        }});
140        ExperimentSpec viewerEditorSpec = aNewlyGeneratedExperimentSpec("SPEC_KEY").
141                withTestGroups(asList("1", "2")).
142                withScopes(editorScope, viewerScope).build();
143
144
145        scala.collection.immutable.Map<String, scala.collection.immutable.List<UiSpecForScope>> map =
146                metaDataService.createScopeToSpecMap(asList(viewerEditorSpec));
147
148        assertThat(map.get(EDITOR_SCOPE).get().head().getKey(), is(viewerEditorSpec.getKey()));
149
150    }
151
152    @Test
153    public void scopesMapAddsHardcodedScope() {
154        context.checking(new Expectations() {{
155
156            allowing(fullPetriClient).fetchSpecs();
157            will(AbstractExpectations.returnValue(ImmutableList.of(viewerEditorSpec)));
158
159
160            allowing(specExposureIdDao).getAll();
161            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));
162
163            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();
164            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));
165        }});
166
167        java.util.Map<String, List<UiSpecForScope>> map = metaDataService.createScopeToSpecMap();
168        assertThat(map.size(), is(4));
169        assertTrue(map.containsKey(MockHardCodedScopesProvider$.MODULE$.HARD_CODED_SPEC_FOR_NON_REG()));
170    }
171
172    @Test
173    public void scopeMapExposureId() {
174        final ExperimentSpec specWithExposure = aNewlyGeneratedExperimentSpec("f.q.n.Class1")
175                .withScopes(editorScope)
176                .build();
177
178        final ExperimentSpec specWithoutExposure = aNewlyGeneratedExperimentSpec("f.q.n.Class2")
179                .withScopes(viewerScope)
180                .build();
181        final SpecExposureIdViewDto specExposure = new SpecExposureIdViewDto(
182                "f.q.n.Class1", new Some<>("GUID"), new DateTime(), new DateTime()
183        );
184
185        context.checking(new Expectations() {{
186
187            allowing(fullPetriClient).fetchSpecs();
188            will(AbstractExpectations.returnValue(asList(specWithExposure, specWithoutExposure)));
189
190            allowing(specExposureIdDao).getAll();
191            will(AbstractExpectations.returnValue(asList(specExposure)));
192
193            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();
194            will(AbstractExpectations.returnValue(Collections.EMPTY_LIST));
195        }});
196
197        java.util.Map<String, List<UiSpecForScope>> map = metaDataService.createScopeToSpecMap();
198        assertThat(map.get(editorScope.getName()).get(0).getExposureId(), is("GUID"));
199        assertNull(map.get(viewerScope.getName()).get(0).getExposureId());
200    }
201
202    @Test
203    public void checkGetSpecExposureMapFromDB() {
204        context.checking(new Expectations() {{
205
206            allowing(specExposureIdDao).getAll();
207            will(AbstractExpectations.returnValue(asList()));
208
209        }});
210
211        metaDataService.getSpecExposureMapFromDB();
212    }
213
214
215}
216
217
218
Full Screen
copy
1package com.wixpress.guineapig.services;
2
3import com.fasterxml.jackson.core.JsonProcessingException;
4import com.google.common.base.Predicate;
5import com.google.common.collect.ImmutableList;
6import com.natpryce.makeiteasy.MakeItEasy;
7import com.natpryce.makeiteasy.Maker;
8import com.wixpress.guineapig.dsl.ExperimentBuilders;
9import com.wixpress.guineapig.entities.ui.ExperimentConverter;
10import com.wixpress.guineapig.entities.ui.ExperimentReport;
11import com.wixpress.guineapig.entities.ui.UiExperiment;
12import com.wixpress.guineapig.spi.HardCodedScopesProvider;
13import com.wixpress.guineapig.util.MockHardCodedScopesProvider;
14import com.wixpress.guineapig.util.ReportMatchers;
15import com.wixpress.petri.experiments.domain.*;
16import com.wixpress.petri.laboratory.dsl.ExperimentMakers;
17import com.wixpress.petri.petri.Clock;
18import com.wixpress.petri.petri.ConductExperimentSummary;
19import com.wixpress.petri.petri.FullPetriClient;
20import com.wixpress.petri.petri.SpecDefinition;
21import org.hamcrest.CoreMatchers;
22import org.jmock.AbstractExpectations;
23import org.jmock.Expectations;
24import org.jmock.integration.junit4.JUnitRuleMockery;
25import org.joda.time.DateTime;
26import org.junit.Assert;
27import org.junit.Before;
28import org.junit.Rule;
29import org.junit.Test;
30
31import javax.annotation.Nullable;
32import java.io.IOException;
33import java.util.ArrayList;
34import java.util.List;
35
36import static com.wixpress.guineapig.util.Matchers.isForAction;
37import static com.wixpress.petri.experiments.domain.ExperimentSnapshotBuilder.anExperimentSnapshot;
38import static com.wixpress.petri.laboratory.dsl.TestGroupMakers.TEST_GROUPS_WITH_SECOND_ALWAYS_WINNING;
39import static java.util.Arrays.asList;
40import static org.hamcrest.Matchers.empty;
41
42public class ExperimentMgmtServiceTest {
43
44    @Rule
45    public JUnitRuleMockery context = new JUnitRuleMockery();
46    private final FullPetriClient fullPetriClient = context.mock(FullPetriClient.class);
47    private final Clock clock = context.mock(Clock.class);
48    private final EventPublisher experimentEventPublisher = context.mock(EventPublisher.class);
49    private final SpecService specService = context.mock(SpecService.class);
50    private final HardCodedScopesProvider hardCodedScopesProvider = new MockHardCodedScopesProvider();
51    private final ExperimentMgmtService ems = new ExperimentMgmtService(clock, experimentEventPublisher, fullPetriClient, hardCodedScopesProvider);
52    private final String someKey = "someKey";
53
54    private final DateTime now = new DateTime();
55    private String userName = "";
56    private Trigger trigger = new Trigger("", userName);
57
58    @Before
59    public void setup() {
60        assumingTimeSourceReturnsNow();
61    }
62
63    @Test
64    public void returnsEmptyListWhenPetriClientHasNoExperiments() throws Exception {
65        assumingPetriClientContains(new ArrayList<>());
66        Assert.assertThat(ems.getAllExperiments(), CoreMatchers.is(empty()));
67    }
68
69    @Test
70    public void convertsExperimentsIntoUIExperiments() throws Exception {
71        Experiment experiment = MakeItEasy.an(ExperimentMakers.Experiment, MakeItEasy.with(ExperimentMakers.id, 1), MakeItEasy.with(ExperimentMakers.key, "spec1")).make();
72        assumingPetriClientContains(asList(experiment));
73        Assert.assertThat(ems.getAllExperiments(), CoreMatchers.is(asList(experiment)));
74    }
75
76    @Test
77    public void returnsCorrectExperimentById() throws Exception {
78        Experiment experiment1 = MakeItEasy.an(ExperimentMakers.Experiment, MakeItEasy.with(ExperimentMakers.id, 1), MakeItEasy.with(ExperimentMakers.key, "spec1")).make();
79        Experiment experiment2 = MakeItEasy.an(ExperimentMakers.Experiment, MakeItEasy.with(ExperimentMakers.id, 2), MakeItEasy.with(ExperimentMakers.key, "spec2")).make();
80        assumingPetriClientContains(asList(experiment1, experiment2));
81        Assert.assertThat(ems.getExperimentById(2), CoreMatchers.is(experiment2));
82    }
83
84    @Test
85    public void returnsCorrectExperimentsReportById() throws Exception {
86        ConductExperimentSummary summary = new ConductExperimentSummary("localhost", 2, "true", 1L, 1L, new DateTime());
87        assumingPetriClientContains(ImmutableList.of(summary));
88        ExperimentReport experimentReport = ems.getExperimentReport(summary.experimentId());
89        Assert.assertThat(experimentReport, ReportMatchers.hasOneCountForValue("true"));
90    }
91
92    @Test
93    public void returnsDealerNonEditableExperimentById() throws Exception {
94        Experiment experimentOnDealerScope = ExperimentBuilders.createActiveOnNonEditableScope().but(
95                MakeItEasy.with(ExperimentMakers.id, 1),
96                MakeItEasy.with(ExperimentMakers.key, "anyKey"),
97                MakeItEasy.with(ExperimentMakers.fromSpec, false),
98                MakeItEasy.with(ExperimentMakers.scope, NOT_EDITABLE_SCOPE))
99                .make();
100        assumingPetriClientContains(asList(experimentOnDealerScope));
101        UiExperiment uiExperiment = convertToUiExperiment(ems.getExperimentById(1));
102        Assert.assertThat(uiExperiment.isEditable(), CoreMatchers.is(false));
103    }
104
105    @Test
106    public void terminateExperiment() {
107
108        final Experiment futureExperiment = ExperimentBuilders.createFuture().but(
109                MakeItEasy.with(ExperimentMakers.id, 1))
110                .make();
111
112        Assert.assertThat(terminateExperimentWithSpecActive(futureExperiment, true), CoreMatchers.is(true));
113    }
114
115    @Test
116    public void terminateNonOriginalExperimentTerminatesPausedOriginalToo() throws IOException, ClassNotFoundException {
117        final Maker<Experiment> experiment = ExperimentBuilders.createActiveOnNonEditableScope().but(MakeItEasy.with(ExperimentMakers.id, 1), MakeItEasy.with(ExperimentMakers.originalId, 1),
118                MakeItEasy.with(ExperimentMakers.paused, true));
119        final Experiment updatedExperiment = experiment.but(MakeItEasy.with(ExperimentMakers.description, "blah")).make();
120
121        context.checking(new Expectations() {{
122            allowing(fullPetriClient).fetchExperimentById(updatedExperiment.getId());
123            will(AbstractExpectations.returnValue(updatedExperiment));
124
125            allowing(fullPetriClient).fetchAllExperiments();
126            will(AbstractExpectations.returnValue(asList(updatedExperiment, experiment.make())));
127        }});
128
129
130        context.checking(new Expectations() {{
131            allowing(fullPetriClient).updateExperiment(with(updatedExperiment.terminateAsOf(clock.getCurrentDateTime(), trigger)));
132            will(AbstractExpectations.returnValue(updatedExperiment));
133            oneOf(fullPetriClient).updateExperiment(with(experiment.make().terminateAsOf(clock.getCurrentDateTime(), trigger)));
134            will(AbstractExpectations.returnValue(experiment.make()));
135
136            allowing(specService).isSpecActive("", ImmutableList.of());
137            will(AbstractExpectations.returnValue(true));
138
139            oneOf(experimentEventPublisher).publish(with(isForAction(ExperimentEvent.TERMINATED)));
140        }});
141
142        ems.terminateExperiment(updatedExperiment.getId(), "", userName);
143    }
144
145    @Test(expected = IllegalArgumentException.class)
146    public void recognizesInvalidFiltersForScope() throws IOException {
147
148        final ScopeDefinition someScope = ScopeDefinition.aScopeDefinitionForAllUserTypes("someScope");
149
150        final SpecDefinition.ExperimentSpecBuilder someSpec = SpecDefinition.ExperimentSpecBuilder.
151                aNewlyGeneratedExperimentSpec(someKey).withScopes(someScope);
152
153        final ExperimentSnapshot experimentSnapshot = anExperimentSnapshot().
154                withScopes(ImmutableList.of(someScope.getName())).
155                withKey(someKey).
156                withOnlyForLoggedInUsers(false).
157                withGroups(TEST_GROUPS_WITH_SECOND_ALWAYS_WINNING).
158                withFeatureToggle(false).
159                withStartDate(now).
160                withEndDate(now.plusMinutes(5)).
161                withFilters(asList(new WixEmployeesFilter())).
162                build();
163
164
165        context.checking(new Expectations() {{
166            allowing(fullPetriClient).fetchSpecs();
167            will(AbstractExpectations.returnValue(asList(someSpec.build())));
168        }});
169
170        ems.newExperiment(experimentSnapshot);
171    }
172
173    private boolean terminateExperimentWithSpecActive(final Experiment futureExperiment, final boolean isSpecActive) {
174        context.checking(new Expectations() {{
175            allowing(fullPetriClient).fetchAllExperiments();
176            will(AbstractExpectations.returnValue(asList(futureExperiment)));
177
178            allowing(fullPetriClient).fetchExperimentById(futureExperiment.getId());
179            will(AbstractExpectations.returnValue(futureExperiment));
180        }});
181
182        assumingPetriClientContains(asList(futureExperiment));
183
184        context.checking(new Expectations() {{
185            allowing(fullPetriClient).updateExperiment(with(futureExperiment.terminateAsOf(
186                    clock.getCurrentDateTime(), new Trigger("terminate experiment", userName))));
187            will(AbstractExpectations.returnValue(futureExperiment));
188            oneOf(experimentEventPublisher).publish(with(isForAction(ExperimentEvent.TERMINATED)));
189
190            allowing(specService).isSpecActive(futureExperiment.getKey(), ImmutableList.of());
191            will(AbstractExpectations.returnValue(isSpecActive));
192        }});
193
194        return ems.terminateExperiment(futureExperiment.getId(), "terminate experiment", userName);
195    }
196
197    private void assumingTimeSourceReturnsNow() {
198        context.checking(new Expectations() {{
199            allowing(clock).getCurrentDateTime();
200            will(AbstractExpectations.returnValue(now));
201        }});
202    }
203
204    private void assumingPetriClientContains(final List<Experiment> contents) {
205        context.checking(new Expectations() {{
206            allowing(fullPetriClient).fetchAllExperimentsGroupedByOriginalId();
207            will(AbstractExpectations.returnValue(contents));
208        }});
209    }
210
211    private void assumingPetriClientContains(final ImmutableList<ConductExperimentSummary> reports) {
212        context.checking(new Expectations() {{
213            allowing(fullPetriClient).getExperimentReport(2);
214            will(AbstractExpectations.returnValue(reports));
215        }});
216    }
217
218    private UiExperiment convertToUiExperiment(Experiment experiment) throws JsonProcessingException, ClassNotFoundException {
219        ExperimentConverter converter = new ExperimentConverter(new IsEditablePredicate(), new NoOpFilterAdapterExtender());
220        return converter.convert(experiment);
221    }
222
223    public static final String NOT_EDITABLE_SCOPE = "NOT_EDITABLE_SCOPE";
224    public class IsEditablePredicate implements Predicate<Experiment> {
225        @Override
226        public boolean apply(@Nullable Experiment experiment) {
227            return !NOT_EDITABLE_SCOPE.equals(experiment.getScope());
228        }
229    }
230}
231
Full Screen
copy
1/*
2 * Copyright (C) 2019 CLARIN
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package eu.clarin.cmdi.vlo.importer.linkcheck;
18
19import com.google.common.collect.ImmutableMap;
20import eu.clarin.cmdi.rasa.DAO.CheckedLink;
21import eu.clarin.cmdi.rasa.filters.CheckedLinkFilter;
22import eu.clarin.cmdi.rasa.linkResources.CheckedLinkResource;
23import eu.clarin.cmdi.vlo.importer.linkcheck.RasaResourceAvailabilityStatusChecker.RasaResourceAvailabilityStatusCheckerConfiguration;
24import java.io.IOException;
25import java.io.Writer;
26import java.sql.SQLException;
27import java.sql.Timestamp;
28import java.time.Duration;
29import java.time.Instant;
30import java.util.AbstractMap;
31import java.util.Map;
32import java.util.stream.Stream;
33import static org.jmock.AbstractExpectations.any;
34import static org.jmock.AbstractExpectations.returnValue;
35import org.jmock.Expectations;
36import org.jmock.Mockery;
37import org.jmock.integration.junit4.JUnit4Mockery;
38import static org.junit.Assert.assertEquals;
39import static org.junit.Assert.assertNotNull;
40import static org.junit.Assert.assertTrue;
41import org.junit.Before;
42import org.junit.Test;
43
44/**
45 * @author Twan Goosen <twan@clarin.eu>
46 */
47public class RasaResourceAvailabilityStatusCheckerTest {
48
49    private final Mockery context = new JUnit4Mockery();
50
51    private CheckedLinkResource checkedLinkResource;
52    private RasaResourceAvailabilityStatusChecker instance;
53    private CheckedLinkFilter checkedLinkFilter;
54
55    private final ImmutableMap<String, CheckedLink> checkedLinksMap = ImmutableMap.<String, CheckedLink>builder()
56            .put(createResponseMapEntry("http://uri1", 200))
57            .put(createResponseMapEntry("http://uri2", 404))
58            .build();
59
60    @Before
61    public void setUp() throws SQLException {
62        checkedLinkResource = context.mock(CheckedLinkResource.class);
63        checkedLinkFilter = context.mock(CheckedLinkFilter.class);
64
65        instance = new RasaResourceAvailabilityStatusChecker(checkedLinkResource,
66                new RasaResourceAvailabilityStatusCheckerConfiguration(Duration.ofDays(10))) {
67            @Override
68            public void writeStatusSummary(Writer writer) throws IOException {
69                writer.write("Status - test " + getClass());
70            }
71        };
72    }
73
74    /**
75     * Test of getLinkStatusForRefs method, of class
76     * RasaResourceAvailabilityStatusChecker.
77     */
78    @Test
79    public void testGetLinkStatusForRefs() throws Exception {
80        context.checking(new Expectations() {
81            {
82                atLeast(1).of(checkedLinkResource).getCheckedLinkFilter();
83                will(returnValue(checkedLinkFilter));
84
85                atLeast(1).of(checkedLinkResource).getMap(with(any(CheckedLinkFilter.class)));
86                will(returnValue(checkedLinksMap));
87
88                oneOf(checkedLinkFilter).setUrlIn(with(any(String[].class)));
89
90                oneOf(checkedLinkFilter).setCheckedBetween(with(any(Timestamp.class)), with(any(Timestamp.class)));
91
92            }
93        });
94
95        System.out.println("getLinkStatusForRefs");
96        Stream<String> hrefs = Stream.of("http://uri3", "http://uri2", "http://uri1");
97        Map<String, CheckedLink> result = instance.getLinkStatusForRefs(hrefs);
98        assertEquals(2, result.size());
99        assertNotNull(result.get("http://uri1"));
100        assertEquals("http://uri1", result.get("http://uri1").getUrl());
101        assertNotNull(result.get("http://uri1").getStatus());
102        assertEquals(200, result.get("http://uri1").getStatus().intValue());
103        assertNotNull(result.get("http://uri2"));
104        assertEquals("http://uri2", result.get("http://uri2").getUrl());
105        assertNotNull(result.get("http://uri2").getStatus());
106        assertEquals(404, result.get("http://uri2").getStatus().intValue());
107    }
108
109    @Test
110    public void testConstructConfig() {
111        final RasaResourceAvailabilityStatusCheckerConfiguration config
112                = new RasaResourceAvailabilityStatusCheckerConfiguration(Duration.ofDays(99));
113        Timestamp ageLimitLowerBound = config.getAgeLimitLowerBound();
114        Timestamp ageLimitUpperBound = config.getAgeLimitUpperBound();
115
116        assertTrue(ageLimitLowerBound.before(ageLimitUpperBound));
117
118        assertTrue(ageLimitLowerBound.before(Timestamp.from(Instant.now().minus(Duration.ofDays(98)))));
119        assertTrue(ageLimitLowerBound.after(Timestamp.from(Instant.now().minus(Duration.ofDays(100)))));
120
121        assertTrue(ageLimitUpperBound.before(Timestamp.from(Instant.now().plus(Duration.ofMinutes(1)))));
122    }
123
124    @Test(expected = IllegalArgumentException.class)
125    public void testConstructConfigIllegalAge1() {
126        final RasaResourceAvailabilityStatusCheckerConfiguration config
127                = new RasaResourceAvailabilityStatusCheckerConfiguration(Duration.ofDays(0));
128    }
129
130    @Test(expected = IllegalArgumentException.class)
131    public void testConstructConfigIllegalAge2() {
132        final RasaResourceAvailabilityStatusCheckerConfiguration config
133                = new RasaResourceAvailabilityStatusCheckerConfiguration(Duration.ofDays(-99));
134    }
135
136    public static AbstractMap.SimpleImmutableEntry<String, CheckedLink> createResponseMapEntry(
137            String url, int status) {
138        final CheckedLink checkedLink = new CheckedLink();
139        checkedLink.setUrl(url);
140        checkedLink.setStatus(status);
141        return new AbstractMap.SimpleImmutableEntry<>(url, checkedLink);
142    }
143}
144
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)