aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/io')
-rw-r--r--src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java126
-rw-r--r--src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java88
2 files changed, 126 insertions, 88 deletions
diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java
new file mode 100644
index 0000000..66f864b
--- /dev/null
+++ b/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java
@@ -0,0 +1,126 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+package io.swagger.api.impl;
+
+
+import com.google.common.collect.Lists;
+import io.swagger.model.DCAEServiceGroupByResults;
+import io.swagger.model.DCAEServiceGroupByResultsPropertyValues;
+import org.assertj.core.util.Maps;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import javax.ws.rs.core.Link;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DcaeServicesGroupByApiServiceImplTest {
+
+
+ @Mock
+ private DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider runningDcaeServicesProvider;
+ @Mock
+ private DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver dcaeServicesLinkResolver;
+ @Mock
+ private UriInfo uriInfo;
+ @Mock
+ private SecurityContext securityContext;
+ private DcaeServicesGroupByApiServiceImpl dcaeServicesGroupbyApiService;
+
+ @Before
+ public void setUp() {
+ this.dcaeServicesGroupbyApiService = new DcaeServicesGroupByApiServiceImpl(
+ runningDcaeServicesProvider, dcaeServicesLinkResolver);
+ }
+
+ @Test
+ public void shouldReportBadRequestResponseWhenColumnNameDoesNotExist() {
+ final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet(
+ "columnNameDoesNotExist",
+ uriInfo,
+ securityContext
+ );
+
+ assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
+ }
+
+ @Test
+ public void shouldMapPropertyNameToProperColumnName() {
+ assertThat(dcaeServicesGroupbyApiService.resolveColumnName("type")).isEqualTo("type_id");
+ assertThat(dcaeServicesGroupbyApiService.resolveColumnName("vnfLocation")).isEqualTo("vnf_location");
+ assertThat(dcaeServicesGroupbyApiService.resolveColumnName("vnfType")).isEqualTo("vnf_type");
+ assertThat(dcaeServicesGroupbyApiService.resolveColumnName("unknownPropertyName")).isEqualTo("n/a");
+ }
+
+ @Test
+ public void shouldCreateQueryTemplate() {
+ assertThat(
+ DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider.createQuery("type")
+ ).isEqualTo("select type, count(1) as num from dcae_services where status = :serviceStatus group by type order by count(1) desc");
+ }
+
+ @Test
+ public void shouldReturnOkResponseWhenThereIsNoRunningDcaeServices(){
+ final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet("type", uriInfo, securityContext);
+ assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
+ }
+ @Test
+ public void shouldReturnOkResponseWhenThereIsOneRunningDcaeService(){
+
+ // given
+ final Map<String, Object> dataMap = Maps.newHashMap("num", 1L);
+ dataMap.put("type_id", "some_id_value");
+
+ List<Map<String, Object>> data = Lists.newArrayList(dataMap);
+ when(runningDcaeServicesProvider.fetch(eq("type_id"))).thenReturn(data);
+
+ Link link = mock(Link.class);
+ when(dcaeServicesLinkResolver.resolveLink(eq("type"),eq(uriInfo),eq("some_id_value"))).thenReturn(link);
+
+ // when
+ final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet("type", uriInfo, securityContext);
+
+ // then
+ assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
+
+ final DCAEServiceGroupByResults entity = (DCAEServiceGroupByResults) response.getEntity();
+ assertThatEntityResponseContainsInformationAboutRunningDcaeServices(entity);
+ }
+
+ void assertThatEntityResponseContainsInformationAboutRunningDcaeServices(DCAEServiceGroupByResults entity) {
+ assertThat(entity.getPropertyName()).isEqualTo("type");
+ assertThat(entity.getPropertyValues().size()).isEqualTo(1);
+ final DCAEServiceGroupByResultsPropertyValues propertyValues = entity.getPropertyValues().get(0);
+ assertThat(propertyValues.getCount()).isEqualTo(1);
+ assertThat(propertyValues.getPropertyValue()).isEqualTo("some_id_value");
+ }
+}
diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java
deleted file mode 100644
index c2d0944..0000000
--- a/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package io.swagger.api.impl;/*-
- * ============LICENSE_START=======================================================
- * dcae-inventory
- * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.
- * ============LICENSE_END=========================================================
- */
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.onap.dcae.inventory.daos.InventoryDAOManager;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-import io.swagger.api.Util;
-
-/**
- * Created by mhwang on 9/27/17.
- */
-@PrepareForTest({InventoryDAOManager.class})
-@RunWith(PowerMockRunner.class)
-public class DcaeServicesGroupbyApiServiceImplTests {
-
- @Before
- public void setupClass() {
- // PowerMockito does bytecode magic to mock static methods and use final classes
- PowerMockito.mockStatic(InventoryDAOManager.class);
- InventoryDAOManager mockDaoManager = mock(InventoryDAOManager.class);
-
- when(InventoryDAOManager.getInstance()).thenReturn(mockDaoManager);
- }
-
- @Test
- public void testBadRequest() {
- DcaeServicesGroupbyApiServiceImpl api = new DcaeServicesGroupbyApiServiceImpl();
-
- String propertyName = "non-existent";
- UriInfo uriInfo = new Util.FakeUriInfo();
- Response response = api.dcaeServicesGroupbyPropertyNameGet(propertyName, uriInfo, null);
- assertEquals(response.getStatus(), 400);
- }
-
- /*
- Commented this unit test because could not get past Nullpointer in the line trying to mock the explicit "bind"
- function call. Mockito does not handle mocking overloaded functions well so it goes into the actual method where
- an member variable called foreman is null.
- @Test
- public void testNoResults() {
- DcaeServicesGroupbyApiServiceImpl api = new DcaeServicesGroupbyApiServiceImpl();
-
- String propertyName = "type";
- UriInfo uriInfo = new io.swagger.api.Util.FakeUriInfo();
-
- Handle mockHandle = mock(Handle.class);
- when(InventoryDAOManager.getInstance().getHandle()).thenReturn(mockHandle);
- Query mockQueryGeneric = mock(Query.class);
- when(mockHandle.createQuery(any())).thenReturn(mockQueryGeneric);
- // LOOK HERE!
- when(mockQueryGeneric.bind(any(), DCAEServiceObject.DCAEServiceStatus.RUNNING)).thenReturn(mockQueryGeneric);
- when(mockQueryGeneric.list()).thenReturn(null);
-
- Response response = api.dcaeServicesGroupbyPropertyNameGet(propertyName, uriInfo, null);
- assertEquals(response.getStatus(), 400);
- }
- */
-
-}