Add tests of Portal API filter.
Extend model (POJO) tests.
No functional changes.
Change-Id: I8d3ea2fa68ef8cbeede1f80afe3eb1c990108595
Signed-off-by: Lott, Christopher (cl778h) <cl778h@att.com>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientResponseException;
/**
- * Provides methods that throw exceptions to support testing.
+ * Extends the Admin controller with methods that throw exceptions to support
+ * testing.
*/
@RestController
@RequestMapping(value = AdminController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
-public class AdminController2 {
+public class AdminControllerExtension {
+
+ public static final String HTTP_STATUS_CODE_EXCEPTION_METHOD = "hscexception";
+ public static final String REST_CLIENT_RESPONSE_EXCEPTION_METHOD = "rcrexception";
+ public static final String RUNTIME_EXCEPTION_METHOD = "rexception";
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- @GetMapping("throw1")
- public void throw1() {
+ @GetMapping(HTTP_STATUS_CODE_EXCEPTION_METHOD)
+ public void throwHttpStatusCodeException() {
+ logger.warn("throwing HttpStatusCodeException");
+ throw new HttpClientErrorException(HttpStatus.CHECKPOINT, "simulate http status code exception");
+ }
+
+ @GetMapping(REST_CLIENT_RESPONSE_EXCEPTION_METHOD)
+ public void throwRestClientResponseException() {
logger.warn("throwing RestClientResponseException");
- throw new RestClientResponseException("foo", 0, "bar", null, null, null);
+ throw new RestClientResponseException("simulate remote client failure", 0, "bar", null, null, null);
}
- @GetMapping("throw2")
- public void throw2() {
+ @GetMapping(RUNTIME_EXCEPTION_METHOD)
+ public void throwRuntimeException() {
logger.warn("throwing RuntimeException");
- throw new RuntimeException("throw2");
+ throw new RuntimeException("simulate runtime failure");
}
}
@Test
public void getxAppMetricsUrlTest() {
Map<String, String> metricsQueryParms = new HashMap<String, String>();
+ URI uri;
+
+ metricsQueryParms.clear();
metricsQueryParms.put("app", DashboardConstants.APP_NAME_AC);
- URI uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
+ uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
logger.debug("Invoking {}", uri);
ResponseEntity<SuccessTransport> successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
null, SuccessTransport.class);
Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
+
+ metricsQueryParms.clear();
+ metricsQueryParms.put("app", DashboardConstants.APP_NAME_MC);
+ logger.debug("Invoking {}", uri);
+ successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null, SuccessTransport.class);
+ Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
+ Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
}
@Test
Assertions.assertTrue(errorResponse.getStatusCode().is4xxClientError());
}
+ @Test
+ public void throwHttpStatusCodeExceptionTest() {
+ URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
+ AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
+ logger.debug("Invoking {}", uri);
+ ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
+ null, ErrorTransport.class);
+ logger.debug("{}", errorResponse.getBody().getError().toString());
+ Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+ }
+
+ @Test
+ public void throwRestClientResponseExceptionTest() {
+ URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
+ AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
+ logger.debug("Invoking {}", uri);
+ ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
+ null, ErrorTransport.class);
+ logger.debug("{}", errorResponse.getBody().getError().toString());
+ Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+ }
+
+ @Test
+ public void throwRuntimeExceptionTest() {
+ URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
+ logger.debug("Invoking {}", uri);
+ ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
+ String.class);
+ logger.debug("{}", errorResponse.getBody());
+ Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+ }
+
}
Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
}
+ @Test
+ public void invalidInstanceTest() {
+ URI uri = buildUri(null, AppManagerController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "bogus",
+ AppManagerController.CONFIG_METHOD);
+ logger.info("Invoking {}", uri);
+ ResponseEntity<Void> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null, Void.class);
+ Assertions.assertTrue(voidResponse.getStatusCode().is5xxServerError());
+ }
+
}
package org.oransc.ric.portal.dashboard.model;
import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.List;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
Assert.assertNotNull(eud.getUsername());
Assert.assertTrue(eud.isAccountNonExpired());
Assert.assertTrue(eud.isAccountNonLocked());
+ Assert.assertTrue(eud.isCredentialsNonExpired());
+ Assert.assertTrue(eud.isEnabled());
+ logger.info(eud.toString());
}
private void checkErrorTransport(ErrorTransport m) {
m.nodebIdentity(nodebIdentity).nodebStatus(nodebResponse);
Assert.assertEquals(m.getNodebIdentity(), nodebIdentity);
Assert.assertEquals(m.getNodebStatus(), nodebResponse);
+ logger.debug(m.toString());
}
private void checkSuccessTransport(SuccessTransport m) {
m.setKey(s1);
m.setName(s2);
checkRicInstanceKeyName(m);
- Assert.assertEquals(m, m);
+ Assert.assertTrue(m.equals(m));
+ Assert.assertFalse(m.equals(null));
+ Assert.assertFalse(m.equals(new RicInstanceKeyName()));
Assert.assertNotEquals(1, m.hashCode());
logger.info(m.toString());
}
m.setKey(s3);
m.setName(s4);
checkRicInstance(m);
- Assert.assertEquals(m, m);
+ Assert.assertTrue(m.equals(m));
+ Assert.assertFalse(m.equals(null));
+ Assert.assertFalse(m.equals(new RicInstance()));
Assert.assertNotEquals(1, m.hashCode());
logger.info(m.toString());
}
+ @Test
+ public void testRicInstanceList() {
+ RicInstanceList m = new RicInstanceList();
+ List<RicInstance> list = new ArrayList<>();
+ m = new RicInstanceList(list);
+ m.getInstances();
+ m.getKeyNameList();
+ try {
+ m.getInstance(s1);
+ } catch (Exception ex) {
+ logger.info("failed as expected", ex);
+ }
+ logger.info(m.toString());
+ }
+
}
--- /dev/null
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 AT&T Intellectual Property
+ * %%
+ * 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 org.oransc.ric.portal.dashboard.portalapi;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
+import org.oransc.ric.portal.dashboard.DashboardUserManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+@ExtendWith(SpringExtension.class)
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+@ActiveProfiles("test")
+public class PortalAuthManagerTest {
+
+ @Value("${portalapi.decryptor}")
+ private String decryptor;
+
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ @Test
+ public void testPortalStuff() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
+ InvocationTargetException, NoSuchMethodException, IOException, ServletException {
+ PortalAuthManager m = new PortalAuthManager("app", "user", "secret", decryptor, "cookie");
+ Assert.assertNotNull(m.getAppCredentials());
+ String s = null;
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ s = m.validateEcompSso(request);
+ logger.debug("validateEcompSso answers {}", s);
+ Assert.assertNull(s);
+
+ Cookie cookie = new Cookie(PortalApiConstants.EP_SERVICE, "bogus");
+ request.setCookies(cookie);
+ s = m.validateEcompSso(request);
+ logger.debug("validateEcompSso answers {}", s);
+ Assert.assertNull(s);
+
+ DashboardUserManager dum = new DashboardUserManager(true);
+ PortalAuthenticationFilter filter = new PortalAuthenticationFilter(false, m, dum);
+ filter.init(null);
+ filter.destroy();
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ try {
+ filter.doFilter(request, response, null);
+ } catch (NullPointerException ex) {
+ logger.debug("chain is null");
+ }
+
+ filter = new PortalAuthenticationFilter(true, m, dum);
+ try {
+ filter.doFilter(request, response, null);
+ } catch (NullPointerException ex) {
+ logger.debug("chain is null");
+ }
+ }
+
+}
\ No newline at end of file